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"

 

posted on 2018-01-14 16:45  Leroy_罗先生  阅读(186)  评论(0编辑  收藏  举报