配置文件可配项:
参考文档: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 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5
6
7 root
8 123456
9 com.mysql.jdbc.Driver
10 jdbc:mysql://localhost/hibernate_01
11
12
14 org.hibernate.dialect.MySQL5InnoDBDialect
15
16 true
17
18 true
19
20 update
21
22 thread
23
24
25
26
27
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
示例:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
。。。500
20
10
2000
2000
10
2)导入c3p0需要的jar包
下载后解压的开发包路径中\hibernate-release-5.2.9.Final\lib\optional\c3p0下jar包导入进工程即可。
3)代码测试:
packagecom.dx.hibernate5.test;importjava.sql.Connection;importjava.sql.SQLException;importjava.util.Date;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.boot.Metadata;importorg.hibernate.boot.MetadataSources;importorg.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;importorg.hibernate.boot.registry.StandardServiceRegistry;importorg.hibernate.boot.registry.StandardServiceRegistryBuilder;importorg.hibernate.jdbc.Work;public classHelloWord {public static voidmain(String[] args) {//1、创建一个SessionFactory对象//但是如果你使用Hibernate5的版本,就会报错。那么Hibernate5应该怎样构建SessionFactory呢,如下://和V4版本比,V5版本看不到configure对象了。直接使用创建者模式构建出了标准服务注册对象
StandardServiceRegistry standardRegistry = newStandardServiceRegistryBuilder().configure().build();//这个对象metadata对象应该扮演了一个万金油的角色,使用以上的注册对象作为入参构建这个对象
Metadata metadata = newMetadataSources(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(newWork() {
@Overridepublic void execute(Connection connection) throwsSQLException {//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]