Mercurial > dive4elements > river
view 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 |
line wrap: on
line source
package de.intevation.db; 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 { protected Connection connection; protected Map<String, SymbolicStatement> statements; 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 { SymbolicStatement.Instance stmnt = boundStatements.get(key); if (stmnt != null) { return stmnt; } SymbolicStatement ss = statements.get(key); if (ss == null) { return null; } stmnt = ss.new Instance(connection); boundStatements.put(key, stmnt); 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) { } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :