本文主要内容:
Hibernate 连接池的三种配置方式:dbcp , c3p0 , proxool
URL的多种指定方式
连接池监控
上班第二天,看到公司的Hibernate配置文件在写法上,跟我以前的不一样,就去研究了哈,得出的结果如下。
在hibernate3.0中,已经不再支持dbcp了,hibernate的作者在hibernate.org中,
明确指出在实践中发现dbcp有BUG,在某些种情会产生很多空连接不能释放,
所以抛弃了对dbcp的支持。
1 =================== C3P0 配置 =============================
Xml代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20hibernate-configuration%20PUBLIC%0A%20%20%22-%2F%2FHibernate%2FHibernate%20Configuration%20DTD%203.0%2F%2FEN%22%0A%20%20%22http%3A%2F%2Fhibernate.sourceforge.net%2Fhibernate-configuration-3.0.dtd%22%3E%0A%3Chibernate-configuration%3E%0A%20%20%20%20%3Csession-factory%3E%0A%20%20%20%20%3C!--%20%E9%85%8D%E7%BD%AE%E4%BA%8B%E5%8A%A1%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%20--%3E%20%0A%20%20%20%20%20%3Cproperty%20name%3D%22transaction.factory_class%22%3E%0A%20%20%20%20%20%20%20org.hibernate.transaction.JDBCTransactionFactory%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%3C%2Fproperty%3E%0A%20%20%20%0A%20%20%20%20%3C!--%20%E9%85%8D%E7%BD%AEJDBC%E9%87%8Cbatch%E7%9A%84%E5%A4%A7%E5%B0%8F%20--%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22jdbc.batch_size%22%3E50%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22cache.use_second_level_cache%22%3Efalse%3C%2Fproperty%3E%0A%20%20%20%0A%20%20%20%20%3C!--%20%E9%85%8D%E7%BD%AE%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84session%20--%3E%0A%20%20%20%20%20%3Cproperty%20name%3D%22current_session_context_class%22%3Ethread%3C%2Fproperty%3E%0A%20%20%20%0A%20%20%20%20%20%20%3C!--%20%E6%98%BE%E7%A4%BASQL%20--%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22show_sql%22%3Etrue%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22format_sql%22%3Etrue%3C%2Fproperty%3E%0A%20%20%20%20%20%0A%20%20%20%20%20%20%3C!--%20%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%B9%E8%A8%80%20--%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22dialect%22%3Eorg.hibernate.dialect.Oracle9Dialect%3C%2Fproperty%3E%0A%20%20%20%20%20%0A%20%20%20%20%20%20%3C!--%20%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%20--%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22connection.driver_class%22%3Eoracle.jdbc.driver.OracleDriver%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22connection.username%22%3E0804%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22connection.password%22%3E0804%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22connection.url%22%3Ejdbc%3Aoracle%3Athin%3A%40192.168.0.200%3A1521%3Aorcl%3C%2Fproperty%3E%0A%20%20%20%20%0A%20%20%20%20%20%20%3C!--%20%E9%85%8D%E7%BD%AE%E8%BF%9E%E6%8E%A5%E6%B1%A0%20--%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.max_size%22%3E2%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.min_size%22%3E2%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.timeout%22%3E5000%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.max_statements%22%3E100%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.idle_test_period%22%3E3000%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.acquire_increment%22%3E2%3C%2Fproperty%3E%0A%20%20%20%20%20%20%3Cproperty%20name%3D%22c3p0.validate%22%3Efalse%3C%2Fproperty%3E%0A%20%20%20%20%20%0A%20%20%20%20%20%3C!--%20%E6%8C%87%E5%AE%9Ahibernate%E7%AE%A1%E7%90%86%E7%9A%84%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6%20--%3E%0A%20%20%20%20%20%20%3Cmapping%20resource%3D%22com%2Fket%2Fruan%2Fso%2Fentity%2FUser.hbm.xml%22%2F%3E%20%0A%20%20%20%20%20%20%3Cmapping%20resource%3D%22com%2Fket%2Fruan%2Fso%2Fentity%2FProduct.hbm.xml%22%2F%3E%20%0A%20%20%20%20%20%20%E3%80%82%E3%80%82%E3%80%82%E3%80%82%E3%80%82%0A%20%20%20%20%3C%2Fsession-factory%3E%0A%3C%2Fhibernate-configuration%3E" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1232348908231" lk_media="yes">
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- 配置事务实现方式 -->
- <property name="transaction.factory_class">
- org.hibernate.transaction.JDBCTransactionFactory
- </property>
-
- <!-- 配置JDBC里batch的大小 -->
- <property name="jdbc.batch_size">50</property>
- <property name="cache.use_second_level_cache">false</property>
-
- <!-- 配置线程安全的session -->
- <property name="current_session_context_class">thread</property>
-
- <!-- 显示SQL -->
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
-
- <!-- 配置数据库方言 -->
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
-
- <!-- 配置数据库连接 -->
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="connection.username">0804</property>
- <property name="connection.password">0804</property>
- <property name="connection.url">jdbc:oracle:thin:@192.168.0.200:1521:orcl</property>
-
- <!-- 配置连接池 -->
- <property name="c3p0.max_size">2</property>
- <property name="c3p0.min_size">2</property>
- <property name="c3p0.timeout">5000</property>
- <property name="c3p0.max_statements">100</property>
- <property name="c3p0.idle_test_period">3000</property>
- <property name="c3p0.acquire_increment">2</property>
- <property name="c3p0.validate">false</property>
-
- <!-- 指定hibernate管理的映射文件 -->
- <mapping resource="com/ket/ruan/so/entity/User.hbm.xml"/>
- <mapping resource="com/ket/ruan/so/entity/Product.hbm.xml"/>
- 。。。。。
- </session-factory>
- </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置事务实现方式 --> <property name="transaction.factory_class"> org.hibernate.transaction.JDBCTransactionFactory </property> <!-- 配置JDBC里batch的大小 --> <property name="jdbc.batch_size">50</property> <property name="cache.use_second_level_cache">false</property> <!-- 配置线程安全的session --> <property name="current_session_context_class">thread</property> <!-- 显示SQL --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 配置数据库方言 --> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- 配置数据库连接 --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.username">0804</property> <property name="connection.password">0804</property> <property name="connection.url">jdbc:oracle:thin:@192.168.0.200:1521:orcl</property> <!-- 配置连接池 --> <property name="c3p0.max_size">2</property> <property name="c3p0.min_size">2</property> <property name="c3p0.timeout">5000</property> <property name="c3p0.max_statements">100</property> <property name="c3p0.idle_test_period">3000</property> <property name="c3p0.acquire_increment">2</property> <property name="c3p0.validate">false</property> <!-- 指定hibernate管理的映射文件 --> <mapping resource="com/ket/ruan/so/entity/User.hbm.xml"/> <mapping resource="com/ket/ruan/so/entity/Product.hbm.xml"/> 。。。。。 </session-factory> </hibernate-configuration>
2 ============= 使用 proxool =============================
Xml代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=%3Chibernate-configuration%3E%0A%3Csession-factory%3E%0A%20%3Cproperty%20name%3D%22show_sql%22%3Etrue%3C%2Fproperty%3E%0A%20%3Cproperty%20name%3D%22dialect%22%3E%0A%20%20org.hibernate.dialect.MySQLDialect%0A%20%3C%2Fproperty%3E%0A%20%0A%20%3C!--%20%E9%85%8D%E7%BD%AEproxool%E5%B1%9E%E6%80%A7%20--%3E%0A%20%3C!---%20%E4%BD%BF%E7%94%A8proxoolConf.xml%E4%B8%AD%E5%AE%9A%E4%B9%89%E7%9A%84%E5%88%AB%E5%90%8D%20--%3E%0A%20%3Cproperty%20name%3D%22proxool.pool_alias%22%3Epool1%3C%2Fproperty%3E%0A%20%3C!--%20%E6%8C%87%E5%AE%9A%E4%BD%BF%E7%94%A8%E7%9A%84proxoolConf%E6%96%87%E4%BB%B6%E7%9A%84%E4%BD%8D%E7%BD%AE%EF%BC%88%E8%BF%99%E9%87%8C%E5%9C%A8SRC%E4%B8%8B%EF%BC%89--%3E%0A%20%3Cproperty%20name%3D%22proxool.xml%22%3EProxoolConf.xml%3C%2Fproperty%3E%0A%20%3Cproperty%20name%3D%22connection.provider_class%22%3E%0A%20org.hibernate.connection.ProxoolConnectionProvider%0A%20%3C%2Fproperty%3E%0A%20....%0A%20%3C!--%20%E6%8C%87%E5%AE%9Ahibernate%E7%AE%A1%E7%90%86%E7%9A%84%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6%20--%3E%0A%20%20%3Cmapping%20resource%3D%22com%2Fket%2Fruan%2Fso%2Fentity%2FUser.hbm.xml%22%2F%3E%20%0A%20%20....%0A%3C%2Fhibernate-configuration%3E" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1232348908235" lk_media="yes">
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
-
- <!-- 配置proxool属性 -->
- <!--- 使用proxoolConf.xml中定义的别名 -->
- <property name="proxool.pool_alias">pool1</property>
- <!-- 指定使用的proxoolConf文件的位置(这里在SRC下)-->
- <property name="proxool.xml">ProxoolConf.xml</property>
- <property name="connection.provider_class">
- org.hibernate.connection.ProxoolConnectionProvider
- </property>
- ....
- <!-- 指定hibernate管理的映射文件 -->
- <mapping resource="com/ket/ruan/so/entity/User.hbm.xml"/>
- ....
- </hibernate-configuration>
<hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 配置proxool属性 --> <!--- 使用proxoolConf.xml中定义的别名 --> <property name="proxool.pool_alias">pool1</property> <!-- 指定使用的proxoolConf文件的位置(这里在SRC下)--> <property name="proxool.xml">ProxoolConf.xml</property> <property name="connection.provider_class"> org.hibernate.connection.ProxoolConnectionProvider </property> .... <!-- 指定hibernate管理的映射文件 --> <mapping resource="com/ket/ruan/so/entity/User.hbm.xml"/> .... </hibernate-configuration>