2013-12-19 94 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