Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 33 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: c3po and Hibernate
PostPosted: Mon Aug 15, 2005 9:50 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
Hi, i want to use c3po with hibernate. Here is my configuration:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ibn</property>
<property name="hibernate.connection.username">xxx</property>
<property name="hibernate.connection.password">xxx</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.isolation">1</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.c3p0.initialPoolSize">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">150</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">300</property>
<property name="hibernate.c3po.idle_test_period">3000</property>
<property name="hibernate.c3po.maxIdleTime">30</property>
<property name="hibernate.c3po.propertyCycle">300</property>
<property name="hibernate.c3po.acquireIncrement">3</property>
<property name="hibernate.c3po.testConnectionOnCheckout">false</property>
<property name="hibernate.c3po.autoCommitOnClose">false</property>
<property name="hibernate.c3po.numHelperThreads">10</property>

<property name="hibernate.connection.useCompression">true</property>
<property name="hibernate.connection.cacheResultSetMetadata">true</property>
<property name="hibernate.connection.dontTrackOpenResources">true</property>
<property name="hibernate.connection.dynamicCalendars">true</property>
<property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
<property name="hibernate.connection.rollbackOnPooledClose">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>

In another forum i was told, that with this c3po is never used but the hibernate connection-pool. What do i have to change to use c3po??

Any help would be much appreciated.

Thanks
Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:01 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
hibernate.properties
Code:
hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider

resp.

hibernate.cfg.xml
Code:
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>


By the way, if you're not sure, whether c3p0 is used or not, have a look at your logfile. It tells you which connection pool is used.

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:31 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Wrong, setting any c3p0.* property is enough to enable the right connection provider. Use your log.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:31 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
Thanks for your answer. My logfile tells me the following:

Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@1def3f5 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@165cebb [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, maxIdleTime -> 5000, maxPoolSize -> 150, maxStatements -> 300, maxStatementsPerConnection -> 0, minPoolSize -> 10, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@1204425 [ description -> null, driverClass -> null, factoryClassLocation -> null, jdbcUrl -> jdbc:mysql://localhost:3306/ibn, properties -> {dontTrackOpenResources=true, cacheResultSetMetadata=true, user=******, password=******, rollbackOnPooledClose=true, dynamicCalendars=true, zeroDateTimeBehavior=convertToNull, useCompression=true} ] , preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ] , factoryClassLocation -> null, numHelperThreads -> 3, poolOwnerIdentityToken -> 1def3f5 ]

So i assume that c3po is running. BUT the properties that i set in the hibernate-configuration (for example maxIdleTime=30) are not set properly. What´s going on here?

Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:31 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Because they have the wrong names.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:37 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
That is all that is supported:

Code:
         int minPoolSize = PropertiesHelper.getInt(Environment.C3P0_MIN_SIZE, props, 1);
         int maxPoolSize = PropertiesHelper.getInt(Environment.C3P0_MAX_SIZE, props, 100);
         int maxIdleTime = PropertiesHelper.getInt(Environment.C3P0_TIMEOUT, props, 0);
         int maxStatements = PropertiesHelper.getInt(Environment.C3P0_MAX_STATEMENTS, props, 0);
         int acquireIncrement = PropertiesHelper.getInt(Environment.C3P0_ACQUIRE_INCREMENT, props, 1);
         int idleTestPeriod = PropertiesHelper.getInt(Environment.C3P0_IDLE_TEST_PERIOD, props, 0);

         PoolConfig pcfg = new PoolConfig();
         pcfg.setInitialPoolSize(minPoolSize);
         pcfg.setMinPoolSize(minPoolSize);
         pcfg.setMaxPoolSize(maxPoolSize);
         pcfg.setAcquireIncrement(acquireIncrement);
         pcfg.setMaxIdleTime(maxIdleTime);
         pcfg.setMaxStatements(maxStatements);
         pcfg.setIdleConnectionTestPeriod(idleTestPeriod);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:39 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
Sorry, i can´t see what´s wrong with the names. Its "maxIdleTime" in the log and "maxIdleTime" in my configuration. Is there any difference that i don´t see?

Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:42 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The names in hibernate.properties/hibernate.cfg.xml have underscores, and are not camelCase.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 11:02 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
Ok, now i have changed my configuration to the following

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ibn</property>
<property name="hibernate.connection.username">ibn</property>
<property name="hibernate.connection.password">gebit</property>
<property name="hibernate.connection.isolation">1</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.c3p0.initial_pool_size">10</property>
<property name="hibernate.c3p0.min_pool_size">10</property>
<property name="hibernate.c3p0.max_pool_size">150</property>
<property name="hibernate.c3p0.max_statements">300</property>
<property name="hibernate.c3po.idle_connection_test_period">3000</property>
<property name="hibernate.c3po.max_idle_time">30</property>
<property name="hibernate.c3po.acquire_increment">3</property>

<property name="hibernate.connection.use_compression">true</property>
<property name="hibernate.connection.cache_result_set_metadata">true</property>
<property name="hibernate.connection.dont_track_open_resources">true</property>
<property name="hibernate.connection.dynamic_calendars">true</property>
<property name="hibernate.connection.zero_date_time_behavior">convertToNull</property>
<property name="hibernate.connection.rollback_on_pooled_close">true</property>

<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>

<!-- Mappings -->
.....
</session-factory>
</hibernate-configuration>

The result is that i don´t get any message in the log about initilizing c3po anymore. What have i done wrong?

Thanks for your help!!!
Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 11:07 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Dude, just use the hibernate.properties file that came with Hibernate3 in the etc/ directory.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 11:34 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
cheers mate, now my c3po pool is starting with the properties i want it to have.


Top
 Profile  
 
 Post subject: interpretation
PostPosted: Mon Aug 15, 2005 11:46 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Quote:
That is all that is supported:

Why the limitation? Why H does not use reflection to allow setting all the desired parameters of the pool?
It seems trivial to treat hibernate.c3p0 as a prefix, get all the parameters with the prefix, and then use BeanUtils to set properties on the target object...

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 11:49 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
BeanUtils? As long as you can write a new connection provider that supports what you want to do without additional dependencies, you are welcome to submit it to JIRA. Of course it can also not break legacy configuration settings.


Top
 Profile  
 
 Post subject: working code
PostPosted: Mon Aug 15, 2005 12:15 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Adding this code does not break current configuration and allows setting everything else based on reflection (conversion to necessary data type happens automatically too - just tested it):


import org.apache.commons.beanutils.BeanUtils;
....
PoolConfig pcfg = new PoolConfig();
pcfg.setInitialPoolSize(minPoolSize);
pcfg.setMinPoolSize(minPoolSize);
pcfg.setMaxPoolSize(maxPoolSize);
pcfg.setAcquireIncrement(acquireIncrement);
pcfg.setMaxIdleTime(maxIdleTime);
pcfg.setMaxStatements(maxStatements);
pcfg.setIdleConnectionTestPeriod(idleTestPeriod);

//reflection based settings
Enumeration en = props.keys();
String pfx = "hibernate.c3p0.";
while( en.hasMoreElements() ){
String key = (String) en.nextElement();
if( key.startsWith( pfx )){
String setter = key.substring( pfx.length() );
BeanUtils.setProperty( pcfg, setter, props.getProperty( key ) );
}
}



And by the way:
it is necessary to set
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

in order to enable c3p0 pool

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 12:26 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, it is not necessary (I've documented exactly this for the nth time yesterday for HiA second edition). Please submit patches to JIRA.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 33 posts ]  Go to page 1, 2, 3  Next

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.