mysql c3p0 参数_C3p0的参数设置

hibernate默认使用自带的连接池

[org.hibernate.connection.DriverManagerConnectionProvider]-[INFO]

Using Hibernate built-in connection pool (not for production use!)

hibernate使用c3p0的连接池

[com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource]-[INFO]

Initializing c3p0 pool...

c3p0数据库连接池自动重连的配置(2008-07-07 15:01:40)

标签:跟着火炬看中国 c3p0 重新连接 连接池 it      分类:Notes

在Tomcat中配置c3p0数据库连接池的时候,如果数据库重启,或者网络原因造成服务器和数据库断开连接,Tomcat便再也不能和数据库连接,

除非Tomcat服务重启。

解决办法是在c3p0的配置中增加两个参数

testConnectionOnCheckin

true

idleConnectionTestPeriod

60

这样配置之后,连接池每隔60秒自动检测数据库连接情况,如果断开则自动重连。

c3p0连接mysql 数据库(解决掉线问题)

2009年06月24日 星期三 下午 01:48

最近项目用到了hibernate3.0   c3po mysql的数据层组合,开发部署非常顺利,但每天早上访问应用都抛出 Could not open Hibernate

session for transaction 异常,Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to

underlying exception,但经过几次访问后又恢复正常,datasource配置如下:

com.mysql.jdbc.Driver

root

zznode

3

20

50

6000000

根据问题现象初步认为是数据库连接出现问题,逐用异常作为关键词google(这是我遇见不能解决的问题时惯用的方法);得到了相关信息:1

、mysql 数据库链接默认的超时时长为28800秒,2、c3p0 的几个参数意思;

通过以上信息的收集隐隐知道了问题所在(mysql经过28800秒也就是8个小时后关掉空闲链接,而c3p0是经过6000000妙才断开链接,就

有可能出现c3po保持的连接有可能已经被mysql关掉了,自然就出现了hibernate不能打开session,并且都是第二天一早就出错);为了验证我

的想法,决定在开发机上重现这个错误;首先在mysql配置文件my.ini 加上 wait_timeout=30 让mysql经过30妙就关掉链接,重启应用,第一

次访问成功,等待一分钟后访问果然出现同样的错误,说明问题诊断正确,着手修改配置如下:

28000

28000

配置解释:28000<28800 使c3p0 在mysql关不连接之前关闭自己持有的链接,配置idleConnectionTestPeriod 参数使c3po每隔28000检查已有

的连接是否可用,这样应该确保拿到的连接都是可用的,如果还不放心可以加 上 testConnectionOnCheckout参数每当拿出连接的时候就检查

一下是否可以,这个可能会使mysql有一定的性能牺牲;

true

通过以上修改检测一段时间没有出现同样问题,问题解决!总结解决问题的三个步骤:收集相关信息、重现问题、解决测试

让Hibernate自动重新连接数据库——使用c3p0连接池

June 7, 2009 | tags java  hibernate  s2sh  数据库   | views

Comments 0

Hibernate没有自动重新连接数据库,原因很可能是因为你使用了Hibernate内置的连接池,这个连接池不会自动重新连接。使用Mysql

时,默认过8小时没有数据交换,Mysql就会单方面断开数据库连接,所以有些时候你会发现过了8小时(或者一晚上)再访问网站,程序就会抛

出数据库链接错误,而重启服务器容器(Tomcat等)之后程序又恢复正常。

细心的你应该会发现,如果使用默认的连接池,在Hibernate的日志记录(INFO级别)中会提示:Using Hibernate built-in

connection pool (not for production use!)(“你现在使用的是Hibernate内置的连接池,请不要在产品中使用它!”)如果你不听劝告,

仍然使用它,就会出现上面所说的数据库单方面断开连接而无法访问的问题。

解决此方法就是使用其他的连接池,比如此c3p0,在Hibernate官网的参考手册(reference)的3.3节中有简单的介绍①:

Hibernate's own connection pooling algorithm is however quite rudimentary. It is intended to help you get started and is

not intended for use in a production system or even for performance testing. You should use a third party pool for best

performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings.

This will turn off Hibernate's internal pool. For example, you might like to use C3P0.

C3P0 is an open source JDBC connection pool distributed along with Hibernate in the lib directory. Hibernate will use its

org.hibernate.connection.C3P0ConnectionProvider for connection pooling if you set hibernate.c3p0.* properties. If you'd like

to use Proxool refer to the packaged hibernate.properties and the Hibernate web site for more information.

Here is an example hibernate.properties file for C3P0:

hibernate.connection.driver_class = org.postgresql.Driverhibernate.connection.url =

jdbc:postgresql://localhost/mydatabasehibernate.connection.username = myuserhibernate.connection.password =

secrethibernate.c3p0.min_size=5hibernate.c3p0.max_size=20hibernate.c3p0.timeout=1800hibernate.c3p0.max_statements=50hibernate

.dialect = org.hibernate.dialect.PostgreSQLDialect

到这里还没结束,如果你按上面的方法添加了,不一定可以成功使用c3p0连接池(至少我测试时没有成功),你还需要添加一句话:

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider

官方参考文档指出connection.provider_class属性是在自定义连接提供者时使用的,并没有说使用c3p0时也要加这句话。但我测试时不加这句

话就没有效果,所以还是加上吧!

完整的设置方法:

(属性文件)

# Database connection settings

hibernate.connection.driver_class=com.mysql.jdbc.Driver

hibernate.connection.url=jdbc:mysql://localhost:3306/mydb

hibernate.connection.username=username

hibernate.connection.password=password

# configuration pool

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider

hibernate.c3p0.min_size=5

hibernate.c3p0.max_size=15

hibernate.c3p0.timeout=3600

hibernate.c3p0.max_statements=50

# SQL dialect

hibernate.dialect=org.hibernate.dialect.MySQLDialect

# Enable Hibernate's automatic session context management

hibernate.current_session_context_class=thread

# Echo all executed SQL to stdout

hibernate.show_sql=true

# Drop and re-create the database schema on startup

hibernate.hbm2ddl.auto=update

如果你想把properties放到xml中,使用hibernate.c3p0.min_size等4项,(而不是c3p0.min_size)不然可能会扔出警告。

5

15

3600

50

关于更多c3p0的用法请你自己到网上搜索吧!这里不再重述。

org.hibernate.connection.C3P0ConnectionProvider

20

5

5000

0

5

3000

60

true

开始报错如下,需更换c3p0包 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:261) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) at test.UserTest01.save(UserTest01.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:197) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:120) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:55) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:105) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251) ... 34 more Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.connection.C3P0ConnectionProvider] as strategy [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:194) ... 38 more
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值