1 Reply Latest reply on Dec 5, 2008 12:20 AM by charlf

    problem with testing: hibernate schema update -> can't commi

    gedel

      Hello

      I write tests for domain persistence layer implemented on Hibernate.
      I use JBoss microcontainer for the following purposes:
      1. provide Datasources through JNDI
      2. provide UserTransaction through JNDI

      my tests fork fine. But I want to create database schema on every test run (I use HSQLDB in embedded mode). Schema update is unavailable, because I use LocalTxDataSources which use "test managed" UserTransaction.

      stack trace:

      SchemaUpdate - Running hbm2ddl schema update
      SchemaUpdate - fetching database metadata
      SchemaUpdate - could not get database metadata
      java.sql.SQLException: You cannot set autocommit during a managed transaction!
       at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:482)
       at org.jboss.resource.adapter.jdbc.WrappedConnection.setAutoCommit(WrappedConnection.java:322)
       at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:36)
       at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:127)
       at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)


      hibernate.cfg.xml
       <property name="connection.datasource">java:comp/env/jdbc/XxxDataSource</property>
       ....
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
       <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
       <property name="hibernate.transaction.flush_before_completion">true</property>
       <property name="hibernate.transaction.auto_close_session">true</property>
       ...
      <property name="hibernate.hbm2ddl.auto">update</property>


      jboss-beans.xml with several datasources:
       <bean name="MasterDSBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
       <property name="driverClass">org.hsqldb.jdbcDriver</property>
       <property name="connectionURL">jdbc:hsqldb:file:test-output/master</property>
       <property name="userName">sa</property>
       <property name="jndiName">java:comp/env/jdbc/XxxDataSource</property>
      
       <bean name="SlaveDSBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
       <property name="driverClass">org.hsqldb.jdbcDriver</property>
       <property name="connectionURL">jdbc:hsqldb:file:test-output/slave</property>
       <property name="userName">sa</property>
       <property name="jndiName">java:comp/env/jdbc/YyyDataSource</property>
      


      "problem code" from SchemaUpdate.execute
       log.info("fetching database metadata");
       connection = connectionProvider.getConnection();
       if ( !connection.getAutoCommit() ) { <<<---- i can return true there through extending datasource classes
       connection.commit();
       connection.setAutoCommit(true); <<< --- exception throwing there
       autoCommitWasEnabled = false;
       }
      

      this code from hibernate 3.0.5, the same (but with insignificant differences) in 3.2

      What are you think about this? Which is the simplest way to solve this issue? Should I use nonTx Datasource's ? May be I have to extends LocalTxDataSource and WrappedConnection for return AutoCommit true always?

      thanks for your attention

        • 1. Re: problem with testing: hibernate schema update -> can't c
          charlf

          I also got the same error, I fixed it, what you do is put the following properties in the persistence.xml

          <property name="hibernate.transaction.flush_before_completion" value="true"/>
          <property name="hibernate.transaction.auto_close_session" value="true"/>
          


          and then you put this property in your Spring Configuration, for the EntityManagerFactory

          <property name="jpaVendorAdapter">
           <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="database" value="MYSQL" />
           <property name="showSql" value="false" />
           <property name="generateDdl" value="false"/>
           </bean>
          </property>
          


          This seemed to solve the problem