What is default isolation level hibernate uses if not explicitly set?

Argyro Kazaki picture Argyro Kazaki · Jun 26, 2011 · Viewed 27.3k times · Source

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?

Answer

Pantelis Nasikas picture Pantelis Nasikas · Jun 27, 2011

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