-->
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.  [ 10 posts ] 
Author Message
 Post subject: MySQL database timeout issue (proposed solution)
PostPosted: Mon Sep 08, 2003 10:21 am 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
An issue I've seen much discussed recently and that I'm contending with is the default 8 hour timeout for stale database connections in MySQL. When MySQL drops the connections, the default connection pool and DBCP don't recover, and so you have to restart the context to get things working again. C3P0 is better in that rather than just dying, it catches the failure and then grabs a new connection, but the first request still fails.

One proposed solution is adding autoReconnect=true to your JDBC URL, but this only works if you have auto commit turned on (I verified this by looking at the source for the MySQL driver). In my situation, that's not appropriate.

I've done some testing with C3P0 by itself, and found that one of the configuration properties, maxIdleTime, provides the perfect solution to this problem. In the JavaDoc, it's described as follows:

Quote:
Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.


In my testing, this works exactly as expected. As long as you set the maxIdleTime for your pool to a value lower than the interactive_timeout and wait_timeout settings in MySQL, you'll never have the stale connection problem. Unfortunately, there's no easy way to set this value in the Hibernate context.

Hibernate configures C3P0 by copying Hibernate properties to the C3P0 PoolConfig object when it's instantiated. maxIdleTime isn't one of the properties that Hibernate supports. Under ordinary circumstances, C3P0 will read property values from a file called c3p0.properties in the classpath, but Hibernate instantiates the pool in such a way that this step is skipped.

What I'm wondering is the best way to set this property in my application, which is a Struts application running under Tomcat 4.1.x.

As I see it, there are three ways to make this work (theoretically).

The first is to just hack my c3p0.jar so that it has the value set to what I want. I don't like this approach because I don't want to have a self-hacked c3p0.jar out there.

The second is to hack Hibernate so that it passes through the maxIdleTime property to the PoolConfig when it instantiates a C3P0 pool. If I were to do this, I'd submit the patch back to the project so that it can be included in the future. This seems like a good solution to me if there's interest in adding this property to the set of configurable properties for C3P0.

The third is to not use C3P0 by configuring it using my Hibernate config but rather set it up using JNDI so that I can configure it in any way that I choose. I've been trying to do this but I haven't had any luck getting it to work. Has anyone successfully used Tomcat's JNDI provider to furnish DataSource objects for Hibernate? I tried using C3P0 and also the DBCP support built into Tomcat for this and haven't been successful.

Any advice on where to go next would be appreciated.

Thanks,
Rafe


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 10:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The property is called:

hibernate.c3p0.timeout


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 11:04 am 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
How in the world did I miss that?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 6:08 am 
Newbie

Joined: Fri Sep 12, 2003 5:17 am
Posts: 3
Yes I have configured a JNDI Datasource whithin the Application context and I use it with Hibernate.
HEre is the server.xml configuration :

<!-- My Application COnfig -->
<Context path="/myApp" docBase="D:\www" debug="0" reloadable="true">
<Resource name="jdbc/DS" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/SS">
<parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>
<parameter><name>username</name><value>login</value></parameter>
<parameter><name>password</name><value>password</value></parameter>
<parameter><name>driverClassName</name><value>com.mysql.jdbc.Driver</value></parameter>
<parameter><name>url</name><value>jdbc:mysql://localhost/MYBASE?autoReconnect=true</value></parameter>
<parameter><name>maxActive</name><value>20</value></parameter>
<parameter><name>maxIdle</name><value>2</value></parameter>
</ResourceParams>

</Context>


An the hibernate.properties file :
hibernate.connection.datasource=java:comp/env/jdbc/DS

hibernate.dialect = cirrus.hibernate.sql.MySQLDialect
hibernate.show_sql = false
It works well until now !


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2003 5:12 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
think this would work with DBCP as well?

I'm having trouble with Hibernate 1.2.

Code:
hibernate.dbcp.maxIdle = 21600

(6 hours)

would this help?

I'll test it, but perhaps someone has an answer before 8 hours have passed. :-)

WILL


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2003 5:20 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
umm... never mind. maxIdle's the wrong property, it means something different for DBCP.

Anyone have a suggestion for a way to do this with DBCP with Hibernate 1.2? I've tried setting a validation query (works with Hibernate 2) but apparently not with Hibernate 1.2

WILL


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2003 9:27 am 
Newbie

Joined: Fri Sep 12, 2003 5:17 am
Posts: 3
Yes I make my pool works under Tomcat and DBCP with Hibernate 1.2.

It works perfectly.

You could use those dbcp properties :
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>

When available db connections run low DBCP will recover and recyle any abandoned dB connections it finds. The default is false.

Use the removeAbandonedTimeout parameter to set the number of seconds a dB connection has been idle before it is considered abandoned.

<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>

Just try it ! and tell me.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 3:52 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
rafeco, could you post your mysql and c3p0 settings? I'm having the same problem, even using the c3p0 timeout, and I'd like to see some settings that someone has gotten to work. :)

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2003 10:13 am 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Bueller? Bueller? Anyone? :P


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 5:57 pm 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
Ack, sorry I'm so late on this. Here are the settings that worked for me:

Code:
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.driver=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/foo
hibernate.connection.username=foo
hibernate.connection.password=bar
hibernate.show_sql=false
hibernate.use_outer_join=true
hibernate.transaction.factory_class=net.sf.hibernate.transaction.JDBCTransactionFactory
hibernate.c3p0.max_size=100
hibernate.c3p0.min_size=3
hibernate.c3p0.timeout=900
hibernate.c3p0.max_statements=100
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.