dims 01/08/14 09:42:18
Modified: src/org/apache/cocoon/transformation Tag: cocoon_20_branch
SQLTransformer.java
Log:
Patch from tom.klaasen@ the-ecorp.com for various problems with SQLTransformer.
Revision Changes Path
No revision
No revision
1.5.2.8 +107 -64
xml-cocoon2/src/org/apache/cocoon/transformation/SQLTransformer.java
Index: SQLTransformer.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/org/apache/cocoon/transformation/SQLTransformer.java,v
retrieving revision 1.5.2.7
retrieving revision 1.5.2.8
diff -u -r1.5.2.7 -r1.5.2.8
--- SQLTransformer.java 2001/08/14 11:27:01 1.5.2.7
+++ SQLTransformer.java 2001/08/14 16:42:18 1.5.2.8
@@ -57,7 +57,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
* (PWR Organisation & Entwicklung)
* @author <a href="mailto:[EMAIL PROTECTED]">Sven Beauprez</a>
- * @version CVS $Revision: 1.5.2.7 $ $Date: 2001/08/14 11:27:01 $ $Author: dims $
+ * @version CVS $Revision: 1.5.2.8 $ $Date: 2001/08/14 16:42:18 $ $Author: dims $
*/
public class SQLTransformer extends AbstractTransformer implements Composable,
Recyclable, Disposable, Configurable {
@@ -112,7 +112,7 @@
/** The offset of the current query in the queries list **/
protected int current_query_index;
-
+
/** The name of the value element we're currently receiving **/
protected String current_name;
@@ -230,7 +230,7 @@
default_properties.setProperty(SQLTransformer.MAGIC_ROW_ELEMENT,
parameter);
}
}
-
+
/** END SitemapComponent methods **/
/** BEGIN my very own methods **/
@@ -328,7 +328,7 @@
attributes.getValue("",
SQLTransformer.MAGIC_STORED_PROCEDURE_ATTRIBUTE);
if (isstoredprocedure != null &&
!isstoredprocedure.equalsIgnoreCase("false"))
q.setStoredProcedure(true);
- String name =
+ String name =
attributes.getValue("", SQLTransformer.MAGIC_NAME_ATTRIBUTE);
if (name != null) {
q.setName(name);
@@ -513,7 +513,7 @@
protected Query getQuery(int i) {
return (Query) queries.elementAt(i);
}
-
+
private String replaceCharWithString(String in, char c, String with) {
Tokenizer tok;
StringBuffer replaced=null;
@@ -683,7 +683,7 @@
protected boolean isstoredprocedure = false;
protected String name=null;
-
+
/** If it is an update/etc, the return value (num rows modified) **/
protected int rv = -1;
@@ -720,7 +720,7 @@
protected boolean isStoredProcedure() {
return isstoredprocedure;
}
-
+
protected void setName(String name) {
this.name = name;
}
@@ -794,6 +794,50 @@
}
}
+ /** Get a Connection. Made this a separate method to separate the logic
from the actual execution. */
+ protected Connection getConnection() throws SQLException {
+ Connection result = null;
+
+ String connection =
properties.getProperty(SQLTransformer.MAGIC_CONNECTION);
+ String dburl = properties.getProperty(SQLTransformer.MAGIC_DBURL);
+ String username = properties.getProperty(SQLTransformer.MAGIC_USERNAME);
+ String password = properties.getProperty(SQLTransformer.MAGIC_PASSWORD);
+
+ DataSourceComponent datasource = null;
+ try {
+ if (connection != null) {
+ datasource = (DataSourceComponent)
dbSelector.select(connection);
+ while (result == null) {
+ try {
+ result = datasource.getConnection();
+ } catch (Exception e) {
+ long waittime = 5000 * (long)Math.random();
+ getTheLogger().debug("SQLTransformer$Query: could not
acquire a Connection -- waiting "+waittime+" ms to try again.");
+ try {
+ Thread.sleep(waittime);
+ } catch (InterruptedException ie) {}
+ }
+ }
+ } else {
+ if (username == null || password == null) {
+ result = DriverManager.getConnection(dburl);
+ } else {
+ result = DriverManager.getConnection(dburl, username,
+ password);
+ }
+ }
+ } catch (ComponentException cme) {
+ transformer.getTheLogger().error("Could not use connection: " +
connection, cme);
+ } catch (SQLException e) {
+ transformer.getTheLogger().error("Caught a SQLException", e);
+ throw e;
+ } finally {
+ if (datasource != null) dbSelector.release(datasource);
+ }
+
+ return result;
+ }
+
protected void execute() throws SQLException {
if (null != properties.getProperty(SQLTransformer.MAGIC_DOC_ELEMENT)) {
this.rowset_name =
properties.getProperty(SQLTransformer.MAGIC_DOC_ELEMENT);
@@ -801,10 +845,7 @@
if (null != properties.getProperty(SQLTransformer.MAGIC_ROW_ELEMENT)) {
this.row_name =
properties.getProperty(SQLTransformer.MAGIC_ROW_ELEMENT);
}
- String connection =
properties.getProperty(SQLTransformer.MAGIC_CONNECTION);
- String dburl = properties.getProperty(SQLTransformer.MAGIC_DBURL);
- String username = properties.getProperty(SQLTransformer.MAGIC_USERNAME);
- String password = properties.getProperty(SQLTransformer.MAGIC_PASSWORD);
+
Enumeration enum = query_parts.elements();
StringBuffer sb = new StringBuffer();
while (enum.hasMoreElements()) {
@@ -820,19 +861,10 @@
}
String query = sb.toString();
transformer.getTheLogger().debug("EXECUTING " + query);
- DataSourceComponent datasource = null;
+
+ conn = getConnection();
+
try {
- if (connection != null) {
- datasource = (DataSourceComponent)
dbSelector.select(connection);
- conn = datasource.getConnection();
- } else {
- if (username == null || password == null) {
- conn = DriverManager.getConnection(dburl);
- } else {
- conn = DriverManager.getConnection(dburl, username,
- password);
- }
- }
if (!isstoredprocedure) {
if (oldDriver) {
pst = conn.prepareStatement(query);
@@ -852,6 +884,7 @@
registerOutParameters(cst);
pst = cst;
}
+
registerInParameters(pst);
boolean result = pst.execute();
if (result) {
@@ -863,10 +896,9 @@
} catch (SQLException e) {
transformer.getTheLogger().error("Caught a SQLException", e);
throw e;
- } catch (ComponentException cme) {
- transformer.getTheLogger().error("Could not use connection: " +
connection, cme);
} finally {
- if (datasource != null) dbSelector.release(datasource);
+ conn.close();
+ conn = null; // To make sure we don't use this connection
again.
}
}
@@ -876,9 +908,13 @@
if (oldDriver){
nr=-1;
} else {
- rs.last();
- nr = rs.getRow();
- rs.beforeFirst();
+ try {
+ rs.last();
+ nr = rs.getRow();
+ rs.beforeFirst();
+ } catch (NullPointerException e) { // A
NullPointerException here crashes a whole lot of C2 -- catching it so it won't do any
harm for now, but seems like it should be solved seriously
+ getTheLogger().error("NPE while getting the nr of rows", e);
+ }
}
} else {
if (outParameters!=null) {
@@ -910,17 +946,21 @@
protected void close() throws SQLException {
try {
- if (rs != null) {
+ if (rs != null)
try {
+ //getTheLogger().debug("Trying to close resultset
"+rs.toString());
rs.close();
+ rs = null; // This prevents us from using the
resultset again.
+ //250getTheLogger().debug("Really closed the resultset
now.");
} catch (NullPointerException e) {
getTheLogger().debug("NullPointer while closing the
resultset.", e);
}
- }
if (pst != null)
pst.close();
+ pst = null; // Prevent using pst again.
if (cst != null)
cst.close();
+ cst = null; // Prevent using cst again.
} finally {
if (conn != null)
conn.close();
@@ -947,49 +987,52 @@
rv = -1; // we only want the return code shown once.
}
}
-
+
protected void serializeStoredProcedure() throws SQLException, SAXException
{
if (outParametersNames==null || cst==null)
return;
- //make sure output follows order as parameter order in stored procedure
+ //make sure output follows order as parameter order in stored procedure
Iterator itOutKeys = (new TreeMap(outParameters)).keySet().iterator();
Integer counter;
AttributesImpl attr = new AttributesImpl();
- while (itOutKeys.hasNext()) {
- counter = (Integer)itOutKeys.next();
- try {
- Object obj = cst.getObject(counter.intValue());
- if (!(obj instanceof ResultSet)) {
- transformer.start((String)outParametersNames.get(counter), attr);
- transformer.data(transformer.getStringValue(obj));
- transformer.end((String)outParametersNames.get(counter));
- } else {
- ResultSet rs = (ResultSet)obj;
- try{
- transformer.start((String)outParametersNames.get(counter),
attr);
- ResultSetMetaData md = rs.getMetaData();
- while (rs.next()) {
- transformer.start(this.row_name, attr);
- for (int i = 1; i <= md.getColumnCount(); i++) {
- transformer.start(md.getColumnName(i).toLowerCase(),
attr);
-
transformer.data(transformer.getStringValue(rs.getObject(i)));
- transformer.end(md.getColumnName(i).toLowerCase());
- }
- transformer.end(this.row_name);
+ try {
+ while (itOutKeys.hasNext()) {
+ counter = (Integer)itOutKeys.next();
+ try {
+ if (cst == null) getTheLogger().debug("SQLTransformer: cst is
null");
+ if (counter == null) getTheLogger().debug(" SQLTransformer:
counter is null");
+ Object obj = cst.getObject(counter.intValue());
+ if (!(obj instanceof ResultSet)) {
+ transformer.start((String)outParametersNames.get(counter),
attr);
+ transformer.data(transformer.getStringValue(obj));
+ transformer.end((String)outParametersNames.get(counter));
+ } else {
+ ResultSet rs = (ResultSet)obj;
+ try{
+
transformer.start((String)outParametersNames.get(counter), attr);
+ ResultSetMetaData md = rs.getMetaData();
+ while (rs.next()) {
+ transformer.start(this.row_name, attr);
+ for (int i = 1; i <= md.getColumnCount(); i++) {
+
transformer.start(md.getColumnName(i).toLowerCase(), attr);
+
transformer.data(transformer.getStringValue(rs.getObject(i)));
+
transformer.end(md.getColumnName(i).toLowerCase());
+ }
+ transformer.end(this.row_name);
+ }
+ }finally{
+ rs.close();
}
- }finally{
- rs.close();
+ transformer.end((String)outParametersNames.get(counter));
}
- transformer.end((String)outParametersNames.get(counter));
- }
- }catch (SQLException e) {
- transformer.getTheLogger().error("Caught a SQLException", e);
- throw e;
- } finally {
- close();
+ }catch (SQLException e) {
+ transformer.getTheLogger().error("Caught a SQLException", e);
+ throw e;
+ }
}
+ } finally {
+ close();
}
-
}
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]