hibernate 本地线程绑定session
2017-03-31 11:39
465 查看
1.在hibernate.cfg.xml中加入:
2.在sessionFactory方法中获取
在UserUntil.java中加入
3.UserTrans.java
<property name="hibernate.current_session_context_class">thread</property>
2.在sessionFactory方法中获取
在UserUntil.java中加入
//返回与本地线程绑定的session的方法 public static Session getSessionobj(){ return sessionFactory.getCurrentSession(); }
3.UserTrans.java
package cn.hiber; import org.hibernate.Session; /*import org.hibernate.SessionFactory;*/ import org.hibernate.Transaction; import org.junit.Test; public class UserTrans { @Test public void testTx(){ /*SessionFactory sessionFactory=null;*/ Session session=null; Transaction tx=null; try { /* sessionFactory=UserUntil.getSessionFactory(); session=sessionFactory.openSession();*/ //获取本地线程绑定的session session=UserUntil.getSessionobj(); //开始事务 tx=session.beginTransaction(); //添加 User user=new User(); user.setUsername("qqqqeww"); user.setPassword("12345"); session.save(user); //提交事务 tx.commit(); } catch (Exception e) { tx.rollback(); }/*finally{ session.close(); sessionFactory.close(); }*/ } }
相关文章推荐
- hibernate事务代码规范和session与本地线程绑定
- hibernate中session与本地线程绑定
- Hibernate中Session与本地线程绑定
- [原创]java WEB学习笔记94:Hibernate学习之路---session 的管理,Session 对象的生命周期与本地线程绑定
- Hibernate中Session与本地线程作绑定
- Hibernate中Session与本地线程绑定
- Hibernate中Session与本地线程作绑定
- hibernate 管理Session:Session 对象的生命周期与本地线程绑定
- 重拾Hibernate框架——实体类、主键生成策略、session绑定本地线程
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate基础之transaction事务代码规范写法和session线程绑定
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)
- Hibernate的学习之路十六(绑定本地session)