Clear statement parameters before new call.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / attrresolv / provider / JDBCDataConnector.java
index dcacc59..928a721 100644 (file)
@@ -113,6 +113,11 @@ public class JDBCDataConnector extends BaseDataConnector implements DataConnecto
                        loadDriver(dbDriverName);
                }
 
+               String validationQuery = e.getAttribute("validationQuery");
+               if (validationQuery == null || validationQuery.equals("")) {
+                       validationQuery = "select 1";
+               }
+
                //Load site-specific implementation classes     
                setupAttributeExtractor(
                        (Element) e.getElementsByTagNameNS(AttributeResolver.resolverNamespace, "AttributeExtractor").item(
@@ -162,13 +167,13 @@ public class JDBCDataConnector extends BaseDataConnector implements DataConnecto
                        log.error("JDBC connection requires a dbURL property");
                        throw new ResolutionPlugInException("JDBCDataConnection requires a \"dbURL\" property");
                }
-               setupDataSource(e.getAttribute("dbURL"), props, maxActive, maxIdle, maxWait);
+               setupDataSource(e.getAttribute("dbURL"), props, maxActive, maxIdle, maxWait, validationQuery);
        }
 
        /**
         * Initialize a Pooling Data Source
         */
-       private void setupDataSource(String dbURL, Properties props, int maxActive, int maxIdle, int maxWait) throws ResolutionPlugInException {
+       private void setupDataSource(String dbURL, Properties props, int maxActive, int maxIdle, int maxWait, String validationQuery) throws ResolutionPlugInException {
 
                GenericObjectPool objectPool = new GenericObjectPool(null);
 
@@ -203,9 +208,9 @@ public class JDBCDataConnector extends BaseDataConnector implements DataConnecto
                                connFactory,
                                objectPool,
                                new StackKeyedObjectPoolFactory(),
-                               "select 1",
-                               false,
-                               true);
+                               validationQuery,
+                               true,
+                                       false);
                } catch (Exception ex) {
                        log.debug("Poolable connection factory error");
                }
@@ -302,7 +307,7 @@ public class JDBCDataConnector extends BaseDataConnector implements DataConnecto
                        conn = dataSource.getConnection();
                        log.debug("Connection retrieved from pool");
                } catch (Exception e) {
-                       log.error("Unable to fetch a connection from the pool");
+                       log.error("JDBC Connector (" + getId() + ") unable to fetch a connection from the pool");
                        throw new ResolutionPlugInException("Unable to fetch a connection from the pool: " + e.getMessage());
                }
                if (conn == null) {
@@ -315,6 +320,7 @@ public class JDBCDataConnector extends BaseDataConnector implements DataConnecto
                PreparedStatement preparedStatement = null;
                try {
                        preparedStatement = conn.prepareStatement(searchVal);
+            preparedStatement.clearParameters();
                        statementCreator.create(preparedStatement, principal, requester, depends);
                        rs = preparedStatement.executeQuery();
                        if (!rs.next()) {
@@ -644,8 +650,8 @@ class DependencyStatementCreator implements JDBCStatementCreator {
                        }
 
                } catch (Exception e) {
-                       log.error("Encountered an error while creating prepared statement: " + e);
-                       throw new JDBCStatementCreatorException(
+                       log.error("Encountered an error while creating prepared statement (principal=" + principal.getName() + "): " + e);
+                        throw new JDBCStatementCreatorException(
                                "Encountered an error while creating prepared statement: " + e.getMessage());
                }
        }