开发者

C3P0 connection pool tester is not called

开发者 https://www.devze.com 2023-02-05 13:52 出处:网络
I tried to check if connect is up or down to MySQL using C3P0 connection pool tester. It works if password is correct. But password can be changed and I need to let user know that there is no connecti

I tried to check if connect is up or down to MySQL using C3P0 connection pool tester. It works if password is correct. But password can be changed and I need to let user know that there is no connection to MySQL.

I 开发者_如何学运维want to understand why buildSessionFactory() does not start connection tester.

I used Hibernate+C3P0.

Here is a my config:

setProperty("hibernate.connection.driver_class",
"com.mysql.jdbc.Driver");
setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
setProperty("hibernate.connection.url", "jdbc:mysql://localhost:"
+ getDBPort() + "/emplatcore");
setProperty("hibernate.connection.username", Network.getInstance()
.getMASDBAccountCred().getUserName());
setProperty("hibernate.connection.password", Network.getInstance()
.getMASDBAccountCred().getPassword());

setProperty("hibernate.show_sql", "false");

setProperty("hibernate.c3p0.aquire_increment",
Integer.toString(getConnectionAcquireIncrement()));
setProperty("hibernate.c3p0.idle_test_period",
Integer.toString(getIdleTestPeriod()));
setProperty("hibernate.c3p0.timeout",
Integer.toString(getIdleTimeout()));
setProperty("hibernate.c3p0.max_size",
Integer.toString(getMaxConnections()));
setProperty("hibernate.c3p0.max_statements",
Integer.toString(getStatementCache()));
setProperty("hibernate.c3p0.min_size",
Integer.toString(getInitialPoolSize()));
setProperty(
"c3p0.connectionTesterClassName",
"com.nortelnetworks.mcp.ne.mediaserver.db.dao.hibernate.c3p0.MCPHibernateConnectionTester");
setProperty("c3p0.acquireRetryAttempts",
Integer.toString(getAcquireRetryAttempts()));
setProperty("c3p0.acquireRetryDelay",
Long.toString(getAcquireRetryDelay()));

Settings:

<parm name="ConnectionAcquireIncrement" default="2" />
<parm name="IdleTestPeriod" default="30"/>
<parm name="IdleTimeOut" default="0" />
<parm name="MaxConnections" default="10"/>
<parm name="StatementCache" default="5" />
<parm name="InitialPoolSize" default="2" />
<parm default="5" name="AcquireRetryAttempts" />
<parm default="5000" name="AcquireRetryDelay" />
<parm default="3306" name="Port" />

There is no exception in my code. What I am trying to say is that it for some reason MySQL DB is down then buildSessionFactory() is not failed. As I read on forum this is correct scenario. But I created a C3P0 connection tester which should run every timeout to check the connection status and let me know if the connection to DB is established or not. But in this case tester is not called for some reason. I want to understand why it happens.

Documentation says nothiing about this situation. If this is a bug and the tester should be started then the issue should be fixed.

This is what I am talking about.


I don't know if this helps but I noticed that you have specified the parameter IdleTestPeriod while according to the C3P0 manual it should be idleConnectionTestPeriod. Check also the section about hibernate configuration. So according to the manual:

If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.

and the default value is 0 - so if not set properly, the tester will not be invoked. Hope this helps.


I had the same issue and in my case gradle was missing the hibernate c3p0 dependency. Add following line to build.gradle did the trick.

compile('org.hibernate:hibernate-c3p0:5.2.9.Final')
0

精彩评论

暂无评论...
验证码 换一张
取 消