In hibernate how to programmatically set the isolation level of a transaction, or how to create two transactions with different isolation levels

StackOverflow https://stackoverflow.com/questions/21859305

문제

I'm using hibernate 3.6 with 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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top