hibernate框架中附带提交事务的方法getCurrentSession()
2017-09-21 09:18
459 查看
1.创建session工厂
public class HibernateUtil {
private static SessionFactory sf ;
public static SessionFactory getSessionFactory(){
Configuration cfg = new Configuration();
cfg.configure(); //加载解析 hibernate.cfg.xml
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(cfg.getProperties());
ServiceRegistry sr = builder.build();
sf = cfg.buildSessionFactory(sr);
return sf;
}
}
2.创建session两种方式:
SessionFactory fs = HibernateUtil.getSessionFactory();
Session ss=null;
Transaction tr = null;
try{
ss = fs.openSession();//通过sessionFactory获得一个session对象 session == connection
tr = ss.getTransaction();
tr.begin();
ss.save(stu);
tr.commit();
}catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(ss != null){
ss.close();
}
}
如果是ss=fs.getCurrentSession(),则不需要手动处理事务,当创建session的时候会检查前面是否有session,有就返回原来的session对象
而openSession()每次创建都会创建新的session对象,且需要手动提交事务
eg. ses1=fs.getCurrentSession() ses2=fs.getCurrentSession()
ses1==ses2--------true
如果中途提交事务则原来的session会被销毁,重新创建session
ses1==ses2---------false
public class HibernateUtil {
private static SessionFactory sf ;
public static SessionFactory getSessionFactory(){
Configuration cfg = new Configuration();
cfg.configure(); //加载解析 hibernate.cfg.xml
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(cfg.getProperties());
ServiceRegistry sr = builder.build();
sf = cfg.buildSessionFactory(sr);
return sf;
}
}
2.创建session两种方式:
SessionFactory fs = HibernateUtil.getSessionFactory();
Session ss=null;
Transaction tr = null;
try{
ss = fs.openSession();//通过sessionFactory获得一个session对象 session == connection
tr = ss.getTransaction();
tr.begin();
ss.save(stu);
tr.commit();
}catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(ss != null){
ss.close();
}
}
如果是ss=fs.getCurrentSession(),则不需要手动处理事务,当创建session的时候会检查前面是否有session,有就返回原来的session对象
而openSession()每次创建都会创建新的session对象,且需要手动提交事务
eg. ses1=fs.getCurrentSession() ses2=fs.getCurrentSession()
ses1==ses2--------true
如果中途提交事务则原来的session会被销毁,重新创建session
ses1==ses2---------false
相关文章推荐
- Hibernate框架使用 getCurrentSession()获取Session对象相关问题
- Hibernate中使用GetCurrentSession()方法
- Spring3与hibernate4整合,sessionFactory.getCurrentSession()方法获取不到Session对象异常解决办法
- hibernate中session在service实现事务openSession()和getCurrentSession的区别
- opensession()和getCurrentSession()方法的区别(JTA(java Transaction Api,分布式事务)事务和Connection事务:数据库自带的事务处理方式)
- Hibernate框架学习(第三讲)---openSession 和getCurrentSession的区别
- SpringMVC 4.0集成Hibernate 4如何使用sessionFactory.getCurrentSession()方法
- hibernate中openSession()跟getCurrentSession()方法之间的区别
- etHibernateTemplate().getSessionFactory().getCurrentSession()事务中的session
- hibernate 使用sessionfactory的getCurrentSession()方法发生异常
- Hibernate 中的 openSession和getCurrentSession 方法的区别
- spring和hibernate整合使用getCurrentSession方法
- 。getCurrentSession是所有的操作都必须开启一个事务在事务中进行,并且事务提交后,session就会自动关闭,不需要再显示关闭。
- 框架学习之Hibernate 第三节 session接口以及get/load/persist方法
- hibernate的session提交前的getcurrentsession一样和提交后的getcurrentsession不一样(六)
- hibernate获取session的两种方式分别是openSession() 方法和getCurrentSession() 方法。
- hibernate中session的get()、load()方法比较
- Hibernate 之getCurrentSession 与 openSession() 的区别
- 框架 day34 Hibernate,h对事务并发处理,管理session,二级缓存,h练习案例
- Hibernate中对数据库的session操作除了查询操作外,都必须在事务(Transaction)提交后才能执行