Mercurial > dive4elements > river
comparison flys-aft/src/main/java/org/dive4elements/etl/db/ConnectedStatements.java @ 5824:06643e440d1e
Moved directories to org.dive4elements.etl
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:35:06 +0200 |
parents | flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java@f939e1e6cfa4 |
children |
comparison
equal
deleted
inserted
replaced
5823:52e966cc7d35 | 5824:06643e440d1e |
---|---|
1 package de.intevation.db; | |
2 | |
3 import java.sql.Connection; | |
4 import java.sql.DatabaseMetaData; | |
5 import java.sql.SQLException; | |
6 import java.sql.Savepoint; | |
7 | |
8 import java.util.ArrayDeque; | |
9 import java.util.Deque; | |
10 import java.util.HashMap; | |
11 import java.util.Map; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 public class ConnectedStatements | |
16 { | |
17 private static Logger log = Logger.getLogger(ConnectedStatements.class); | |
18 | |
19 protected Connection connection; | |
20 | |
21 protected Map<String, SymbolicStatement> statements; | |
22 | |
23 protected Map<String, SymbolicStatement.Instance> boundStatements; | |
24 | |
25 protected Deque<Savepoint> savepoints; | |
26 | |
27 public ConnectedStatements( | |
28 Connection connection, | |
29 Map<String, SymbolicStatement> statements | |
30 ) | |
31 throws SQLException | |
32 { | |
33 this.connection = connection; | |
34 this.statements = statements; | |
35 checkSavePoints(); | |
36 | |
37 boundStatements = new HashMap<String, SymbolicStatement.Instance>(); | |
38 } | |
39 | |
40 protected void checkSavePoints() throws SQLException { | |
41 DatabaseMetaData metaData = connection.getMetaData(); | |
42 if (metaData.supportsSavepoints()) { | |
43 log.info("Driver '" + metaData.getDriverName() + | |
44 "' does support savepoints."); | |
45 savepoints = new ArrayDeque<Savepoint>(); | |
46 } | |
47 else { | |
48 log.info("Driver '" + metaData.getDriverName() + | |
49 "' does not support savepoints."); | |
50 } | |
51 } | |
52 | |
53 public SymbolicStatement.Instance getStatement(String key) | |
54 throws SQLException | |
55 { | |
56 SymbolicStatement.Instance stmnt = boundStatements.get(key); | |
57 if (stmnt != null) { | |
58 return stmnt; | |
59 } | |
60 | |
61 SymbolicStatement ss = statements.get(key); | |
62 if (ss == null) { | |
63 return null; | |
64 } | |
65 | |
66 stmnt = ss.new Instance(connection); | |
67 boundStatements.put(key, stmnt); | |
68 return stmnt; | |
69 } | |
70 | |
71 public void beginTransaction() throws SQLException { | |
72 if (savepoints != null) { | |
73 savepoints.push(connection.setSavepoint()); | |
74 } | |
75 } | |
76 | |
77 public void commitTransaction() throws SQLException { | |
78 if (savepoints != null) { | |
79 savepoints.pop(); | |
80 } | |
81 connection.commit(); | |
82 } | |
83 | |
84 public void rollbackTransaction() throws SQLException { | |
85 if (savepoints != null) { | |
86 Savepoint savepoint = savepoints.pop(); | |
87 connection.rollback(savepoint); | |
88 } | |
89 else { | |
90 connection.rollback(); | |
91 } | |
92 } | |
93 | |
94 public void close() { | |
95 for (SymbolicStatement.Instance s: boundStatements.values()) { | |
96 s.close(); | |
97 } | |
98 | |
99 try { | |
100 if (savepoints != null && !savepoints.isEmpty()) { | |
101 Savepoint savepoint = savepoints.peekFirst(); | |
102 connection.rollback(savepoint); | |
103 } | |
104 connection.close(); | |
105 } | |
106 catch (SQLException sqle) { | |
107 } | |
108 } | |
109 } | |
110 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |