diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/java/org/dive4elements/etl/db/ConnectedStatements.java	Thu Apr 25 11:35:06 2013 +0200
@@ -0,0 +1,110 @@
+package de.intevation.db;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import java.sql.Savepoint;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+public class ConnectedStatements
+{
+    private static Logger log = Logger.getLogger(ConnectedStatements.class);
+
+    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 {
+        DatabaseMetaData metaData = connection.getMetaData();
+        if (metaData.supportsSavepoints()) {
+            log.info("Driver '" + metaData.getDriverName() +
+                "' does support savepoints.");
+            savepoints = new ArrayDeque<Savepoint>();
+        }
+        else {
+            log.info("Driver '" + metaData.getDriverName() +
+                "' does not support savepoints.");
+        }
+    }
+
+    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;
+    }
+
+    public void beginTransaction() throws SQLException {
+        if (savepoints != null) {
+            savepoints.push(connection.setSavepoint());
+        }
+    }
+
+    public void commitTransaction() throws SQLException {
+        if (savepoints != null) {
+            savepoints.pop();
+        }
+        connection.commit();
+    }
+
+    public 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 :

http://dive4elements.wald.intevation.org