30

I have an application that uses hibernate version 3.6.4, and c3p0 version 0.9.1.2 for connection pooling. My underlying RDBMS is MySql version 5.0.67.

My installation of MySql indicates that the default transaction isolation level is "REPEATABLE-READ" (4):

mysql> select @@GLOBAL.tx_isolation, @@tx_isolation;
+-----------------------+-----------------+
| @@GLOBAL.tx_isolation | @@tx_isolation  |
+-----------------------+-----------------+
| REPEATABLE-READ       | REPEATABLE-READ |
+-----------------------+-----------------+

I have not changed or configured the transaction isolation level within hibernate.cfg.xml or anywhere within my application. From the app, I use the following code to print configuration:

DatabaseMetaData meta = getSession().connection().getMetaData();
System.out.println("Default Tx Isolation: " + meta.getDefaultTransactionIsolation());
System.out.println("Current Tx Isolation: " + getSession().connection().getTransactionIsolation());

And I get the following results:

Default Tx Isolation: 2 (=READ_COMMITTED)
Current Tx Isolation: 4 (=REPEATABLE_READ)

So, my questions are the following:

  1. Where did the "2" value came from? Since the default is the REPEATABLE_READ, why getDefaultTransactionIsolation() returns READ_COMMITTED?
  2. What is the isolation level that hibernate uses after all? REPEATABLE_READ or READ_COMMITTED?
  3. I thought that when no isolation level is set, hibernate should use the underlying database's default. Is this true? Maybe the jbdc driver implementation sets a default on its own and hibernate uses this?

1 Answer 1

17

looks like meta.getDefaultTransactionIsolation()); is hardcoded in the implementation of DatabaseMetaData in the mysql driver

public class DatabaseMetaData implements java.sql.DatabaseMetaData lines....


1475    public int getDefaultTransactionIsolation() throws java.sql.SQLException JavaDoc {
1476        if (this.conn.supportsIsolationLevel()) {
1477            return java.sql.Connection.TRANSACTION_READ_COMMITTED;
1478        } else {
1479            return java.sql.Connection.TRANSACTION_NONE;
1480        }
1481    }

so i'd bet and trust getSession().connection().getTransactionIsolation()

3
  • 2
    How would you achieve the same thing with hibernate 4 since connection() has been deprecated?
    – emt14
    Commented May 2, 2012 at 5:31
  • 1
    Like this: ((((SessionFactoryImplementor)getSession().getSessionFactory()) .getJdbcServices().getConnectionProvider().getConnection().getTransactionIsolation() // method is also deprecated and causes "connection pool exausted" error, but working. I think actual method is: getSession.doWork(new Work() { @Override void execute(Connection conn) { /* working with conn */ }} ); But I 'm not checking if it's working during session or not.
    – Lord
    Commented Jun 28, 2013 at 15:20
  • In Hibernate 5 it's ((org.hibernate.engine.spi.SessionFactoryImplementor)session.getSessionFactory()).getJdbcServices().getBootstrapJdbcConnectionAccess().obtainConnection().getTransactionIsolation()
    – S. Doe
    Commented May 31, 2022 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.