Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 568
Rotem Avatar asked Feb 18 '14 16:02

Rotem


People also ask

What is isolation in hibernate?

Isolation in hibernate is similar to transactional isolations provided by database i. e the degree to which the data being updated is visible to other transaction taking place concurrently. Every database provide default isolation settings. There are isolation-levels. And hibernate provides you to set the isolation level for your application.

What is the default transaction isolation for NHibernate?

Fluent NHibernate doesn't do anything with the transaction isolation, so the default will be whatever NHibernate defaults to. I don't know off the top of my head what that is.

What is REPEATABLE READ isolation level in hibernate?

Because of repeatable reads isolation level, Hibernate's sees exactly the same number of rows for every SELECT query under began transaction. This level acts like a cache, so we can tell that the reads are faster than usually. But in the other hand, over again we are working with the data whose doesn't reflect the reality.

What is transaction isolation isolation?

Transaction Isolation Isolation is one of the common ACID properties: Atomicity, Consistency, Isolation, and Durability. Isolation describes how changes applied by concurrent transactions are visible to each other. Each isolation level prevents zero or more concurrency side effects on a transaction:


1 Answers

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.

like image 102
Rotem Avatar answered Sep 28 '22 20:09

Rotem