sascha@4074: package de.intevation.db; sascha@4074: sascha@4074: import java.util.HashMap; sascha@4074: import java.util.Map; sascha@4086: import java.util.Deque; sascha@4086: import java.util.ArrayDeque; sascha@4074: sascha@4074: import java.sql.Connection; sascha@4074: import java.sql.SQLException; sascha@4086: import java.sql.Savepoint; sascha@4074: sascha@4074: public class ConnectedStatements sascha@4074: { sascha@4074: protected Connection connection; sascha@4074: sascha@4074: protected Map statements; sascha@4074: sascha@4074: protected Map boundStatements; sascha@4074: sascha@4086: protected Deque savepoints; sascha@4086: sascha@4074: public ConnectedStatements( sascha@4074: Connection connection, sascha@4074: Map statements sascha@4086: ) sascha@4086: throws SQLException sascha@4086: { sascha@4074: this.connection = connection; sascha@4074: this.statements = statements; sascha@4086: checkSavePoints(); sascha@4074: sascha@4074: boundStatements = new HashMap(); sascha@4074: } sascha@4074: sascha@4086: protected void checkSavePoints() throws SQLException { sascha@4086: if (connection.getMetaData().supportsSavepoints()) { sascha@4086: savepoints = new ArrayDeque(); sascha@4086: } sascha@4086: } sascha@4086: sascha@4074: public SymbolicStatement.Instance getStatement(String key) sascha@4074: throws SQLException sascha@4074: { sascha@4074: SymbolicStatement.Instance stmnt = boundStatements.get(key); sascha@4074: if (stmnt != null) { sascha@4074: return stmnt; sascha@4074: } sascha@4074: sascha@4074: SymbolicStatement ss = statements.get(key); sascha@4074: if (ss == null) { sascha@4074: return null; sascha@4074: } sascha@4074: sascha@4074: stmnt = ss.new Instance(connection); sascha@4074: boundStatements.put(key, stmnt); sascha@4074: return stmnt; sascha@4074: } sascha@4074: sascha@4086: void beginTransaction() throws SQLException { sascha@4086: if (savepoints != null) { sascha@4086: savepoints.push(connection.setSavepoint()); sascha@4086: } sascha@4086: } sascha@4086: sascha@4086: void commitTransaction() throws SQLException { sascha@4086: if (savepoints != null) { sascha@4086: savepoints.pop(); sascha@4086: } sascha@4086: connection.commit(); sascha@4086: } sascha@4086: sascha@4086: void rollbackTransaction() throws SQLException { sascha@4086: if (savepoints != null) { sascha@4086: Savepoint savepoint = savepoints.pop(); sascha@4086: connection.rollback(savepoint); sascha@4086: } sascha@4086: else { sascha@4086: connection.rollback(); sascha@4086: } sascha@4086: } sascha@4086: sascha@4074: public void close() { sascha@4074: for (SymbolicStatement.Instance s: boundStatements.values()) { sascha@4074: s.close(); sascha@4074: } sascha@4074: sascha@4074: try { sascha@4086: if (savepoints != null && !savepoints.isEmpty()) { sascha@4086: Savepoint savepoint = savepoints.peekFirst(); sascha@4086: connection.rollback(savepoint); sascha@4086: } sascha@4074: connection.close(); sascha@4074: } sascha@4074: catch (SQLException sqle) { sascha@4074: } sascha@4074: } sascha@4074: } sascha@4074: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :