我有一个具有以下配置的类测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ContextPersistenceTest.class)
在ContextPersistenceTest中,我有EntityManager的配置:
public @Bean(name = "entityManagerFactory") EntityManagerFactory entityManagerFactory {
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setPersistenceUnitName("unit-name");
...
}
我将实体经理注入:
@PersistenceUnit
EntityManager entityManager;
在我的@Test中,我连续两次打电话:
entityManager.createQuery(sql, MyClass.class);
令人惊讶的是,大多数情况下测试成功执行但有时entityManager
关闭以便第二次调用createQuery()
然后我得到一个:
PSQLException: The connection has been closed.