2013-12-19 164 views
0

我有一些测试类,它们启动spring上下文,每个包含hibernate(和EntityManagerFactory)。春季测试:UnknownServiceException:ConnectionProvider

自定义TestExecutionListener确保弹簧上下文总是被标记为脏的,所以上下文不应该被重用。

在一个Spring上下文(不是第一次启动时)我得到的后续启动:

org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] 

什么会导致此异常?之前的春季环境是否未正式关闭?

编辑/添加:上面的异常实际上是在启动过程中尝试在bean中启动事务时抛出的。 (Lifecycle.start())。所以问题是为什么在应该存在时没有可用的SessionFactory - 并且创建了第一个Spring测试上下文(用于第一个测试类)。

回答

0

原来这是由旧的Spring上下文引用泄漏(定制代码)引起的。

0

的异常可能是由试图创建一个新的会话,而SessionFactory的关闭

确保您不会调用非事务性测试事务方法造成的。

test context:

In the TestContext framework, transactions are managed by the TransactionalTestExecutionListener, which is configured through the @TestExecutionListeners annotation by default, even if you do not explicitly declare @TestExecutionListeners on your test class. To enable support for transactions, however, you must provide a PlatformTransactionManager bean in the application context loaded by @ContextConfiguration semantics. In addition, you must declare @Transactional either at the class or method level. 

TransactionalTestExecutionListener:

... test methods that are not annotated with either @Transactional (at the class or method level) or @NotTransactional will not be run within a transaction. 
+0

你的意思是@Transactional应该放在所有可能使用数据库的测试上。这些注释已经存在于豆类的适当位置。 我不明白为什么SessionFactory应该在加载Spring Context并且当前正在刷新(启动)时关闭。 –

+0

我正在谈论AbstractTransactionalJUnit4SpringContextTests。 – jonasnas

+0

我已经更新了我的答案,包括来自api文档的文本 – jonasnas