Using c3p0 with Hibernate – 在Hibernate中使用c3p0

最后修改: 2018年 12月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

It’s quite expensive to establish database connections. Database connection pooling is a well-established way to lower this expenditure.

建立数据库连接是相当昂贵的。数据库连接池是一种成熟的降低这种支出的方法。

In this tutorial, we’ll discuss how to use c3p0 with Hibernate to pool connections.

在本教程中,我们将讨论如何将c3p0与Hibernate一起使用来汇集连接。

2. What Is c3p0?

2.什么是c3p0??

c3p0 is a Java library that provides a convenient way for managing database connections.

c3p0是一个Java库,为管理数据库连接提供了一种方便的方法

In short, it achieves this by creating a pool of connections. It also effectively handles the cleanup of Statements and ResultSets after use. This cleanup is necessary to ensure that resource usage is optimized and avoidable deadlocks do not occur.

简而言之,它是通过创建一个连接池来实现的。它还有效地处理了Statements和ResultSets使用后的清理工作。这种清理是必要的,以确保资源的使用得到优化,避免发生可避免的死锁。

This library integrates seamlessly with various traditional JDBC drivers. Additionally, it provides a layer for adapting DriverManager-based JDBC drivers to the newer javax.sql.DataSource scheme.

该库可与各种传统的JDBC驱动程序无缝集成。此外,它还提供了一个层,用于将基于DriverManager的JDBC驱动程序调整为较新的javax.sql.DataSource方案。

And, because Hibernate supports connecting to databases over JDBC, it’s simple to use Hibernate and c3p0 together.

而且,由于 Hibernate 支持通过 JDBC 连接到数据库,将 Hibernate 和 c3p0 一起使用很简单。

3. Configuring c3p0 With Hibernate

3.用Hibernate配置c3p0

Let’s now look at how to configure an existing Hibernate application to use c3p0 as its database connection manager.

现在让我们看看如何配置一个现有的Hibernate应用程序,以使用c3p0作为其数据库连接管理器。

3.1. Maven Dependencies

3.1.Maven的依赖性

Firstly, we’ll first need to add the hibernate-c3p0 maven dependency:

首先,我们需要添加hibernate-c3p0 maven依赖。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.3.6.Final</version>
</dependency>

With Hibernate 5, just adding the above dependency is enough to enable c3p0. This is true as long as no other JDBC connection pool manager is specified.

对于Hibernate 5,只需添加上述依赖关系就足以启用c3p0。只要没有指定其他的JDBC连接池管理器,这一点就是真的。

Therefore, after we add the dependency, we can run our application and check the logs:

因此,在我们添加依赖关系后,我们可以运行我们的应用程序并检查日志。

Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@34d0bdb9 [ ... default settings ... ]

If another JDBC connection pool manager is being used, we can force our application to use c3p0. We just need to set the provider_class to C3P0ConnectionProvider in our properties file:

如果正在使用另一个JDBC连接池管理器,我们可以强制我们的应用程序使用c3p0。我们只需要在我们的属性文件中将provider_class设置为C3P0ConnectionProvider

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider

3.2. Connection Pool Properties

3.2.连接池属性

Eventually, we will need to override the default configuration. We can add custom properties to the hibernate.cfg.xml file:

最终,我们将需要覆盖默认的配置。我们可以在hibernate.cfg.xml文件中添加自定义属性。

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="hibernate.c3p0.timeout">1800</property>

Likewise, the hibernate.properties file can contain the same settings:

同样,hibernate.properties文件可以包含相同的设置。

hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=5
hibernate.c3p0.timeout=1800

The min_size property specifies the minimum number of connections it should maintain at any given time. By default, it will maintain at least three connections. This setting also defines the initial size of the pool.

min_size属性指定了它在任何给定时间内应维护的最小连接数。默认情况下,它将维护至少三个连接。这个设置也定义了池子的初始大小。

The max_size property specifies the maximum number of connections it can maintain at any given time. By default, it will keep a maximum of 15 connections.

max_size属性指定了它在任何时候可以保持的最大连接数。默认情况下,它最多保留15个连接

The acquire_increment property specifies how many connections it should try to acquire if the pool runs out of available connections. By default, it will attempt to acquire three new connections.

acquire_increment属性指定了如果池子里的可用连接用完,它应该尝试获取多少个连接。默认情况下,它将尝试获取三个新连接。

The timeout property specifies the number of seconds an unused connection will be kept before being discarded. By default, connections will never expire from the pool.

timeout属性指定了未使用的连接在被丢弃之前将被保留的秒数。默认情况下,连接将永远不会从池中过期。

We can verify the new pool settings by checking the logs again:

我们可以通过再次检查日志来验证新的池设置。

Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@b0ad7778 [ ... new settings ... ]

These are the basic connection pool properties. In addition, other configuration properties can be found in the official guide.

这些是基本的连接池属性。此外,其他配置属性可以在官方指南中找到。

5. Conclusion

5.结论

In this article, we’ve discussed how to use c3p0 with Hibernate. We’ve looked at some common configuration properties and added c3p0 to a test application.

在这篇文章中,我们已经讨论了如何在Hibernate中使用c3p0。我们看了一些常见的配置属性,并将c3p0添加到一个测试应用程序中。

For most environments, we recommend using a connection pool manager such as c3p0 or HikariCP instead of traditional JDBC drivers.

对于大多数环境,我们建议使用连接池管理器,如c3p0或HikariCP而不是传统的JDBC驱动程序。

As usual, the complete source code for this tutorial is available on GitHub.

像往常一样,本教程的完整源代码可在GitHub上获得。