Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the transaction isolation level used for the particular query in hibernate?

I am using Spring MVC and hibernate. My underlying database is Sybase ASA. Inside my DAO I want to find out what is the transaction level used for the query

This is what my DAO has.

Session session = getSession();
String SQL_QUERY = "select ..... ";
Query query = session.createSQLQuery(SQL_QUERY);
query.executeUpdate(); 

I referred to this link but this seems to be outdated so doesn't work What is default isolation level hibernate uses if not explicitely set?

like image 749
user1549994 Avatar asked Aug 19 '16 19:08

user1549994


1 Answers

I would say that question is raised incorrectly: a query itself doesn't have any isolation level. An Isolation level is a feature of a transaction.

So you, most probably, want to know what is the isolation level of current transaction? Hence you, most probably, do not manage this level by your code. In this case it is managed by DBMS default or current settings.

One of possible ways to explore it is described here:

try {
    session = getSessionFactory().openSession();
    txn = session.beginTransaction();
    session.doWork(new Work() {
        @Override
        public void execute(Connection connection) throws SQLException {
            LOGGER.debug("Transaction isolation level is {}", Environment.isolationLevelToString(connection.getTransactionIsolation()));
        }
    });
    txn.commit();
} catch (RuntimeException e) {
    if ( txn != null && txn.isActive() ) txn.rollback();
    throw e;
} finally {
    if (session != null) {
        session.close();
    }
}
like image 198
Andremoniy Avatar answered Nov 14 '22 21:11

Andremoniy