Hibernate的增删改查(事务)

Hibernate的事务:

     1、 设置事务隔离级别都是用  自己进行存储的

                                                                二进制      十进制

            read uncommitted   读未提交        0001         1

            read  committed   读已提交           0010         2

            repeatable  read  重读                   0100         4       mysql的默认级别

            serializable     可串行化                 1000          8

 

           配置数据库的隔离级别直接写十进制的值即可。

           在主配置文件(hibernate.cfg.xml)中配置数据库的隔离级别

           <property  name="hibernate.connection.isolation"></property>

     2、Hibernate 管理事务的方法

       //开启事务:

       Transcation t =  session.beginTransaction();

       try{

          //。。。。。写自己的代码(do  somework . .)

          //提交事务:

         }catch(Exception e){

            //异常后:回滚事务

            if(t!=null){

              t.rollbback();

             }

        }

 

     3、Hibernate得到Session对象的方式

             Configuration  c =  new Configuration()

             c.Configure();     //读取  hibernate.cfg.xml文件

             SessionFactory   factory  =  c.buildSessionFactory(); 

           方式一:

             factory.openSession();

          方式二:

              factory.getCurrentSession();

     

SessionFactory线程安全的

 

Session是线程不安全的

 

Session要线程安全  建议使用  getCurrentSession();

openSession()和getCurrentSession()

1、openSession()每次调用生成新的Session

    getCurrentSession()每次调用都使用同一个session(与当前线程绑定的session) 

2、openSession()使用不需要配置

   getCurrentSession() 必须要配置 ,在主配置文件 hibernate.cfg.xml中配置

  name="hibernate.current_session_context_class"

 

转载于:https://www.cnblogs.com/Leroyo/p/8283649.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值