我正在使用一個庫,我需要獲取數據源並將其提供給它。無論如何,我可以從連接池中獲得連接嗎?我正在使用Hibernate 4和C3p0連接池。從C3P0連接池獲取數據源/連接
,這裏是我的hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sampleDB</property>
<property name="connection.username">root</property>
<property name="connection.password">mypass</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.idle_test_period">30</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Shows Generated SQL Queries By Hibernate -->
<property name="show_sql">false</property>
<!-- Drop and re-create The Database Schema on Start up -->
<property name="hbm2ddl.auto">update</property>
<property name="cache.provider.class">org.hibernate.cache.NoCacheProvider</property>
</session-factory>
</hibernate-configuration>
你能舉例說明如何做到這一點嗎?如果我「泄漏」連接,我認爲它會自動關閉? – user962206 2013-05-01 11:16:06
查看上面的更新。 – 2013-05-01 11:55:59
謝謝,雖然我目前正在使用它來設置我的c3p0連接,https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool在哪裏可以設置dataSourceName?你的findUniqueDataSource有什麼用處?爲什麼我可以直接使用findDataSourceByName? – user962206 2013-05-01 12:00:46