annotate 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
rev   line source
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.db;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.util.HashMap;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.util.Map;
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
5 import java.util.Deque;
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
6 import java.util.ArrayDeque;
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 import java.sql.Connection;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import java.sql.SQLException;
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
10 import java.sql.Savepoint;
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 public class ConnectedStatements
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 protected Connection connection;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 protected Map<String, SymbolicStatement> statements;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 protected Map<String, SymbolicStatement.Instance> boundStatements;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
20 protected Deque<Savepoint> savepoints;
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
21
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 public ConnectedStatements(
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 Connection connection,
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 Map<String, SymbolicStatement> statements
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
25 )
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
26 throws SQLException
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
27 {
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 this.connection = connection;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 this.statements = statements;
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
30 checkSavePoints();
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 boundStatements = new HashMap<String, SymbolicStatement.Instance>();
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
35 protected void checkSavePoints() throws SQLException {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
36 if (connection.getMetaData().supportsSavepoints()) {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
37 savepoints = new ArrayDeque<Savepoint>();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
38 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
39 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
40
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 public SymbolicStatement.Instance getStatement(String key)
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 throws SQLException
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 SymbolicStatement.Instance stmnt = boundStatements.get(key);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 if (stmnt != null) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 return stmnt;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 SymbolicStatement ss = statements.get(key);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 if (ss == null) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 return null;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 stmnt = ss.new Instance(connection);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 boundStatements.put(key, stmnt);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 return stmnt;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
59 void beginTransaction() throws SQLException {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
60 if (savepoints != null) {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
61 savepoints.push(connection.setSavepoint());
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
62 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
63 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
64
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
65 void commitTransaction() throws SQLException {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
66 if (savepoints != null) {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
67 savepoints.pop();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
68 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
69 connection.commit();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
70 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
71
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
72 void rollbackTransaction() throws SQLException {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
73 if (savepoints != null) {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
74 Savepoint savepoint = savepoints.pop();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
75 connection.rollback(savepoint);
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
76 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
77 else {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
78 connection.rollback();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
79 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
80 }
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
81
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 public void close() {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 for (SymbolicStatement.Instance s: boundStatements.values()) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 s.close();
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 try {
4086
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
88 if (savepoints != null && !savepoints.isEmpty()) {
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
89 Savepoint savepoint = savepoints.peekFirst();
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
90 connection.rollback(savepoint);
ee0c60757a94 Added transaction support
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4074
diff changeset
91 }
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 connection.close();
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 catch (SQLException sqle) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org