Announcement Announcement Module
Collapse
No announcement yet.
Spring+Hibernate+dbUnit, clean up DB hangs up after tests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring+Hibernate+dbUnit, clean up DB hangs up after tests



    Hi.

    I have quite strange problem.

    When during dbUnit test, my service is changing db and I want to clean up, the whole tests hangs up. When my tests reads only some data from db, clean up working ok.

    Here is how it looks:
    test code:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations= {"classpath:/META-INF/document-config-local.xml"})
    class Test {
     @Inject
        DocumentsServices documentService;
    @Test
        public void saveModuleTest() throws Exception{
            VpsModule module=  new VpsModule();    
            VpsModule savedModule = documentService.saveModule(module);
            Assert.assertEquals(module.getName(), savedModule.getName());
        }
    Here are supported methods:
    Code:
      @Before
        public void prepareDB() {
            try {
                dataSet = loadAndWriteToDB(DB_NAME, PREPARE_DB_FILE);
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail();
            }
        }
        @After
        public void cleanDB() {
            try {
                cleanUp(DB_NAME, "src/test/resources/META-INF/prepare_db_documents_service_bean_cleanup.xml");
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail();
            }
        }
    
     protected IDataSet loadAndWriteToDB(final String db_name,final String inputFilePath)  throws Exception {
           Connection con = DataSourceUtils.getConnection(((JdbcTemplate)simpleJdbcTemplate.getJdbcOperations()).getDataSource());
          dbUnitCon = new DatabaseConnection(con,db_name);
           con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
           IDataSet dataSet = loadDataSet(db_name, inputFilePath);
           try {
               DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               DataSourceUtils.releaseConnection(con, ((JdbcTemplate)simpleJdbcTemplate.getJdbcOperations()).getDataSource());
           }
           return dataSet;
       }
    
      protected void cleanUp(final String db_name, final String inputFilePath) {
            Connection con = DataSourceUtils.getConnection(((JdbcTemplate) simpleJdbcTemplate.getJdbcOperations()).getDataSource());
            try {
                IDatabaseConnection dbUnitCon;
                dbUnitCon = new DatabaseConnection(con, db_name);
                IDataSet dataSet;
                dataSet = loadDataSet(db_name, inputFilePath);
                DatabaseOperation.DELETE_ALL.execute(dbUnitCon, dataSet);
            } catch (DatabaseUnitException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                DataSourceUtils.releaseConnection(con, ((JdbcTemplate) simpleJdbcTemplate.getJdbcOperations()).getDataSource());
            }
        }
    }
    and when after test dbUnit call cleanUp it hangs up on

    DatabaseOperation.DELETE_ALL.execute(dbUnitCon, dataSet);
    document-config-local.xml
    Code:
      <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="emf"/>
      </bean>
     
      <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
       <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence-test-local.xml" />
      </bean>
     
      <tx:annotation-driven />
     
      <bean class="com.volvo.jvs.vps.documents.b.beans.DocumentsServicesBean" id="documentService" />
       <import resource="classpath:dataSourcesLocal.xml" />
    dataSourcesLocal.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
      <context:property-placeholder location="classpath:dataSources.properties" />
    
      <!-- Default, standalone DataSources configuration -->
    
      <bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <property name="targetDataSource">
          <bean class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
            <property name="uniqueResourceName" value="vtec8d_Resource" />
            <property name="maxPoolSize" value="8" />
            <property name="xaDataSourceClassName" value="${XADataSourceClassName}" />
            <property name="xaProperties">
              <props>
                <prop key="URL">${URL}</prop>
                <prop key="user">${user}</prop>
                <prop key="password">${password}</prop>
              </props>
            </property>
          </bean>
        </property>
      </bean>
    </beans>
    persistence-test-local.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    
      <persistence-unit name="DocumentsDomainPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <mapping-file>META-INF/DocumentsDomainLocal_orm.xml</mapping-file>
        <!--Class for DocumentsServicesTest  -->
    <class>xxxxx</class>
        <exclude-unlisted-classes/>
        <properties>
          <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
          <property name="hibernate.connection.url" value="jdbc:oracle:thin:@xx.xx.xx.xx:1521/vps_test_plug"/>                                 
          <property name="hibernate.connection.username" value="TEST"/>
          <property name="hibernate.connection.password" value="test"/>
          <property name="hibernate.show_sql" value="false"/>
          <property name="hibernate.format_sql" value="true"/>
          <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        </properties>
      </persistence-unit>
     
    </persistence>

    After hangs up I have to reset DB service. It looks like clean up doesn't work, because my service change db. I also try to use different xml for clean up than for creation but still the same problem.

    How to resolve this problem?Any idea?
    Thank you for help

  • #2
    Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
    We need to clean this forum (old unanswered question) before close it.

    Thanks for understanding

    Comment

    Working...
    X