一 opensession与getCurrentSession的区别
1、 getCurrentSession在事务提交或者回滚之后会自动关闭,而openSession需要手动关闭。如果使用openSession而没有手动关闭,多次之后会导致连接池溢出。
2、openSession每次创建新的session对象,getCurrentSession使用现有的session对象。
二 测试openSession每次创建新的session对象
1、测试代码
@Test
public void testOpenSession(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
// 创建会话工厂对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
// 创建会话对象
Session session1 = sessionFactory.openSession();
Session session2 = sessionFactory.openSession();
System.out.println(session1==session2);
}
2、运行结果
false
三 测试getCurrentSession使用现有的session对象
1、测试代码
@Test
public void testGetCurrentSession(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
// 创建会话工厂对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
// 创建会话对象
Session session1 = sessionFactory.getCurrentSession();
Session session2 = sessionFactory.getCurrentSession();
System.out.println(session1==session2);
}
2、运行结果
true
四 openSession用完后必须手动关闭,否则会溢出
1、测试代码
@Test
public void testSaveStudentswithOpenSession(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
// 创建会话工厂对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
// 创建会话对象
Session session1 = sessionFactory.openSession();
// 开启事务
Transaction transaction = session1.beginTransaction();
Students s= new Students(1,"张三丰","男",new Date(),"武当山");
session1.doWork(new Work(){
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection hashCode:"+connection.hashCode());
}
});
session1.save(s);
//session.close();
transaction.commit();
Session session2 = sessionFactory.openSession();
transaction = session2.beginTransaction();
s= new Students(2,"李四","男",new Date(),"武当山");
session2.doWork(new Work(){
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection hashCode:"+connection.hashCode());
}
});
session2.save(s);
transaction.commit();
}
2、测试结果
connection hashCode:1048278180
Hibernate:
insert
into
STUDENTS
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
connection hashCode:156943829
Hibernate:
insert
into
STUDENTS
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
五 getCurrentSession不用关闭session
1、测试代码
@Test
public void testSaveStudentswithGetCurrentSession(){
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
// 创建会话工厂对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
// 创建会话对象
Session session1 = sessionFactory.getCurrentSession();
// 开启事务
Transaction transaction = session1.beginTransaction();
Students s= new Students(1,"张三丰","男",new Date(),"武当山");
session1.doWork(new Work(){
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection hashCode:"+connection.hashCode());
}
});
session1.save(s);
transaction.commit();
Session session2 = sessionFactory.getCurrentSession();
transaction = session2.beginTransaction();
s= new Students(2,"李四","男",new Date(),"武当山");
session2.doWork(new Work(){
@Override
public void execute(Connection connection) throws SQLException {
// TODO Auto-generated method stub
System.out.println("connection hashCode:"+connection.hashCode());
}
});
session2.save(s);
transaction.commit();
}
2、运行结果
connection hashCode:455886737
Hibernate:
insert
into
STUDENTS
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
connection hashCode:455886737
Hibernate:
insert
into
STUDENTS
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
相关推荐
Session 在Hibernate中是一个重点,也是一个难点,本文主要对Session做一详尽的讲述。希望对正在学习Hibernate的朋友们有所帮助。
hibernate中session对象的状态详解
12.2.3 Session(提供Connection) 12.3 使用Hibernate操作数据库的示例 12.3.1 创建数据库表 12.3.2 编写表对应的JavaBean 12.3.3 编写JavaBean对应的Xml 12.3.4 编写Hibernate的配置文件 12.3.5 编写并验证测试...
Hibernate缓存深入详解,详细介绍Hibernate缓存机制。 1.Hibernate缓存概述 2.Hibernate一级缓存(Session缓存) 3.Hibernate二级缓存 4.查询缓存 5.二级缓存的高级应用(分布式缓存)
Hibernate应用详解,配置管理,session介绍等.
hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...
主要介绍了Hibernate中Session增删改查操作代码详解,具有一定借鉴价值,需要的朋友可以参考下。
12.2.3 Session(提供Connection) 12.3 使用Hibernate操作数据库的示例 12.3.1 创建数据库表 12.3.2 编写表对应的JavaBean 12.3.3 编写JavaBean对应的Xml 12.3.4 编写Hibernate的配置文件 12.3.5 编写并验证测试...
3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 3.6.4 把helloapp应用作为...
<session-factory> <!– 各属性的配置–> <!—为true表示将Hibernate发送给数据库的sql显示出来 –> ”show_sql”>true <!– SQL方言,这边设定的是MySQL –> ”dialect”>...
12.2.3 Session(提供Connection) 12.3 使用Hibernate操作数据库的示例 12.3.1 创建数据库表 12.3.2 编写表对应的JavaBean 12.3.3 编写JavaBean对应的Xml 12.3.4 编写Hibernate的配置文件 12.3.5 编写并验证测试...
Hibernate的统计(statistics)机制 4.5. 日志 4.6. 实现NamingStrategy 4.7. XML配置文件 4.8. J2EE应用程序服务器的集成 4.8.1. 事务策略配置 4.8.2. JNDI绑定的SessionFactory 4.8.3. JTA和Session的...
本章学习目标 Hibernate 初认识 Hibernate 的Hello... hibernate.cfg.xml 配置详解 *.hbm.xml 对象映射文件详解 Hibernate 核心接口详解 抽取HibernateUtil 工具类 使用Session 接口进行增删改查
主要介绍了Hibernate的Session_flush与隔离级别代码详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 3.6.4 把helloapp应用作为...
3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 3.6.4 把helloapp应用作为...
3.5.2 访问Hibernate的Session接口 3.6 运行helloapp应用 3.6.1 创建运行本书范例的系统环境 3.6.2 创建helloapp应用的目录结构 3.6.3 把helloapp应用作为独立应用程序运行 3.6.4 把helloapp应用作为...
11.1 传播Hibernate Session 11.1.1 Session传播的用例 11.1.2 通过线程局部传播 11.1.3 利用JTA传播 11.1.4 利用EJB传播 11.2 利用Hibernate的对话 11.2.1 提供对话保证 11.2.2 利用...
因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception; Open Session In View...