为什么我的DBUnit测试消耗了这么多内存?

时间:2009-07-10 13:19:19

标签: unit-testing hibernate hsqldb dbunit

我有一个基于hibernate的应用程序,它使用DBUnit进行单元测试。我们有一个XML测试数据库,它在每个测试的setUp()中加载了虚拟数据,并在tearDown()期间被删除。问题是我不能再在IDE中运行整个套件(在本例中为Intellij),因为在大约300次测试之后,堆内存全部用完了。测试从执行约0.3秒到30秒以上,直到JVM最终放弃并死亡。

当我通过ant的junit任务运行测试套件时,它没有问题,也没有运行单个类的测试套件。但是,我希望能够在检查代码库的大量重构更改之前在本地运行整个套件,而不是破坏CI服务器上的构建。

我正在使用-Xmx512m运行测试套件作为我对JVM的唯一参数,这与我在CI服务器上运行任务时传递给ant的数量相同。我的hibernate-test.cfg.xml看起来像这样:

<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:mem:mydatabase</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"/>

    <!-- Other configuration properties -->
    <property name="connection.pool_size">1</property>
    <property name="jdbc.batch_size">20</property>
    <property name="connection.autocommit">true</property>
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
    <property name="bytecode.use_reflection_optimizer">false</property>
    <property name="show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>

    <!-- Mappings (omitted for brevity) -->
    <mapping resource="hbm/blah.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

我们已经编写了一个类,所有的测试类都是从这个类扩展而来的,它看起来像这样:

package com.mycompany.test;
// imports omitted for brevity

public abstract class DBTestCase extends TestCase {

  private final String XML_DATA_SET = "test/resources/mytestdata.xml";
  private Session _session;
  private Configuration _config;

  public DBTestCase(String name) {
    super(name);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    _config = new Configuration().configure();
    SessionFactory sf = _config.buildSessionFactory();
    // This is a singleton which is used the DAO's to acquire a session.
    // The session must be manually set from the test's setup so that any
    // calls to the singleton return this session factory, otherwise NPE
    // will result, since the session factory is normally built during
    // webapp initialization.
    HibernateUtil.setSessionFactory(sf);
    _session = sf.openSession();
    _session.beginTransaction();

    IDataSet dataSet = new FlatXmlDataSet(new File(XML_DATA_SET));
    DatabaseOperation.CLEAN_INSERT.execute(getConnection(), dataSet);
  }

  protected void tearDown() throws Exception {
    super.tearDown();
    _session.close();
  }

  protected IDatabaseConnection getConnection() throws Exception {
    ConnectionProvider connProvider = ConnectionProviderFactory
      .newConnectionProvider(_config.getProperties());
    Connection jdbcConnection = connProvider.getConnection();
    DatabaseConnection dbConnection = new DatabaseConnection(jdbcConnection);
    DatabaseConfig dbConfig = dbConnection.getConfig();
    dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
    return dbConnection;
  }
}

很明显,这里发生了一些内存泄漏,但我不知道在哪里。我该如何诊断?

2 个答案:

答案 0 :(得分:2)

您在这里使用内存数据库:

<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:mydatabase</property>

这意味着数据库中的所有内容都在内存中。使用带有缓存表的磁盘数据库,或确保在每次测试后丢弃所有内容。

答案 1 :(得分:0)

J-16 SDiZ的回答让我朝着正确的方向努力,但我想我会提供更详细的信息,说明我是如何解决这个问题的。问题的根源确实是数据库保存在内存中,但解决方案是继承自DBUnit的DBTestCase类,而不是尝试通过继承JUnit TestCase来自行创建。我的测试用例基类现在看起来像这样:

public class MyTestCase extends DBTestCase {
  private static Configuration _config = null;

  public MyTestCase(String name) {
    super(name);
    if(_config == null) {
      _config = new Configuration().configure();
      SessionFactory sf = _config.buildSessionFactory();
      HibernateUtil.setSessionFactory(sf);
    }

    System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.hsqldb.jdbcDriver");
    System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:hsqldb:mem:mydbname");
    System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "sa");
    System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "");
  }

  @Override
  protected IDataSet getDataSet() throws Exception {
    return new FlatXmlDataSet(new FileReader(MY_XML_DATA_FILE_NAME), false, true, false);
  }

  @Override
  protected void setUpDatabaseConfig(DatabaseConfig config) {
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
  }

这个课程效果很好,我的测试套件运行时间从几分钟减少到仅仅30秒。