Hibernate(五):Hibernate配置文件及C3P0的用法
2017-04-19 14:43
411 查看
[b]配置文件可配项:[/b]
参考文档:hibernate-release-5.2.9.Final/documentation/userguide/html_single/Hibernate_User_Guide.html
1)Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需要的各种属性。
hibernate.cfg.xml常用的属性:
2)每个Hibernate配置文件对应一个Hibernate Configuration类对象。
3)配置Hibernate配置文件可以有两种格式:
hibernate.properties
hibernate.cfg.xml(推荐使用这种配置方式)
[b]使用Hibernate的“[b]C3P0”管理“数据库连接池”[/b][/b]
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
示例:
2)导入c3p0需要的jar包
下载后解压的开发包路径中\hibernate-release-5.2.9.Final\lib\optional\c3p0下jar包导入进工程即可。
![](https://images2015.cnblogs.com/blog/307536/201704/307536-20170420174201415-834151624.jpg)
3)代码测试:
打印结果:
参考文档:hibernate-release-5.2.9.Final/documentation/userguide/html_single/Hibernate_User_Guide.html
1)Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需要的各种属性。
hibernate.cfg.xml常用的属性:
<?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> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_01</property> <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/dx/hibernate5/test/News.hbm.xml" /> <mapping class="com.dx.hibernate5.test.News" /> </session-factory> </hibernate-configuration>
2)每个Hibernate配置文件对应一个Hibernate Configuration类对象。
3)配置Hibernate配置文件可以有两种格式:
hibernate.properties
hibernate.cfg.xml(推荐使用这种配置方式)
[b]使用Hibernate的“[b]C3P0”管理“数据库连接池”[/b][/b]
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.Final\lib\optional\c3p0下jar包导入进工程即可。
![](https://images2015.cnblogs.com/blog/307536/201704/307536-20170420174201415-834151624.jpg)
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]
相关文章推荐
- [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等
- 第5章Hibernate的基本用法 5.4深入Hibernate的配置文件
- hibernate使用c3p0的情况下直接oracle的配置文件
- <meta attribute="???">xxxx</meta>的用法 - hibernate配置文件生成java类
- hibernate配置文件中order-by用法
- 5 -- Hibernate的基本用法 --4 深入Hibernate配置文件
- Hibernate中配置C3P0数据源步骤及hbm.xml 文件详解
- Hibernate的配置文件(包括C3P0配置)
- 项目经验:Hibernate 映射配置文件precision与scale的用法
- hibernate 数据库字段为CLOB时,配置文件的用法
- 关于hibernate与spring的结合使用中的配置文件
- hibernate hbm.xml配置文件说明
- 配置Middlegen-Hibernate和hibernate-extensions以产生hbm映射文件和POJO
- Hibernate配置文件可以有两种格式
- 掌握 .NET 1.1 的配置文件用法
- Spring+Hibernate 配置和使用c3p0和proxool连接池
- 不同配置文件的初始化Hibernate心得
- Hibernate入门03 - 配置文件
- Hibernate不同配置文件读取方式
- Hibernate的配置文件