失败的dbunit测试使事务处于打开状态

时间:2015-07-12 19:35:58

标签: java hibernate transactions dbunit

我已经设置了dbunit(2.4.9)来处理我的hibernate(4.2.19)代码。一切都运行良好,直到插入过程中抛出异常。测试用例失败(正确)但db事务处于打开状态,我无法关闭它。

这是我从dbUnits DbTestCase扩展的基本测试用例。我猜我的泪水方法没有被调用。

public class DbTestCase extends DBTestCase
{
 private static boolean doOnce = false;
 private Session session;

 public DbTestCase() throws Exception
 {
    super();
    //This connection info is duplicated in dbu.hib.cfg.xml
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "oracle.jdbc.driver.OracleDriver" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:oracle:thin:@host:1521:SID" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "schema" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "psswd" );

}

@Override
protected void setUp() throws Exception
{
    super.setUp();
    if(!doOnce)
    {
        doOnce = true;

        BasicConfigurator.configure();

        EbigHibernateFactory.setImplementation(new DbUnitHibUtils());
        EbigHibernateFactory.initilize();
    }

    session = EbigHibernateFactory.getSession();
    session.beginTransaction();
}

public void tearDown() throws Exception
{  
    session.getTransaction().commit();
    if(session.isOpen())
    {
        session.close();  
    }
    super.tearDown();  
}  

@Override
protected IDataSet getDataSet() throws Exception
{
    return new FlatXmlDataSetBuilder().build(new FileInputStream("src/dbtest/dataset.xml"));
}

}

0 个答案:

没有答案