tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Filip Hanik - Dev Lists <devli...@hanik.com>
Subject Re: ConnectionPool question
Date Fri, 30 Oct 2009 16:12:18 GMT
look at jdbc-pool.html it has all the info, here are examples out of it

Configuration


<Resource name="jdbc/TestDB"
               auth="Container"
               type="javax.sql.DataSource"
               factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
               testWhileIdle="true"
               testOnBorrow="true"
               testOnReturn="false"
               validationQuery="SELECT 1"
               validationInterval="30000"
               timeBetweenEvictionRunsMillis="30000"
               maxActive="100"
               minIdle="10"
               maxWait="10000"
               initialSize="10"
               removeAbandonedTimeout="60"
               removeAbandoned="true"
               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"
               url="jdbc:mysql://localhost:3306/mysql"/>




Code:

import java.sql.Connection;
         import java.sql.ResultSet;
         import java.sql.Statement;

         import org.apache.tomcat.jdbc.pool.DataSource;
         import org.apache.tomcat.jdbc.pool.PoolProperties;

         public class SimplePOJOExample {

             public static void main(String[] args) throws Exception {
                 PoolProperties p = new PoolProperties();
                 p.setUrl("jdbc:mysql://localhost:3306/mysql");
                 p.setDriverClassName("com.mysql.jdbc.Driver");
                 p.setUsername("root");
                 p.setPassword("password");
                 p.setJmxEnabled(true);
                 p.setTestWhileIdle(false);
                 p.setTestOnBorrow(true);
                 p.setValidationQuery("SELECT 1");
                 p.setTestOnReturn(false);
                 p.setValidationInterval(30000);
                 p.setTimeBetweenEvictionRunsMillis(30000);
                 p.setMaxActive(100);
                 p.setInitialSize(10);
                 p.setMaxWait(10000);
                 p.setRemoveAbandonedTimeout(60);
                 p.setMinEvictableIdleTimeMillis(30000);
                 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);

                 Connection con = null;
                 try {
                   con = datasource.getConnection();
                   Statement st = con.createStatement();
                   ResultSet rs = st.executeQuery("select * from user");
                   int cnt = 1;
                   while (rs.next()) {
                       System.out.println((cnt++)+". Host:" 
+rs.getString("Host")+
                         " User:"+rs.getString("User")+" 
Password:"+rs.getString("Password"));
                   }
                   rs.close();
                   st.close();
                 } finally {
                   if (con!=null) try {con.close();}catch (Exception 
ignore) {}
                 }
             }

         }


On 10/30/2009 09:54 AM, Filip Hanik - Dev Lists wrote:
>
> http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/StatementFinalizer.java?view=log

>
>
> Filip
>
> On 10/30/2009 08:34 AM, Josh Gooding wrote:
>> Hey what API holds the statementFinalizer?
>>
>> On Fri, Oct 30, 2009 at 9:57 AM, Josh 
>> Gooding<josh.gooding@gmail.com>wrote:
>>
>>> AHHHHHH, I will read the API for the StatementFinalizer.  I was 
>>> looking at
>>> something to do that.  Thank you Filip!
>>>
>>>
>>> On Fri, Oct 30, 2009 at 9:53 AM, Filip Hanik - Dev Lists<
>>> devlists@hanik.com>  wrote:
>>>
>>>> hi Josh, calling Connection.close() does not close statements and
>>>> resultsets.
>>>> There is an interceptor you can configure called StatementFinalizer 
>>>> that
>>>> does exactly that during the close call.
>>>>
>>>> Filip
>>>>
>>>>
>>>>
>>>> On 10/29/2009 07:17 PM, Josh Gooding wrote:
>>>>
>>>>> I wrote some code on top of the Tomcat's ConnectionPool class.  In
>>>>> regular
>>>>> Java based programming if I close a ResultSet with 
>>>>> connection.close(),
>>>>> this
>>>>> frees up both the statement and resultset's memory associated with 
>>>>> the
>>>>> connection if it was still open.  If I close a connection with 
>>>>> Tomcat's
>>>>> ConnectionPool, does it also close the statement and resultset's
>>>>> associated
>>>>> with that particular connection or do I need to manually close them?
>>>>>
>>>>> I know best practice is to not rely on anything to be closed
>>>>> automatically,
>>>>> but I inherited a code base and I am looking at making some pretty
>>>>> significant changes to fix some problems, and this is one of them.
>>>>>
>>>>> Thanks in advance,
>>>>>
>>>>> - Josh
>>>>>
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message