# HG changeset patch # User Sascha L. Teichmann # Date 1323786775 0 # Node ID 100c4e0a45e1980ad6d1814447771921be9b8186 # Parent 2c70fae83d0c30dee2f35056ef27bf70b66858aa Improved infrastructure for prepared statements. flys-aft/trunk@3407 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 2c70fae83d0c -r 100c4e0a45e1 flys-aft/ChangeLog --- a/flys-aft/ChangeLog Tue Dec 13 13:41:43 2011 +0000 +++ b/flys-aft/ChangeLog Tue Dec 13 14:32:55 2011 +0000 @@ -1,3 +1,14 @@ +2011-12-13 Sascha L. Teichmann + + * src/main/java/de/intevation/db/ConnectionBuilder.java: Removed + statements here. + + * src/main/java/de/intevation/db/Statements.java: Added method + to access the hole map of statements. + + * src/main/java/de/intevation/db/ConnectedStatements.java: New. + A cache that binds prepared statements to a connection. + 2011-12-13 Sascha L. Teichmann * src/main/java/de/intevation/db/ConnectionBuilder.java: Added diff -r 2c70fae83d0c -r 100c4e0a45e1 flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java Tue Dec 13 14:32:55 2011 +0000 @@ -0,0 +1,57 @@ +package de.intevation.db; + +import java.util.HashMap; +import java.util.Map; + +import java.sql.Connection; +import java.sql.SQLException; + +public class ConnectedStatements +{ + protected Connection connection; + + protected Map statements; + + protected Map boundStatements; + + public ConnectedStatements( + Connection connection, + Map statements + ) { + this.connection = connection; + this.statements = statements; + + boundStatements = new HashMap(); + } + + 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 close() { + for (SymbolicStatement.Instance s: boundStatements.values()) { + s.close(); + } + + try { + connection.close(); + } + catch (SQLException sqle) { + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 2c70fae83d0c -r 100c4e0a45e1 flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java --- a/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java Tue Dec 13 13:41:43 2011 +0000 +++ b/flys-aft/src/main/java/de/intevation/db/ConnectionBuilder.java Tue Dec 13 14:32:55 2011 +0000 @@ -29,12 +29,9 @@ protected String password; protected String url; - protected Statements statements; - public ConnectionBuilder(String type, Document document) { this.type = type; extractCredentials(document); - statements = new Statements(type, driver != null ? driver : ""); } protected void extractCredentials(Document document) { @@ -72,8 +69,11 @@ return DriverManager.getConnection(url, user, password); } - public SymbolicStatement getStatement(String key) { - return statements.getStatement(key); + public ConnectedStatements getConnectedStatements() throws SQLException { + return new ConnectedStatements( + getConnection(), + new Statements(type, driver != null ? driver : "") + .getStatements()); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 2c70fae83d0c -r 100c4e0a45e1 flys-aft/src/main/java/de/intevation/db/Statements.java --- a/flys-aft/src/main/java/de/intevation/db/Statements.java Tue Dec 13 13:41:43 2011 +0000 +++ b/flys-aft/src/main/java/de/intevation/db/Statements.java Tue Dec 13 14:32:55 2011 +0000 @@ -29,10 +29,14 @@ } public SymbolicStatement getStatement(String key) { + return getStatements().get(key); + } + + public Map getStatements() { if (statements == null) { statements = loadStatements(); } - return statements.get(key); + return statements; } protected Map loadStatements() {