-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: c3p0 connection pooling does nothing
PostPosted: Wed Feb 13, 2008 10:57 am 
Newbie

Joined: Wed Feb 13, 2008 10:35 am
Posts: 7
Hi everyone,
I'm having trouble setting up c3p0 connection pooling with Hibernate and Spring. I followed the documentation I found on the net, but it looks like I'm doing something wrong.

When I watch Hibernates output, it seems that no connection pooling is used but a new JDBC connection is acquired every time:

Code:
...
<2008-02-13 15:22:55,281> Opening Hibernate Session
<2008-02-13 15:22:55,281> opened session at timestamp: 4927129908350976
<2008-02-13 15:22:55,281> Registering Spring transaction synchronization for new Hibernate Session
<2008-02-13 15:22:55,281> Bound value [org.springframework.orm.hibernate3.SessionHolder@e72f0c] for key [org.hibernate.impl.SessionFactoryImpl@1c1cfc1] to thread [QuartzScheduler_Worker-4]
<2008-02-13 15:22:55,281> Retrieved value [org.springframework.orm.hibernate3.SessionHolder@e72f0c] for key [org.hibernate.impl.SessionFactoryImpl@1c1cfc1] bound to thread [QuartzScheduler_Worker-4]
<2008-02-13 15:22:55,312> about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
<2008-02-13 15:22:55,312> opening JDBC connection
<2008-02-13 15:22:55,312> Creating new JDBC Connection to [jdbc:oracle:thin:@xyz.de:1521:MIFID1]
<2008-02-13 15:22:55,531>
    select
        this_.STATE as STATE130_0_,
        this_.PRICE as PRICE130_0_,
        this_.TRADEDATE as TRADEDATE130_0_,
    from
        T_TRADE this_
    where
        this_.STATE=?
    order by
        this_.TRADEDATE desc for update
            of this_.TECHKEY
<2008-02-13 15:22:55,531> preparing statement
<2008-02-13 15:22:55,703> about to open ResultSet (open ResultSets: 0, globally: 0)
<2008-02-13 15:22:55,703> processing result set
<2008-02-13 15:22:55,703> done processing result set (0 rows)
<2008-02-13 15:22:55,703> about to close ResultSet (open ResultSets: 1, globally: 1)
<2008-02-13 15:22:55,703> about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
<2008-02-13 15:22:55,703> closing statement
<2008-02-13 15:22:55,703> total objects hydrated: 0
<2008-02-13 15:22:55,703> initializing non-lazy collections
<2008-02-13 15:22:55,703> after autocommit
<2008-02-13 15:22:55,703> transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
<2008-02-13 15:22:55,703> after transaction completion




I put c3p0-0.9.0.jar into classpath and configured data source, Hibernate session factory and pooling in spring (not using hibernate.cfg.xml):

Code:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"
      default-lazy-init="true">

   <!--
      ****************************************************************************************************
      * Data Source
      ****************************************************************************************************
   -->
   <bean id="appDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="prototype">
      <description>The data source</description> 
      <property name="driverClassName"><value>datasource</value></property>
      <property name="url"><value>url_hidden</value></property>
      <property name="username"><value>db user hidden</value></property>
      <property name="password"><value>password</value></property>
   </bean> 

   <!--
      ****************************************************************************************************
      * Hibernate session factory
      ****************************************************************************************************
   -->
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="prototype">
      
      <property name="dataSource" ref="appDataSource"/>
      
      <!-- the mapped resources -->
      <property name="mappingResources">
         <list>
            <value>hibernate/Trade.hbm.xml</value>
         </list>
      </property>
      
      <property name="hibernateProperties">
         <props>
         
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>           
             
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
           
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>

         <!-- Connect Pool Provider -->
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider"</prop> 
            <prop key="hibernate.c3p0.acquire_increment">3</prop> 
            <prop key="hibernate.c3p0.idle_test_period">300</prop> 
            <prop key="hibernate.c3p0.initialPoolSize">10</prop> 
            <prop key="hibernate.c3p0.min_size">15</prop> 
            <prop key="hibernate.c3p0.max_size">50</prop> 
            <prop key="hibernate.c3p0.timeout">1800</prop>
            
            </props>
        </property>
   </bean>
   
   <!--
      ****************************************************************************************************
      * Spring transaction manager
      ****************************************************************************************************
   -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" scope="prototype">
      <property name="sessionFactory">
         <ref bean="sessionFactory"/>
      </property>
   </bean>
   
   <bean id="tradeDAO" class="xyz.TradeDAOHibernate">
      <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
</beans>




Hibernate version: 3.1.2

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: Oracle 10G

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html
Code:
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 11:37 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
scope="prototype" means a completely new object is created whenever that bean is accessed from spring.


Code:
  <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="prototype">
       
      <property name="dataSource" ref="appDataSource"/>



your session factory is pointing towards that regular datasource type. I don't know if "connection.provider_class property" thingy overrides or uses that dataSource, or, is itself overridden, thus explaining your behavior.

We configure ours in a different manner so I'm unaware of how exactly that works.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 11:49 am 
Newbie

Joined: Wed Feb 13, 2008 10:35 am
Posts: 7
Thanks Chris, I'll change scope to singleton. How do you configure c3p0?

Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 11:56 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
manually

Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref bean="dataSource"/></property>

...

</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   
    <!-- Connection Parameters -->
    <property name="driverClass"><value>${jdbc.driver}</value></property>

...

  </bean>




_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 12:54 pm 
Newbie

Joined: Wed Feb 13, 2008 10:35 am
Posts: 7
Hi Chris,
I changed te data source definition to a c3p0 pooled data source as in your example and removed the redundant hibernate properties. Now everything works fine and connections are retrieved from c3p0!

