情况:我正在使用Hibernate和Maven surefire为DAO类编写单元测试。我们决定在单元测试之前从test_template创建一个test_db,并在测试之后删除test_db。
完成代码并使用mvn test测试之后,当我仅测试一个测试类时,它成功了:mvn test -Dtest = com.package.DummyTest 但是当我测试所有测试类时失败:mvn test -Dtest = com.package。* Test
setup(),teardown()和休眠查询方法以及错误堆栈跟踪如下所示:
setUp()
@BeforeClass
public static void setUpJdbc() throws IOException, InterruptedException, SQLException {
Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/default_db", "user_name", "pw");
Statement statement = c.createStatement();
statement.executeUpdate("create database test_db with template test_template;");
statement.close();
c.close();
}
tearDown()
@AfterClass
public static void tearDownJdbc() throws SQLException {
Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/default_db", "user_name", "pw");
Statement statement = c.createStatement();
statement.executeUpdate("REVOKE CONNECT ON DATABASE test_db FROM public;");
statement.execute("SELECT pid, pg_terminate_backend(pid) \n" +
"FROM pg_stat_activity \n" +
"WHERE datname = 'test_db' AND pid <> pg_backend_pid();");
statement.executeUpdate("drop database if exists test_db;");
statement.close();
c.close();
}
休眠查询方法:
public List<Object> query(String sqlquery) throws DatabaseConnectionException {
Session session = null;
Transaction transcation = null;
List<Object> sqlQueryResults = null;
try {
session = factory.openSession();
transcation = session.beginTransaction();
Query query = session.createNativeQuery(sqlquery);
sqlQueryResults = query.list();
transcation.commit();
} catch (SQLGrammarException e) {
if (transcation != null)
transcation.rollback();
throw new DatabaseConnectionException();
} catch (HibernateException e) {
if (transcation != null)
transcation.rollback();
throw new DatabaseConnectionException();
} finally {
if (session != null)
session.close();
}
return sqlQueryResults;
}
我不知道这是怎么回事。看来我无法正常运行session.close()。
org.hibernate.exception.JDBCConnectionException: Unable to release JDBC Connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.releaseConnection(LogicalConnectionManagedImpl.java:199)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.close(LogicalConnectionManagedImpl.java:239)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:189)
at org.hibernate.internal.AbstractSharedSessionContract.close(AbstractSharedSessionContract.java:311)
at org.hibernate.internal.SessionImpl.close(SessionImpl.java:423)
at com.dummy.DataLayer.query(DataLayer.java:173)
at com.dummy.MITypeCompositeDAO.getLatestExecutionForUser(MITypeCompositeDAO.java:71)
at com.dummy.MITypeCompositeDAO.getMITypeCompositeForUser(MITypeCompositeDAO.java:137)
at com.dummy.MITypeCompositeDAOTest.testGetMITypeCompositeForUser(MITypeCompositeDAOTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
Caused by: org.postgresql.util.PSQLException: This connection has been closed.
at org.postgresql.jdbc.PgConnection.checkClosed(PgConnection.java:766)
at org.postgresql.jdbc.PgConnection.setAutoCommit(PgConnection.java:712)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.add(PooledConnections.java:68)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.closeConnection(DriverManagerConnectionProviderImpl.java:195)
at org.hibernate.internal.NonContextualJdbcConnectionAccess.releaseConnection(NonContextualJdbcConnectionAccess.java:46)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.releaseConnection(LogicalConnectionManagedImpl.java:196)
... 35 more
情况有点复杂,所以如果您需要更多信息,请告诉我。
我愿意接受任何意见和建议,并且愿意尝试您提供的所有内容。预先感谢!