hibernate 有自带的连接池,但大家用起来颇有微词,因为其稳定性以及性能都不太好。c3p0 连接池的性能和稳定性久经考验,所以用 hibernate 的朋友一般都使用 c3p0 的连接池。那么是不是把 c3p0 的包导进来,然后 hibernate.cfg.xml 里把 c3p0 的各种属性加进来就万事大吉了呢?不见得,很可能你的项目上线以后,发现你的连接池不仅性能低下,而且可靠性差,c3p0 并没有表现出它传说中应该具有的那些特性。你在咒骂 c3p0 的可靠性以及性能的时候,有没有想过你用的很可能还是 hibernate 默认的连接池,因为你的 c3p0 的那些配置压根儿就没起作用呢?你有没有考虑过,怎么样才能让你配置的 hibernate 中 c3p0 配置真正起作用?怎样验证你的 c3p0 配置真正起了作用了?本文结合作者亲自经历的一次 java.net.SocketException: Connection reset 故障的排除体验,来回答一下这些问题。
本文所用 mysql 版本是 5.0, hibernate 版本是 3.3.2,c3p0 版本是 0.9.1.1:
笔者的项目 dao 层组合是 hibernate+c3p0+mysql,hibernate.cfg.xml 中 c3p0 连接池配置如下:
上线以后,发现偶尔会有以下错误:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(
本文所用 mysql 版本是 5.0, hibernate 版本是 3.3.2,c3p0 版本是 0.9.1.1:
笔者的项目 dao 层组合是 hibernate+c3p0+mysql,hibernate.cfg.xml 中 c3p0 连接池配置如下:
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.checkoutTimeout">15000</property>
<property name="hibernate.c3p0.max_statements">200</property>
<property name="hibernate.c3p0.idle_test_period">0</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.timeout">0</property>
上线以后,发现偶尔会有以下错误:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(