在hibernate中如何以编程方式设置一个事务的隔离级别,或者如何创建两个不同隔离级别的事务

Rot*_*tem 7 java sql-server hibernate transactions isolation-level

我在 MSSQL 2005、2008、2012 中使用 hibernate 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();
   }
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情

   sess.beginTransaction(1|2|4|8);
Run Code Online (Sandbox Code Playgroud)

那可能吗?

谢谢你。

Rot*_*tem 4

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

        if (forceReadCommitted) {

            this.session.doWork(new Work() {

                @Override
                public void execute(Connection connection) throws SQLException {

                    connection.setTransactionIsolation(2);
                }
            });
        }
Run Code Online (Sandbox Code Playgroud)

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