Mercurial > dive4elements > river
changeset 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 (2012-01-02) |
parents | 067341e86375 |
children | aad1886ea226 |
files | flys-aft/ChangeLog flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java |
diffstat | 3 files changed, 66 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-aft/ChangeLog Thu Dec 22 12:07:29 2011 +0000 +++ b/flys-aft/ChangeLog Mon Jan 02 15:13:58 2012 +0000 @@ -1,3 +1,12 @@ +2012-01-02 Sascha L. Teichmann <sascha.teichmann@inteavtion.de> + + * src/main/java/de/intevation/db/ConnectionBuilder.java: + Set auto commit of new connection to false to enable transaction. + + * src/main/java/de/intevation/db/ConnectedStatements.java: + Added methods to begin, commit and rollback transactions. + Relies on savepoint support which is check by database metadata. + 2011-12-22 Sascha L. Teichmann <sascha.teichmann@inteavtion.de> * src/main/java/de/intevation/aft/River.java: Figure out
--- a/flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java Thu Dec 22 12:07:29 2011 +0000 +++ b/flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java Mon Jan 02 15:13:58 2012 +0000 @@ -2,9 +2,12 @@ import java.util.HashMap; import java.util.Map; +import java.util.Deque; +import java.util.ArrayDeque; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Savepoint; public class ConnectedStatements { @@ -14,16 +17,27 @@ protected Map<String, SymbolicStatement.Instance> boundStatements; + protected Deque<Savepoint> savepoints; + public ConnectedStatements( Connection connection, Map<String, SymbolicStatement> statements - ) { + ) + throws SQLException + { this.connection = connection; this.statements = statements; + checkSavePoints(); boundStatements = new HashMap<String, SymbolicStatement.Instance>(); } + protected void checkSavePoints() throws SQLException { + if (connection.getMetaData().supportsSavepoints()) { + savepoints = new ArrayDeque<Savepoint>(); + } + } + public SymbolicStatement.Instance getStatement(String key) throws SQLException { @@ -42,12 +56,39 @@ return stmnt; } + void beginTransaction() throws SQLException { + if (savepoints != null) { + savepoints.push(connection.setSavepoint()); + } + } + + void commitTransaction() throws SQLException { + if (savepoints != null) { + savepoints.pop(); + } + connection.commit(); + } + + void rollbackTransaction() throws SQLException { + if (savepoints != null) { + Savepoint savepoint = savepoints.pop(); + connection.rollback(savepoint); + } + else { + connection.rollback(); + } + } + public void close() { for (SymbolicStatement.Instance s: boundStatements.values()) { s.close(); } try { + if (savepoints != null && !savepoints.isEmpty()) { + Savepoint savepoint = savepoints.peekFirst(); + connection.rollback(savepoint); + } connection.close(); } catch (SQLException sqle) {
--- a/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java Thu Dec 22 12:07:29 2011 +0000 +++ b/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java Mon Jan 02 15:13:58 2012 +0000 @@ -11,6 +11,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.DriverManager; +import java.sql.DatabaseMetaData; import org.apache.log4j.Logger; @@ -66,7 +67,20 @@ } } - return DriverManager.getConnection(url, user, password); + Connection connection = + DriverManager.getConnection(url, user, password); + + connection.setAutoCommit(false); + + DatabaseMetaData metaData = connection.getMetaData(); + + if (metaData.supportsTransactionIsolationLevel( + Connection.TRANSACTION_READ_UNCOMMITTED)) { + connection.setTransactionIsolation( + Connection.TRANSACTION_READ_UNCOMMITTED); + } + + return connection; } public ConnectedStatements getConnectedStatements() throws SQLException {