Hibernate/MySQL Connection Timeout

可紊 提交于 2019-11-29 07:32:59

The jars required for C3P0 configuration are c3p0-0.9.2-pre2.jar & mchange-commons-java-0.2.1.jar. Also, additionally you need to put c3p0.properties in classpath.

Below are the properties, which should be configured while using C3P0 with Hibernate.

hibernate.cfg.xml

<property name="connection.provider_class">
                org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">1</property> 
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">100</property> 
<property name="hibernate.c3p0.max_statements">100</property> 
<property name="hibernate.c3p0.min_size">10</property> 
<property name="hibernate.c3p0.timeout">180</property>

c3p0.properties

  • You can validate connection on each checkout c3p0.testConnectionOnCheckout=true, but this is expensive operation.

  • Else, you can retry to establish connection periodically.

    c3p0.acquireRetryAttempts = 4
    c3p0.acquireRetryDelay = 5000

    This will retry 4 times with a delay of 5 seconds between each consecutive attempt.

c3p0 has no dependency on SLF4J. If adding that library to your classpath fixed your issue, that is interesting, but not so easy to explain.

A stack trace of the original NPE tomcat logged when you had problems would be helpful. (I'm c3p0's developer.)

Note that c3p0.properties needs to be at the top level of the CLASSPATH of your app, which may not be where other config files are located. c3p0.properties is loaded as a ClassLoader resource.

The bug can be fixed using commercial pooling libraries as suggested above. I used c3p0 exactly as suggested by @NayanWadekar. Please note that c3p0 is dependent on SLF4J. So after including the c3p0 jars in your classpath, you also have to add SLF4J jars in your classpath. The trick about this thing is that, if those SLF4J jars are not present, the compiler still won't complain, you only get errors after deploying the app. Also, if you are using Netbeans, avoid creating an SLF4J library from all the jars bundled together here at slf4.org. The reason being that some of the jars are not meant to be used together in the same project. Just use the following two jars alone from the library: slf4j-api-1.6.4.jar and slf4j-jdk14-1.6.4.jar. You can visit www.slf4j.org for details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!