經(jīng)過前面的學習,已經(jīng)掌握了hibernate的基本使用指郁。本篇講的是hibernate使用數(shù)據(jù)庫連接池的配置。
1拷呆、使用C3P0
- 導入jar包坡氯,
hibernate-release-5.2.10.Final\lib\optional\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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 綁定本地session 放在本地線程中 共三個值 一般用thread與本地線程綁定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- C3P0連接池配置 -->
<!-- 丟棄 不必配置即可使用 property name="hibernate.connection.provider_class">
org.hibernate.c3p0.internal.C3P0ConnectionProvider
</property -->
<!-- 最大連接數(shù) -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小連接數(shù) -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 當連接池里面的連接用完的時候,C3P0一下獲取的新的連接數(shù) -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 最大的PreparedStatement的數(shù)量 -->
<property name="hibernate.c3p0.max_statements">50</property>
<!-- 連接的最大空閑時間,1800秒內(nèi)未使用則連接被丟棄。單位為秒 -->
<property name="hibernate.c3p0.timeout">1800</property>
<!-- 每隔120秒檢查連接池里的空閑連接是否超時,單位是秒 -->
<property name="hibernate.c3p0.idle_test_period">120</property>
<mapping resource="cn/lkangle/entity/onetwo.hbm.xml"/>
<mapping resource="cn/lkangle/entity/three.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 測試使用C3P0
控制臺輸出如上信息說明C3P0使用成功箫柳。END
在源碼中發(fā)現(xiàn)hibernate中C3P0有效的配置也就已上六個手形,而我百度到的還發(fā)現(xiàn)這樣一條配置
<property name="hibernate.c3p0.validate">true</property>
設置為true每次都檢測連接是否可用。沒有測試不知道是否有用悯恍。
下面是AvailableSettings
類中關(guān)于C3P0的配置部分代碼
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// c3p0 connection pooling specific settings
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* A setting prefix used to indicate settings that target the hibernate-c3p0 integration
*/
String C3P0_CONFIG_PREFIX = "hibernate.c3p0";
/**
* Maximum size of C3P0 connection pool
*/
String C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
/**
* Minimum size of C3P0 connection pool
*/
String C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
/**
* Maximum idle time for C3P0 connection pool
*/
String C3P0_TIMEOUT = "hibernate.c3p0.timeout";
/**
* Maximum size of C3P0 statement cache
*/
String C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
/**
* Number of connections acquired when pool is exhausted
*/
String C3P0_ACQUIRE_INCREMENT = "hibernate.c3p0.acquire_increment";
/**
* Idle time beforeQuery a C3P0 pooled connection is validated
*/
String C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";