💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Hibernate C3P0 连接池配置教程 > 原文: [https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/](https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/) 默认情况下,Hibernate 使用 JDBC 连接以便与数据库进行交互。 创建这些连接非常昂贵 - Hibernate 在典型的使用情况下可能会执行最昂贵的单个操作。 由于 JDBC 连接管理非常昂贵,因此您可能会建议您使用连接池,它可以提前打开连接(并仅在需要时关闭它们,而不是“不再使用时”)。 幸运的是,默认情况下,Hibernate 设计为使用连接池(内部实现)。 但是,Hibernate 的内置连接池并非设计用于生产用途。 在生产中,将通过使用 JNDI 提供的数据库连接或通过参数和类路径配置的外部连接池来使用外部连接池。 [**C3P0**](http://www.mchange.com/projects/c3p0/ "c3p0") 是外部连接池的示例。 在本教程中,我们将学习如何将其与 hibernate 一起使用。 ```java Table of Contents 1) Maven dependencies 2) Configure C3P0 Connection Pool with Hibernate 3) Test connection pooling in runtime ``` ## 1)Maven 依赖 要使用 Hiberate 配置 c3p0,我们需要在`pom.xml`中添加 c3p0 和 Hibernate 的 c3p0 连接供应器作为依赖项。 请注意,`hibernate-c3p0`依赖项的版本应与 Hibernate 的兼容版本匹配。 以下配置使用了最新版本的 hibernate 和 c3p0。 ```java <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.6.Final</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> ``` ## 2)使用 Hiberate 配置 C3P0 连接池 最好的部分是,使用 Hiberate 配置 C3P0 确实非常容易。 **只需在`hibernate.cfg.xml`文件**中添加任何 c3p0 属性即可。 例如在`hibernate.cfg.xml`中添加以下条目。 ```java <property name="hibernate.c3p0.min_size">10</property> ``` 恭喜!! 现在,使用您的应用的 Hiberate 层配置了 C3P0 的连接池。 您很好地开始测试这些东西。 真的很简单,不是吗? 可以使用`hibernate.cfg.xml`中的以下属性来设置相当详细的配置。 ```java <property name="hibernate.c3p0.min_size">10</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.acquire_increment">1</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.timeout">1800</property> ``` 您可以在[**官方文档**](http://www.mchange.com/projects/c3p0/#configuration "configuration")中找到有关上述配置开关的详细信息。 ## 3)在运行时测试连接池 #### 不带 C3P0 连接池配置 在未配置 C3P0 的情况下,如果您看到 Hiberate 的调试日志,则会看到以下内容: ```java DEBUG Configuration:1841 - Preparing to build session factory with filters : {} WARN DriverManagerConnectionProviderImpl:93 - HHH000402: Using Hibernate built-in connection pool (not for production use!) INFO DriverManagerConnectionProviderImpl:166 - HHH000401: using driver [org.hsqldb.jdbcDriver] at URL [jdbc:hsqldb:mem:howtodoinjava] INFO DriverManagerConnectionProviderImpl:172 - HHH000046: Connection properties: {user=sa, password=} INFO DriverManagerConnectionProviderImpl:180 - HHH000006: Autocommit mode: false INFO DriverManagerConnectionProviderImpl:102 - HHH000115: Hibernate connection pool size: 20 (min=1) DEBUG DriverManagerConnectionProviderImpl:104 - Initializing Connection pool with 1 Connections ... ... ... EBUG JdbcTransaction:113 - committed JDBC Connection DEBUG SessionFactoryImpl:1339 - HHH000031: Closing DEBUG AbstractServiceRegistryImpl:406 - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries INFO DriverManagerConnectionProviderImpl:281 - HHH000030: Cleaning up connection pool [jdbc:hsqldb:mem:howtodoinjava] ``` #### 使用 C3P0 连接池配置 配置 C3P0 连接池后,您将在日志中看到现在已从 C3P0 连接池本身获取连接。 ```java DEBUG Configuration:1841 - Preparing to build session factory with filters : {} INFO C3P0ConnectionProvider:133 - HHH010002: C3P0 using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:mem:howtodoinjava INFO C3P0ConnectionProvider:134 - HHH000046: Connection properties: {user=sa, password=****} INFO C3P0ConnectionProvider:137 - HHH000006: Autocommit mode: false INFO MLog:92 - MLog clients using log4j logging. INFO C3P0Registry:216 - Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10] DEBUG DynamicPooledDataSourceManagerMBean:258 - MBean: com.mchange.v2.c3p0:type=PooledDataSource,identityToken=19tu9of94ho8s13xij3fm|34e475e1,name=19tu9of94ho8s13xij3fm|34e475e1 registered. DEBUG DynamicPooledDataSourceManagerMBean:253 - MBean: com.mchange.v2.c3p0:type=PooledDataSource,identityToken=19tu9of94ho8s13xij3fm|34e475e1,name=19tu9of94ho8s13xij3fm|34e475e1 unregistered, in order to be reregistered after update. DEBUG DynamicPooledDataSourceManagerMBean:258 - MBean: com.mchange.v2.c3p0:type=PooledDataSource,identityToken=19tu9of94ho8s13xij3fm|34e475e1,name=19tu9of94ho8s13xij3fm|34e475e1 registered. INFO AbstractPoolBackedDataSource:522 - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@d29bbf50 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@a9bbd924 [ ... ] ... ... ... DEBUG ActiveManagementCoordinator:97 - C3P0Registry mbean unregistered. DEBUG BasicResourcePool:1022 - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@1d1fcfbb DEBUG C3P0PooledConnectionPool:616 - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@1d1fcfbb DEBUG AbstractPoolBackedDataSource:477 - com.mchange.v2.c3p0.PoolBackedDataSource@34e475e1 has been closed. java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close(). at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:477) at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:489) at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:372) at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:348) at org.hibernate.c3p0.internal.C3P0ConnectionProvider.stop(C3P0ConnectionProvider.java:258) at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:377) at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:361) at org.hibernate.service.internal.AbstractServiceRegistryImpl.deRegisterChild(AbstractServiceRegistryImpl.java:410) at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:368) at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1377) at com.howtodoinjava.demo.util.HibernateUtil.shutdown(HibernateUtil.java:39) at com.howtodoinjava.test.TestHibernate.main(TestHibernate.java:22) ``` 这就是这个简单而有用的教程,有关如何使用 Hiberate 配置 C3P0 连接池。 **祝您学习愉快!** **参考**:[http://www.mchange.com/projects/c3p0/#configuration](http://www.mchange.com/projects/c3p0/#configuration "c3p0 configuration")