bruno 2003/10/06 09:24:16
Modified: src/blocks/databases/java/org/apache/cocoon/transformation
SQLTransformer.java
Log:
Applying patch 16718 "Using only one connection in SQLTransformer" by Daniel
Fagerstrom
Revision Changes Path
1.10 +34 -5
cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/transformation/SQLTransformer.java
Index: SQLTransformer.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/transformation/SQLTransformer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- SQLTransformer.java 20 Sep 2003 17:15:32 -0000 1.9
+++ SQLTransformer.java 6 Oct 2003 16:24:16 -0000 1.10
@@ -203,6 +203,9 @@
this.namespaceURI = NAMESPACE;
}
+ /** The connection used by all top level queries */
+ protected Connection conn;
+
/**
* Composable
*/
@@ -221,6 +224,15 @@
*/
public void recycle() {
super.recycle();
+ try {
+ // Close the connection used by all top level queries
+ if (this.conn != null) {
+ this.conn.close();
+ this.conn = null;
+ }
+ } catch ( SQLException e ) {
+ getLogger().warn( "Could not close the connection", e );
+ }
this.queries.clear();
this.outUri = null;
this.outPrefix = null;
@@ -304,8 +316,19 @@
AttributesImpl attr = new AttributesImpl();
Query query = (Query) queries.elementAt( index );
boolean query_failure = false;
+ Connection conn = null;
try {
try {
+ if (index == 0) {
+ if (this.conn == null) // The first top level
execute-query
+ this.conn = query.getConnection();
+ // reuse the global connection for all top level queries
+ conn = this.conn;
+ }
+ else // index > 0, sub queries are always executed in an own
connection
+ conn = query.getConnection();
+
+ query.setConnection(conn);
query.execute();
} catch ( SQLException e ) {
if (getLogger().isDebugEnabled()) {
@@ -356,6 +379,8 @@
} finally {
try {
query.close();
+ if (index > 0) // close the connection used by a sub query
+ conn.close();
} catch ( SQLException e ) {
getLogger().warn( "SQLTransformer: Could not close JDBC
connection", e );
}
@@ -969,6 +994,10 @@
}
}
+ protected void setConnection(Connection conn) {
+ this.conn = conn;
+ }
+
/** Get a Connection. Made this a separate method to separate the
logic from the actual execution. */
protected Connection getConnection() throws SQLException {
Connection result = null;
@@ -1030,6 +1059,10 @@
}
protected void execute() throws SQLException {
+ if (this.conn == null) {
+ throw new SQLException("A connection must be set before
executing a query");
+ }
+
this.rowset_name = properties.getParameter(
SQLTransformer.MAGIC_DOC_ELEMENT, "rowset" );
this.row_name = properties.getParameter(
SQLTransformer.MAGIC_ROW_ELEMENT, "row" );
@@ -1058,8 +1091,6 @@
transformer.getTheLogger().debug( "EXECUTING " + query );
}
- conn = getConnection();
-
try {
if ( !isstoredprocedure ) {
if ( oldDriver ) {
@@ -1191,8 +1222,6 @@
cst.close();
cst = null; // Prevent using cst again.
} finally {
- if ( conn != null )
- conn.close();
conn = null;
}
}