Hibernate Dbcp Connection Pooling Configuration

摘要: This tutorial shows you how to configure Hibernate dbcp Connection Pooling. By default hibernate comes with a built-in connection pool. But this connection pool is by no means production ready. They even advice not to use it in production, as you can see in the logging.

This tutorial shows you how to configure Hibernate dbcp Connection Pooling. By default hibernate comes with a built-in connection pool. But this connection pool is by no means production ready. They even advice not to use it in production, as you can see in the logging.

HHH10001002: Using Hibernate built-in connection pool (not for production use!)

Maven Dependencies

We are using Apache Maven to manage our project dependencies. Add the following dependencies to your projects pom.xml file.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.4</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.3.Final</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.7</version>
</dependency>

Hibernate dbcp Connection Pooling Configuration

The database connections and hibernate dbcp connection pooling configuration are in the hibernate.cfg.xml file, located on the classpath in the src/main/resources folder.

By default, dbcp uses sensible defaults, but you can override these settings by setting the following properties.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- database connection properties -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore?serverTimezone=Europe/Brussels</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- show mysql queries output in console -->
        <property name="hibernate.show_sql">false</property>

        <!-- manage automatic database creation -->
        <property name="hibernate.hbm2ddl.auto">create-drop</property>

        <!-- dbcp connection pool configuration -->
        <property name="hibernate.dbcp.initialSize">5</property>
        <property name="hibernate.dbcp.maxTotal">20</property>
        <property name="hibernate.dbcp.maxIdle">10</property>
        <property name="hibernate.dbcp.minIdle">5</property>
        <property name="hibernate.dbcp.maxWaitMillis">-1</property>

        <!-- add annotated resources here -->
        <mapping class="com.memorynotfound.hibernate.Book"/>

    </session-factory>
</hibernate-configuration>
  • initialSize: The initial number of connections that are created when the pool is started.
  • maxTotal: The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
  • maxIdle: The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
  • minIdle: The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none.
  • maxWaitMillis: The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.

Demo

When using the previous configuration, dbcp initializes the connection pool with 5 connections at startup. So when we run the application – we can see in the following screenshot – that there are 5 connections waiting in the pool. Also that there is one active connection being used.

Download

上一篇: Hibernate One to One Unidirectional Foreign Key
下一篇: Configure Hibernate Logging with slf4j + log4j2
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号