Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-5012

HibernateJpaDialect does not support setting a specific isolation level per transaction



    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 2.5.2
    • Fix Version/s: 4.1 RC2
    • Component/s: Core
    • Labels:
    • Last commented by a User:



      our problem is in unit testing. When we deploy under Weblogic and use the Weblogic transaction manager, everything is ok.
      But for unit testing, we use a LocalContainerEntityManagerFactoryBean with a HibernateJpaVendorAdapter injected into a JpaTransactionManager.

      When isolation level other than DEFAULT is required for a transaction (setted through annotations), an InvalidIsolationLevelException is thrown.

      I have solved this problem by writing a CustomHibernateJpaDialect in which I just delete the throw exception :

      public class CustomHibernateJpaDialect
      extends HibernateJpaDialect
      public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
      throws PersistenceException, SQLException,

      { entityManager.getTransaction().begin(); return null; }

      public void cleanupTransaction(Object transactionData)

      { // This line throws a NullPointerException. Got no time to go deeper but we never change the flush mode so I let it commented //((SessionTransactionData) transactionData).resetFlushMode(); }


      and a CustomHibernateJpaVendorAdapter in which I use my new CustomHibernateJpaDialect :

      public class CustomHibernateJpaVendorAdapter extends AbstractJpaVendorAdapter
      private final PersistenceProvider persistenceProvider = new HibernatePersistence();

      private final JpaDialect jpaDialect = new CustomHibernateJpaDialect();

      public PersistenceProvider getPersistenceProvider()

      { return this.persistenceProvider; }

      public Map getJpaPropertyMap() {
      Properties jpaProperties = new Properties();

      if (getDatabasePlatform() != null)

      { jpaProperties.setProperty(Environment.DIALECT, getDatabasePlatform()); }

      else if (getDatabase() != null) {
      Class databaseDialectClass = determineDatabaseDialectClass(getDatabase());
      if (databaseDialectClass != null)

      { jpaProperties.setProperty(Environment.DIALECT, databaseDialectClass.getName()); }


      if (isGenerateDdl())

      { jpaProperties.setProperty(Environment.HBM2DDL_AUTO, "update"); }

      if (isShowSql())

      { jpaProperties.setProperty(Environment.SHOW_SQL, "true"); }

      return jpaProperties;


      • Determine the Hibernate database dialect class for the given target database.
      • @param database the target database
      • @return the Hibernate database dialect class, or <code>null<code> if none found
        protected Class determineDatabaseDialectClass(Database database)
        Unknown macro: { switch (database) { case DB2: return DB2Dialect.class; case HSQL: return HSQLDialect.class; case INFORMIX: return InformixDialect.class; case MYSQL: return MySQLDialect.class; case ORACLE: return Oracle9Dialect.class; case POSTGRESQL: return PostgreSQLDialect.class; case SQL_SERVER: return SQLServerDialect.class; case SYBASE: return SybaseDialect.class; default: return null; } }

      public Class<? extends EntityManager> getEntityManagerInterface()

      { return HibernateEntityManager.class; }

      public JpaDialect getJpaDialect()

      { return this.jpaDialect; }


      I can't believe it's all we have to do to make it work correctly but I can go ahead in my unit testing.

      Is my isolation level really set (I want SERIALIZABLE) ?

      Thank you


          Issue Links



              juergen.hoeller Juergen Hoeller
              sam42fr Samuel Gaiffe
              Last updater:
              Spring Issues Spring Issues
              6 Vote for this issue
              11 Start watching this issue


                Days since last comment:
                3 years, 37 weeks, 1 day ago