为什么不被骗:
我认为这不是重复的问题,因为我不是在寻找用异常本身编写的解决方案。我正在寻找澄清实际情况以及为什么引发此异常。链接的帖子对此没有任何评论。
我想避免使用“生产”数据库来运行测试,所以我使用内存数据库进行测试。我为dao函数编写了一个简单的测试,它通过了,但是在完成每个测试之后,还会引发异常。该异常不会通过任何测试,但是我真的不知道是什么原因(它没有指向我编写的代码中的任何地方)以及它是否可能导致任何不良行为。我应该忽略它吗?
例外:
org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-194]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.194.jar:1.4.194]
at org.h2.message.DbException.get(DbException.java:179) ~[h2-1.4.194.jar:1.4.194]
at org.h2.message.DbException.get(DbException.java:155) ~[h2-1.4.194.jar:1.4.194]
at org.h2.message.DbException.get(DbException.java:144) ~[h2-1.4.194.jar:1.4.194]
at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1484) ~[h2-1.4.194.jar:1.4.194]
at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1459) ~[h2-1.4.194.jar:1.4.194]
at org.h2.jdbc.JdbcConnection.getAutoCommit(JdbcConnection.java:458) ~[h2-1.4.194.jar:1.4.194]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126) ~[tomcat-jdbc-8.5.14.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.14.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.14.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.14.jar:na]
at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81) ~[tomcat-jdbc-8.5.14.jar:na]
at com.sun.proxy.$Proxy75.getAutoCommit(Unknown Source) ~[na:na]
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:34) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:35) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:425) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:375) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:371) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1081) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:347) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:459) [spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:630) [spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at com.sun.proxy.$Proxy91.close(Unknown Source) [na:na]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.destroy(AbstractEntityManagerFactoryBean.java:550) [spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:272) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1009) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext$2.run(AbstractApplicationContext.java:928) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
测试:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class ClientDaoTests {
@Autowired
private ClientDao clientDao;
@Test
public void initTest() {
assertNotNull(clientDao);
}
@Test
@Transactional
public void findByEmailTest() {
assertNull(clientDao.findByEmail("notInDb@mail.com"));
Client client = new Client("inDb@mail.com", null, null);
clientDao.save(client);
assertEquals(client,clientDao.findByEmail(client.getEmail()));
}
}
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/foobar
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=foo
spring.datasource.password=bar
spring.jpa.hibernate.ddl-auto=create-drop
application-test.properties:
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop