From 6fbff1f373c18e334ba3de61902a966323095db4 Mon Sep 17 00:00:00 2001 From: fhanik Date: Mon, 24 Nov 2008 18:47:49 +0000 Subject: [PATCH] document how interceptors work and how to configure them git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@720253 13f79535-47bb-0310-9956-ffa450edef68 --- modules/jdbc-pool/doc/jdbc-pool.xml | 59 ++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/modules/jdbc-pool/doc/jdbc-pool.xml b/modules/jdbc-pool/doc/jdbc-pool.xml index 789aa330b..cf44d53e7 100644 --- a/modules/jdbc-pool/doc/jdbc-pool.xml +++ b/modules/jdbc-pool/doc/jdbc-pool.xml @@ -300,7 +300,11 @@

(String) A semicolon separated list of classnames extending org.apache.tomcat.jdbc.pool.JdbcInterceptor class. These interceptors will be inserted as an interceptor into the chain of operations on a java.sql.Connection object. - The default value is null.

+ The default value is null.
+ Predefined interceptors:
+ org.apache.tomcat.jdbc.pool.interceptor.ConnectionState - keeps track of auto commit, read only, catalog and transaction isolation level.
+ org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer - keeps track of opened statements, and closes them when the connection is returned to the pool.
+

@@ -373,6 +377,7 @@ p.setMinIdle(10); p.setLogAbandoned(true); p.setRemoveAbandoned(true); + p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"); DataSource datasource = new DataSource(); datasource.setPoolProperties(p); @@ -417,6 +422,7 @@ logAbandoned="true" minEvictableIdleTimeMillis="30000" jmxEnabled="true" + jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" username="root" password="password" driverClassName="com.mysql.jdbc.Driver" @@ -426,6 +432,57 @@ + +

Interceptors are a powerful way to enable, disable or modify functionality on a specific connection or its sub components. + There are many different use cases for when interceptors are useful. By default, and for performance reasons, the connection pool is stateless. + The only state the pool itself inserts are defaultAutoCommit, defaultReadOnly, defaultTransactionIsolation, defaultCatalog if + these are set. These 4 properties are only set upon connection creation. Should these properties be modified during the usage of the connection, + the pool itself will not reset them.

+

An interceptor has to extend the org.apache.tomcat.jdbc.pool.JdbcInterceptor class. This class is fairly simple, + You will need to have a no arg constructor

+ + + public JdbcInterceptor() { + } + +

+ When a connection is borrowed from the pool, the interceptor can initialize or in some other way react to the event by implementing the + + + public abstract void reset(ConnectionPool parent, PooledConnection con); + + method. This method gets called with two parameters, a reference to the connection pool itself ConnectionPool parent + and a reference to the underlying connection PooledConnection con. +

+

+ When a method on the java.sql.Connection object is invoked, it will cause the + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable + + method to get invoked. The Method method is the actual method invoked, and Object[] args are the arguments. + To look at a very simple example, where we demonstrate how to make the invokation to java.sql.Connection.close() a noop + if the connection has been closed + + + if (CLOSE_VAL==method.getName()) { + if (isClosed()) return null; //noop for already closed. + } + return super.invoke(proxy,method,args); + + There is an observation being made. It is the comparison of the method name. One way to do this would be to do + "close".equals(method.getName()). + Above we see a direct reference comparison between the method name and static final String reference. + According to the JVM spec, method names and static final String end up in a shared constant pool, so the reference comparison should work. +

+

Configuring interceptors
+ Interceptors are configured using the jdbcInterceptors property or the setJdbcInterceptors method. + An interceptor can have properties, and would be configured like this + + + String jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState(useEquals=true,fast=yes)" + +

+
-- 2.11.0