Author: fhanik
Date: Thu Nov 13 10:02:35 2008
New Revision: 713763
URL: http://svn.apache.org/viewvc?rev=713763&view=rev
Log:
Fix connection state, make it smarter and faster.
Added:
tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestConnectionState.java
Modified:
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/ConnectionState.java
Modified:
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java?rev=713763&r1=713762&r2=713763&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
Thu Nov 13 10:02:35 2008
@@ -129,6 +129,7 @@
(JdbcInterceptor) Class.forName(proxies[i], true,
//should this be the class loader?
Thread.currentThread().getContextClassLoader()).newInstance();
interceptor.setNext(handler);
+ interceptor.reset(this, con); //initialize
handler = interceptor;
}catch(Exception x) {
SQLException sx = new SQLException("Unable to instantiate
interceptor chain.");
Modified:
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java?rev=713763&r1=713762&r2=713763&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java
Thu Nov 13 10:02:35 2008
@@ -172,13 +172,16 @@
return driver.getPool(getPoolProperties().getPoolName()).getSize();
}
- public String toString() {
+ public String toString() {
return super.toString()+"{"+getPoolProperties()+"}";
}
/*-----------------------------------------------------------------------*/
// PROPERTIES WHEN NOT USED WITH FACTORY
/*------------------------------------------------------------------------*/
+
+
+
public void setPoolProperties(PoolProperties poolProperties) {
this.poolProperties = poolProperties;
}
@@ -200,7 +203,7 @@
}
public void setMaxActive(int maxActive) {
- this.poolProperties.setMaxIdle(maxActive);
+ this.poolProperties.setMaxActive(maxActive);
}
public void setMaxIdle(int maxIdle) {
@@ -212,12 +215,11 @@
}
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
- this.poolProperties.setMinEvictableIdleTimeMillis(
- minEvictableIdleTimeMillis);
+
this.poolProperties.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
}
public void setMinIdle(int minIdle) {
- this.setMinIdle(minIdle);
+ this.poolProperties.setMinIdle(minIdle);
}
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
@@ -253,10 +255,8 @@
this.poolProperties.setTestWhileIdle(testWhileIdle);
}
- public void setTimeBetweenEvictionRunsMillis(int
-
timeBetweenEvictionRunsMillis) {
- this.poolProperties.setTimeBetweenEvictionRunsMillis(
- timeBetweenEvictionRunsMillis);
+ public void setTimeBetweenEvictionRunsMillis(int
timeBetweenEvictionRunsMillis) {
+
this.poolProperties.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
}
public void setUrl(String url) {
@@ -283,26 +283,38 @@
public void setJmxEnabled(boolean enabled) {
this.getPoolProperties().setJmxEnabled(enabled);
}
-
+
public void setFairQueue(boolean fairQueue) {
this.getPoolProperties().setFairQueue(fairQueue);
}
+ public void setDefaultCatalog(String catalog) {
+ this.getPoolProperties().setDefaultCatalog(catalog);
+ }
+
+ public void setDefaultAutoCommit(Boolean autocommit) {
+ this.getPoolProperties().setDefaultAutoCommit(autocommit);
+ }
+
+ public void setDefaultTransactionIsolation(int
defaultTransactionIsolation) {
+
this.getPoolProperties().setDefaultTransactionIsolation(defaultTransactionIsolation);
+ }
+
public void setConnectionProperties(String properties) {
try {
- java.util.Properties prop =
DataSourceFactory.getProperties(properties);
+ java.util.Properties prop = DataSourceFactory
+ .getProperties(properties);
Iterator i = prop.keySet().iterator();
while (i.hasNext()) {
- String key = (String)i.next();
+ String key = (String) i.next();
String value = prop.getProperty(key);
getPoolProperties().getDbProperties().setProperty(key, value);
}
-
- }catch (Exception x) {
+
+ } catch (Exception x) {
log.error("Unable to parse connection properties.", x);
throw new RuntimeException(x);
}
}
-
}
Modified:
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java?rev=713763&r1=713762&r2=713763&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
Thu Nov 13 10:02:35 2008
@@ -26,6 +26,7 @@
public abstract class JdbcInterceptor implements InvocationHandler {
public static final String CLOSE_VAL = "close";
public static final String TOSTRING_VAL = "toString";
+ public static final String ISCLOSED_VAL = "isClosed";
private JdbcInterceptor next = null;
@@ -47,6 +48,11 @@
public void setNext(JdbcInterceptor next) {
this.next = next;
}
-
+
+ /**
+ * Gets called each time the connection is borrowed from the pool
+ * @param parent - the connection pool owning the connection
+ * @param con - the pooled connection
+ */
public abstract void reset(ConnectionPool parent, PooledConnection con);
}
Modified:
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java?rev=713763&r1=713762&r2=713763&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java
Thu Nov 13 10:02:35 2008
@@ -68,6 +68,9 @@
}
public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
+ if (ISCLOSED_VAL==method.getName()) {
+ return isClosed();
+ }
if (isClosed()) throw new SQLException("Connection has already been
closed.");
if (CLOSE_VAL==method.getName()) {
PooledConnection poolc = this.connection;
Modified:
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/ConnectionState.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/ConnectionState.java?rev=713763&r1=713762&r2=713763&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/ConnectionState.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/ConnectionState.java
Thu Nov 13 10:02:35 2008
@@ -17,10 +17,14 @@
package org.apache.tomcat.jdbc.pool.interceptor;
import java.lang.reflect.Method;
+import java.sql.SQLException;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.ConnectionPool;
import org.apache.tomcat.jdbc.pool.DataSourceFactory;
import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
+import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.apache.tomcat.jdbc.pool.PooledConnection;
/**
@@ -30,18 +34,63 @@
*/
public class ConnectionState extends JdbcInterceptor {
-
- protected final String[] readState =
{"getAutoCommit","getTransactionIsolation","isReadOnly"};
- protected final String[] writeState =
{"setAutoCommit","setTransactionIsolation","setReadOnly"};
+ protected static Log log = LogFactory.getLog(ConnectionState.class);
+
+ protected final String[] readState =
{"getAutoCommit","getTransactionIsolation","isReadOnly","getCatalog"};
+ protected final String[] writeState =
{"setAutoCommit","setTransactionIsolation","setReadOnly","setCatalog"};
protected Boolean autoCommit = null;
protected Integer transactionIsolation = null;
protected Boolean readOnly = null;
-
+ protected String catalog = null;
+
+
public void reset(ConnectionPool parent, PooledConnection con) {
- autoCommit = null;
- transactionIsolation = null;
- readOnly = null;
+ PoolProperties poolProperties = parent.getPoolProperties();
+ if (poolProperties.getDefaultReadOnly()!=null) {
+ try {
+ if (readOnly==null ||
readOnly.booleanValue()!=poolProperties.getDefaultReadOnly().booleanValue()) {
+
con.getConnection().setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
+ readOnly = poolProperties.getDefaultReadOnly();
+ }
+ }catch (SQLException x) {
+ readOnly = null;
+ log.error("Unable to reset readonly state to connection.",x);
+ }
+ }
+ if (poolProperties.getDefaultAutoCommit()!=null) {
+ try {
+ if (autoCommit==null ||
autoCommit.booleanValue()!=poolProperties.getDefaultAutoCommit().booleanValue())
{
+
con.getConnection().setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
+ autoCommit = poolProperties.getDefaultAutoCommit();
+ }
+ }catch (SQLException x) {
+ autoCommit = null;
+ log.error("Unable to reset autocommit state to connection.",x);
+ }
+ }
+ if (poolProperties.getDefaultCatalog()!=null) {
+ try {
+ if (catalog==null ||
(!catalog.equals(poolProperties.getDefaultCatalog()))) {
+
con.getConnection().setCatalog(poolProperties.getDefaultCatalog());
+ catalog = poolProperties.getDefaultCatalog();
+ }
+ }catch (SQLException x) {
+ catalog = null;
+ log.error("Unable to reset default catalog state to
connection.",x);
+ }
+ }
+ if
(poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION)
{
+ try {
+ if (transactionIsolation==null ||
transactionIsolation.intValue()!=poolProperties.getDefaultTransactionIsolation())
{
+
con.getConnection().setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
+ transactionIsolation =
poolProperties.getDefaultTransactionIsolation();
+ }
+ }catch (SQLException x) {
+ transactionIsolation = null;
+ log.error("Unable to reset transaction isolation state to
connection.",x);
+ }
+ }
}
@Override
@@ -64,6 +113,7 @@
case 0:{result = autoCommit; break;}
case 1:{result = transactionIsolation; break;}
case 2:{result = readOnly; break;}
+ case 3:{result = catalog; break;}
default: result = null;
}
//return cached result, if we have it
@@ -76,6 +126,7 @@
case 0:{autoCommit = (Boolean) (read?result:args[0]); break;}
case 1:{transactionIsolation = (Integer)(read?result:args[0]);
break;}
case 2:{readOnly = (Boolean)(read?result:args[0]); break;}
+ case 3:{catalog = (String)(read?result:args[0]); break;}
}
}
return result;
Added:
tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestConnectionState.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestConnectionState.java?rev=713763&view=auto
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestConnectionState.java
(added)
+++
tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TestConnectionState.java
Thu Nov 13 10:02:35 2008
@@ -0,0 +1,84 @@
+/*
+ * 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.test;
+
+import java.sql.Connection;
+
+import org.apache.tomcat.jdbc.pool.DataSourceProxy;
+import org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
+
+public class TestConnectionState extends DefaultTestCase {
+
+ public TestConnectionState(String name) {
+ super(name);
+ }
+
+ public void testAutoCommitFalse() throws Exception {
+ DataSourceProxy d1 = this.createDefaultDataSource();
+ d1.setMaxActive(1);
+ d1.setMinIdle(1);
+ d1.setMaxIdle(1);
+ d1.setJdbcInterceptors(ConnectionState.class.getName());
+ d1.setDefaultAutoCommit(Boolean.FALSE);
+ Connection c1 = d1.getConnection();
+ assertFalse("Auto commit should be false",c1.getAutoCommit());
+ c1.setAutoCommit(true);
+ assertTrue("Auto commit should be true",c1.getAutoCommit());
+ c1.close();
+ c1 = d1.getConnection();
+ assertFalse("Auto commit should be false for a reused
connection",c1.getAutoCommit());
+ d1.close(true);
+ assertTrue("Connection should be closed",c1.isClosed());
+ }
+
+ public void testAutoCommitTrue() throws Exception {
+ DataSourceProxy d1 = this.createDefaultDataSource();
+ d1.setMaxActive(1);
+ d1.setJdbcInterceptors(ConnectionState.class.getName());
+ d1.setDefaultAutoCommit(Boolean.TRUE);
+ d1.setMinIdle(1);
+ Connection c1 = d1.getConnection();
+ assertTrue("Auto commit should be true",c1.getAutoCommit());
+ c1.setAutoCommit(false);
+ assertFalse("Auto commit should be false",c1.getAutoCommit());
+ c1.close();
+ c1 = d1.getConnection();
+ assertTrue("Auto commit should be true for a reused
connection",c1.getAutoCommit());
+ }
+
+ public void testDefaultCatalog() throws Exception {
+ DataSourceProxy d1 = this.createDefaultDataSource();
+ d1.setMaxActive(1);
+ d1.setJdbcInterceptors(ConnectionState.class.getName());
+ d1.setDefaultCatalog("information_schema");
+ d1.setMinIdle(1);
+ Connection c1 = d1.getConnection();
+ assertEquals("Catalog should be
information_schema",c1.getCatalog(),"information_schema");
+ c1.close();
+ c1 = d1.getConnection();
+ assertEquals("Catalog should be
information_schema",c1.getCatalog(),"information_schema");
+ c1.setCatalog("mysql");
+ assertEquals("Catalog should be
information_schema",c1.getCatalog(),"mysql");
+ c1.close();
+ c1 = d1.getConnection();
+ assertEquals("Catalog should be
information_schema",c1.getCatalog(),"information_schema");
+ }
+
+
+
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]