Only apply state once, so if the connection state interceptor is not applied, then...
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 15 Nov 2008 03:51:51 +0000 (03:51 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 15 Nov 2008 03:51:51 +0000 (03:51 +0000)
Add abstract interceptor to handle statement wrappers and creation, to be used later when we do a clean up and performance interceptor

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@714217 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/AbstractCreateStatementInterceptor.java [new file with mode: 0644]

index a506f52..7288f0d 100644 (file)
@@ -24,6 +24,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
+
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -80,12 +82,13 @@ public class PooledConnection {
         poolProperties.getDbProperties().setProperty("user", usr);
         poolProperties.getDbProperties().setProperty("password", pwd);
         connection = driver.connect(driverURL, poolProperties.getDbProperties());
-        //set up the default state
-        if (poolProperties.getDefaultReadOnly()!=null) connection.setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
-        if (poolProperties.getDefaultAutoCommit()!=null) connection.setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
-        if (poolProperties.getDefaultCatalog()!=null) connection.setCatalog(poolProperties.getDefaultCatalog());
-        if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) connection.setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
-        
+        //set up the default state, unless we expect the interceptor to do it
+        if (poolProperties.getJdbcInterceptors()==null || poolProperties.getJdbcInterceptors().indexOf(ConnectionState.class.getName())<0) {
+            if (poolProperties.getDefaultReadOnly()!=null) connection.setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
+            if (poolProperties.getDefaultAutoCommit()!=null) connection.setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
+            if (poolProperties.getDefaultCatalog()!=null) connection.setCatalog(poolProperties.getDefaultCatalog());
+            if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) connection.setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
+        }        
         this.discarded = false;
     }
 
diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/AbstractCreateStatementInterceptor.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/AbstractCreateStatementInterceptor.java
new file mode 100644 (file)
index 0000000..bf5fcf1
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.jdbc.pool.interceptor;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.sql.CallableStatement;
+import java.sql.SQLException;
+
+import org.apache.tomcat.jdbc.pool.ConnectionPool;
+import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
+import org.apache.tomcat.jdbc.pool.PooledConnection;
+
+/**
+ * @author Filip Hanik
+ * @version 1.0
+ */
+public abstract class  AbstractCreateStatementInterceptor extends JdbcInterceptor {
+    public static final String[] statements = {"createStatement","prepareStatement","prepareCall"};
+    public static final String[] executes = {"execute","executeQuery","executeUpdate","executeBatch"};
+
+    public  AbstractCreateStatementInterceptor() {
+        super();
+    }
+
+    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+        boolean process = false;
+        process = process(statements, method, process);
+        if (process) {
+            Object statement = super.invoke(proxy,method,args);
+            return createStatement(proxy,method,args,statement);
+        } else {
+            return super.invoke(proxy,method,args);
+        }
+    }
+    
+    /**
+     * This method should return a wrapper object around a
+     * java.sql.Statement, java.sql.PreparedStatement or java.sql.CallableStatement
+     * @param proxy
+     * @param method
+     * @param args
+     * @param statement
+     * @return
+     */
+    public abstract Object createStatement(Object proxy, Method method, Object[] args, Object statement);
+
+    protected boolean process(String[] names, Method method, boolean process) {
+        for (int i=0; (!process) && i<names.length; i++) {
+            process = (method.getName()==names[i]);
+        }
+        return process;
+    }
+    
+    public void reset(ConnectionPool parent, PooledConnection con) {
+
+    }
+}