sascha@4074: package de.intevation.db; sascha@4074: teichmann@4772: import java.sql.Connection; teichmann@4772: import java.sql.DatabaseMetaData; teichmann@4772: import java.sql.SQLException; teichmann@4772: import java.sql.Savepoint; teichmann@4772: teichmann@4772: import java.util.ArrayDeque; teichmann@4772: import java.util.Deque; sascha@4074: import java.util.HashMap; sascha@4074: import java.util.Map; sascha@4087: sascha@4087: import org.apache.log4j.Logger; sascha@4074: sascha@4074: public class ConnectedStatements sascha@4074: { sascha@4087: private static Logger log = Logger.getLogger(ConnectedStatements.class); sascha@4087: 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@4087: DatabaseMetaData metaData = connection.getMetaData(); sascha@4087: if (metaData.supportsSavepoints()) { sascha@4087: log.info("Driver '" + metaData.getDriverName() + sascha@4087: "' does support savepoints."); sascha@4086: savepoints = new ArrayDeque(); sascha@4086: } sascha@4087: else { teichmann@4736: log.info("Driver '" + metaData.getDriverName() + sascha@4087: "' does not support savepoints."); sascha@4087: } sascha@4086: } sascha@4086: teichmann@4736: 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@4087: public void beginTransaction() throws SQLException { sascha@4086: if (savepoints != null) { sascha@4086: savepoints.push(connection.setSavepoint()); sascha@4086: } sascha@4086: } sascha@4086: sascha@4087: public void commitTransaction() throws SQLException { sascha@4086: if (savepoints != null) { sascha@4086: savepoints.pop(); sascha@4086: } sascha@4086: connection.commit(); sascha@4086: } sascha@4086: sascha@4087: public 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 :