Hibernate(五):Hibernate配置文件及C3P0的用法

  • 配置文件可配项:

参考文档:hibernate-release-5.2.9.Final/documentation/userguide/html_single/Hibernate_User_Guide.html

1)Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需要的各种属性。

  hibernate.cfg.xml常用的属性:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="hibernate.connection.username">root</property>
 8         <property name="hibernate.connection.password">123456</property>
 9         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_01</property>
11 
12         <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
13             <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->
14         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
15 
16         <property name="hibernate.show_sql">true</property>
17 
18         <property name="hibernate.format_sql">true</property>
19 
20         <property name="hibernate.hbm2ddl.auto">update</property>
21 
22         <property name="hibernate.current_session_context_class">thread</property>
23 
24         <mapping resource="com/dx/hibernate5/test/News.hbm.xml" />
25         <mapping class="com.dx.hibernate5.test.News" />
26     </session-factory>
27 </hibernate-configuration>

2)每个Hibernate配置文件对应一个Hibernate Configuration类对象。

3)配置Hibernate配置文件可以有两种格式:

  hibernate.properties

  hibernate.cfg.xml(推荐使用这种配置方式)

  • 使用Hibernate的“C3P0”管理“数据库连接池

1)C3P0数据库连接池属性

  // 数据库连接池的最大连接数

  hibernate.c3p0.max_size 

  // 数据库连接池的最大连接数

  hibernate.c3p0.min_size 

  // 数据库连接池中连接对象在多长时间没有使用过后,就应该被销毁

  hibernate.c3p0.timeout 

  // 缓存Statement对象的数量

  hibernate.c3p0.max_statements 

  // 表示连接池检测线程多长时间检测一次线程池内的所有连接对象是否超时。

  // 连接池本身不会把自己从连接池中移除,而是专门有一个线程按照一定的时间间隔来做这个事情。

  // 这个线程通过比较连接对象最后一次被使用时间和当前时间的时间差来和timeout做对比,进而决定是否销毁这个连接对象。

  hibernate.c3p0.max_idle_test_period

  // 当数据库连接池中的连接耗尽时,同一时刻获取多少个新的数据连接。

  hibernate.c3p0.accquire.increment

 示例:

<?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>
        <!--other settings-->
                。。。
        <property name="hibernate.c3p0.max_size">500</property>
        <property name="hibernate.c3p0.min_size">20</property>
        <property name="hibernate.c3p0.max_statements">10</property>
        <property name="hibernate.c3p0.timeout">2000</property>
        <property name="hibernate.c3p0.idle_test_period">2000</property>
        <property name="hibernate.c3p0.acquire_increment">10</property>

        <mapping resource="com/dx/hibernate5/test/News.hbm.xml" />
        <mapping class="com.dx.hibernate5.test.News" />
    </session-factory>
</hibernate-configuration>

2)导入c3p0需要的jar包

下载后解压的开发包路径中hibernate-release-5.2.9.Finalliboptionalc3p0下jar包导入进工程即可。

3)代码测试:

package com.dx.hibernate5.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.jdbc.Work;

public class HelloWord {
    public static void main(String[] args) {
        // 1、创建一个SessionFactory对象
        // 但是如果你使用Hibernate5的版本,就会报错。那么Hibernate5应该怎样构建SessionFactory呢,如下:
        // 和V4版本比,V5版本看不到configure对象了。直接使用创建者模式构建出了标准服务注册对象
        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();

        // 这个对象metadata对象应该扮演了一个万金油的角色,使用以上的注册对象作为入参构建这个对象
        Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder()
                .applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();

        // 最后由这个metadata使用构建出sessionFactory
        SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();

        // 2、创建一个Session对象
        Session session = sessionFactory.getCurrentSession();
        // 3、开启事物
        Transaction transaction = session.beginTransaction();
        // 4、查看connection对象类型是否为c3p0        
        session.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                // TODO Auto-generated method stub
                System.out.println(connection);
            }
        });
        
        // 5、提交事物
        transaction.commit();
        // 6、关闭Session对象
        session.close();
        // 7、关闭SessionFactory对象
        sessionFactory.close();

        System.out.println("Complete...");
    }
}

打印结果:

com.mchange.v2.c3p0.impl.NewProxyConnection@58a55449 [wrapping: com.mysql.jdbc.JDBC4Connection@7a676440]
原文地址:https://www.cnblogs.com/yy3b2007com/p/6733419.html