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

时间:2021-02-22 06:47:18

I'm using hibernate 3.6 with MSSQL 2005, 2008, 2012.

我正在使用hibernate 3.6和MSSQL 2005,2008,2012。

I would like to set the isolation level of a transaction created by the session, but I can't find any information about in.

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

This is my code

这是我的代码

   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();
   }

I would like an to do something like that

我想要做一些类似的事情

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

Is that possible?

那可能吗?

Thank you.

谢谢。

1 个解决方案

#1


2  

I just found out one solution that worked for me

我刚刚发现了一个对我有用的解决方案

        if (forceReadCommitted) {

            this.session.doWork(new Work() {

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

                    connection.setTransactionIsolation(2);
                }
            });
        }

but you can notice that this is for the whole connection and not for a specific transaction. There might be better solutions still.

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

#1


2  

I just found out one solution that worked for me

我刚刚发现了一个对我有用的解决方案

        if (forceReadCommitted) {

            this.session.doWork(new Work() {

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

                    connection.setTransactionIsolation(2);
                }
            });
        }

but you can notice that this is for the whole connection and not for a specific transaction. There might be better solutions still.

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