【问题标题】:In hibernate how to programmatically set the isolation level of a transaction, or how to create two transactions with different isolation levels在hibernate中如何以编程方式设置事务的隔离级别,或者如何创建两个具有不同隔离级别的事务
【发布时间】:2014-03-18 12:47:43
【问题描述】:

我正在使用带有 MSSQL 2005、2008、2012 的休眠 3.6。

我想设置会话创建的事务的隔离级别,但是我找不到任何关于in的信息。

这是我的代码

   Session sess = factory.openSession();
   Transaction tx = null;
   try {
       tx = sess.beginTransaction();

       // do some work
       ...

       tx.commit();
   }
   catch (RuntimeException e) {
       if (tx != null) tx.rollback();
       throw e; // or display error message
   }
   finally {
       sess.close();
   }

我想做这样的事情

   sess.beginTransaction(1|2|4|8);

这可能吗?

谢谢。

【问题讨论】:

    标签: java sql-server hibernate transactions isolation-level


    【解决方案1】:

    我刚刚找到了一个适合我的解决方案

            if (forceReadCommitted) {
    
                this.session.doWork(new Work() {
    
                    @Override
                    public void execute(Connection connection) throws SQLException {
    
                        connection.setTransactionIsolation(2);
                    }
                });
            }
    

    但您会注意到这是针对整个连接而不是针对特定事务的。可能还有更好的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 2011-09-30
      • 2011-12-17
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多