Code:
<2008-02-13 17:51:28,500> Using transaction object [org.springframework.orm.hibernate3.HibernateTransactionManager$HibernateTransactionObject@590510]
<2008-02-13 17:51:28,500> Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
<2008-02-13 17:51:28,500> opened session at timestamp: 4927166416896000
<2008-02-13 17:51:28,500> Opened new Session [org.hibernate.impl.SessionImpl@5b6d00] for Hibernate transaction
<2008-02-13 17:51:28,500> Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@5b6d00]
<2008-02-13 17:51:28,500> begin
<2008-02-13 17:51:28,500> opening JDBC connection
<2008-02-13 17:51:28,500> resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@8ddb93 ---> age: 34782   max: 3600000 [com.mchange.v2.resourcepool.BasicResourcePool@1d8d237]
<2008-02-13 17:51:28,500> trace com.mchange.v2.resourcepool.BasicResourcePool@1d8d237 [managed: 10, unused: 9, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1041876)
<2008-02-13 17:51:28,500> current autocommit status: true
<2008-02-13 17:51:28,500> disabling autocommit
<2008-02-13 17:51:28,500> after transaction begin
<2008-02-13 17:51:28,500> Exposing Hibernate transaction as JDBC transaction [com.mchange.v2.c3p0.impl.NewProxyConnection@3a5a9c]
<2008-02-13 17:51:28,515> Bound value [org.springframework.jdbc.datasource.ConnectionHolder@112d7ae] for key [com.mchange.v2.c3p0.ComboPooledDataSource@7f58ef[ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, description -> null, driverClass -> oracle.jdbc.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 7f58ef, idleConnectionTestPeriod -> 300, initialPoolSize -> 10, jdbcUrl -> jdbc:oracle:thin:@xyz:1521:SID, loginTimeout -> 0, maxIdleTime -> 3600, maxPoolSize -> 50, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ]] to thread [QuartzScheduler_Worker-4]
<2008-02-13 17:51:28,515> Bound value [org.springframework.orm.hibernate3.SessionHolder@a8a314] for key [org.hibernate.impl.SessionFactoryImpl@1e8fa70] to thread [QuartzScheduler_Worker-4]
<2008-02-13 17:51:28,515> Initializing transaction synchronization



Thank you a lot!
Sebastian


Top
 Profile  
 
 Post subject: Solution found
PostPosted: Mon Feb 25, 2008 5:07 pm 
Newbie

Joined: Wed Feb 13, 2008 10:35 am
Posts: 7
I'll post this for everyone who has the same problem. The reason why

Code:
<property name="hibernateProperties">
         <props>
         <!-- Connect Pool Provider -->
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider"</prop>
...


Does not work to setup c3p0 pooling is that I specified a datasource in my sessionFactory bean. In this case, Hibernate ignores the setting.

So the solution was to completely get rid of the dataSource and configure averything thorugh the "hibernateProperties" property:

Code:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"
      default-lazy-init="true">


   <!--
      ****************************************************************************************************
      * Hibernate session factory
      ****************************************************************************************************
   -->
   
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
      scope="singleton">
      
      <description>The session factory needed to create the (Hibernate)session.
         The session in Hibernate is more or less a connection to the database
         including some additional features.
      </description>

      <!-- the mapped resources -->
      <property name="mappingResources">
         <!-- List *.hbm.xml here -->
         <list>
            <value>hibernate/Trade.hbm.xml</value>
         </list>
      </property>
      
      <property name="hibernateProperties">
         <map>
            <!-- Cache properies -->
            <entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
            <entry key="hibernate.cache.use_query_cache" value="true"/>
            
            <!-- DB connection properties -->
            <entry key="hibernate.connection.driver_class" value="some Driver Class"/>
            <entry key="hibernate.connection.url" value="some DB URL"/>
            <entry key="hibernate.connection.username" value="some DB username"/>
            <entry key="hibernate.connection.password" value="some DB password"/>
            
            <!-- Query properties -->
            <entry key="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
            <entry key="hibernate.show_sql" value="false"/>
            <entry key="hibernate.format_sql" value="true"/>
            
            <!-- C3P0 - Connection Pool Provider -->   
            <entry key="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
            <entry key="hibernate.c3p0.min_size" value="5"/>
            <entry key="hibernate.c3p0.max_size" value="20"/>
            <entry key="hibernate.c3p0.acquire_increment" value="3"/>
            <entry key="hibernate.c3p0.timeout" value="1800"/>
            <entry key="hibernate.c3p0.idle_test_period" value="300"/>
            <entry key="hibernate.c3p0.num_helper_threads" value="3"/>
            <entry key="hibernate.c3p0.max_statements" value="0"/>
         </map>
        </property>
   </bean>
   
   <!--
      ****************************************************************************************************
      * Spring transaction manager
      ****************************************************************************************************
   -->
   
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
      scope="singleton">
      
      <property name="sessionFactory">
         <ref bean="sessionFactory"/>
      </property>
   </bean>


   <bean id="abstractDAO" abstract="true">
       <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
   

   <bean id="tradeDAO"
      parent="abstractDAO"
      scope="singleton"
      class="xyz.TradeDAO">

       <property name="sessionFactory" ref="sessionFactory"/>
        </bean>

</beans>


Note: You'd probably want to specify the "hibernateProperties" through the easier <props><prop></prop>...</props> format. Both do the same thing. Also don't forget to put the c3po jar file in the classpath!

To prove that c3p0 is actually doing something configure your log4j logger like this:

Code:
log4j.logger.com.mchange.v2.resourcepool.BasicResourcePool=DEBUG


Now you should see the connection pool usage in your logs.


Good luck,
Sebastian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.