comparison flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java @ 4086:ee0c60757a94

Added transaction support flys-aft/trunk@3566 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 02 Jan 2012 15:13:58 +0000
parents 100c4e0a45e1
children aad1886ea226
comparison
equal deleted inserted replaced
4085:067341e86375 4086:ee0c60757a94
1 package de.intevation.db; 1 package de.intevation.db;
2 2
3 import java.util.HashMap; 3 import java.util.HashMap;
4 import java.util.Map; 4 import java.util.Map;
5 import java.util.Deque;
6 import java.util.ArrayDeque;
5 7
6 import java.sql.Connection; 8 import java.sql.Connection;
7 import java.sql.SQLException; 9 import java.sql.SQLException;
10 import java.sql.Savepoint;
8 11
9 public class ConnectedStatements 12 public class ConnectedStatements
10 { 13 {
11 protected Connection connection; 14 protected Connection connection;
12 15
13 protected Map<String, SymbolicStatement> statements; 16 protected Map<String, SymbolicStatement> statements;
14 17
15 protected Map<String, SymbolicStatement.Instance> boundStatements; 18 protected Map<String, SymbolicStatement.Instance> boundStatements;
16 19
20 protected Deque<Savepoint> savepoints;
21
17 public ConnectedStatements( 22 public ConnectedStatements(
18 Connection connection, 23 Connection connection,
19 Map<String, SymbolicStatement> statements 24 Map<String, SymbolicStatement> statements
20 ) { 25 )
26 throws SQLException
27 {
21 this.connection = connection; 28 this.connection = connection;
22 this.statements = statements; 29 this.statements = statements;
30 checkSavePoints();
23 31
24 boundStatements = new HashMap<String, SymbolicStatement.Instance>(); 32 boundStatements = new HashMap<String, SymbolicStatement.Instance>();
33 }
34
35 protected void checkSavePoints() throws SQLException {
36 if (connection.getMetaData().supportsSavepoints()) {
37 savepoints = new ArrayDeque<Savepoint>();
38 }
25 } 39 }
26 40
27 public SymbolicStatement.Instance getStatement(String key) 41 public SymbolicStatement.Instance getStatement(String key)
28 throws SQLException 42 throws SQLException
29 { 43 {
40 stmnt = ss.new Instance(connection); 54 stmnt = ss.new Instance(connection);
41 boundStatements.put(key, stmnt); 55 boundStatements.put(key, stmnt);
42 return stmnt; 56 return stmnt;
43 } 57 }
44 58
59 void beginTransaction() throws SQLException {
60 if (savepoints != null) {
61 savepoints.push(connection.setSavepoint());
62 }
63 }
64
65 void commitTransaction() throws SQLException {
66 if (savepoints != null) {
67 savepoints.pop();
68 }
69 connection.commit();
70 }
71
72 void rollbackTransaction() throws SQLException {
73 if (savepoints != null) {
74 Savepoint savepoint = savepoints.pop();
75 connection.rollback(savepoint);
76 }
77 else {
78 connection.rollback();
79 }
80 }
81
45 public void close() { 82 public void close() {
46 for (SymbolicStatement.Instance s: boundStatements.values()) { 83 for (SymbolicStatement.Instance s: boundStatements.values()) {
47 s.close(); 84 s.close();
48 } 85 }
49 86
50 try { 87 try {
88 if (savepoints != null && !savepoints.isEmpty()) {
89 Savepoint savepoint = savepoints.peekFirst();
90 connection.rollback(savepoint);
91 }
51 connection.close(); 92 connection.close();
52 } 93 }
53 catch (SQLException sqle) { 94 catch (SQLException sqle) {
54 } 95 }
55 } 96 }

http://dive4elements.wald.intevation.org