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

家住魔仙堡 提交于 2020-01-22 05:14:07

问题


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:


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



来源:https://stackoverflow.com/questions/6484332/what-is-default-isolation-level-hibernate-uses-if-not-explicitly-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!