我有一個在服務類中調用的EntityManager bean對象作爲自動裝配對象。連接丟失後自動重新連接JPA EntityManager
Spring配置類:
@EnableWebMvc
@Configuration("myWebConfiguration")
@ComponentScan(basePackages = {"org.my.app"})
@EnableScheduling
public class MyWebConfiguration extends WebMvcConfigurerAdapter {
....
private static EntityManager entityManager;
@Bean
public EntityManager entityManager() {
if (entityManager == null) {
entityManager = managerFactoryBean().getObject().createEntityManager();
}
return entityManager;
}
....
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
return properties;
}
....
}
樣品服務類:
@Service("sampleService")
public class SampleService {
@Autowired
protected EntityManager entityManager;
public Object find(Class entityClass, Object id) {
return entityManager.find(entityClass, id);
}
....
}
而且問題:
如果Web應用程序服務器之間的連接和DB服務器丟失,JPA和spring無法重新連接到數據庫服務器,並且調用entityManager方法會導致異常,如org.postgresql.util.PSQLException: This connection has been closed.
和org.hibernate.exception.JDBCConnectionException: could not inspect JDBC autocommit mode
。
是否可以自動檢測連接丟失並在連接丟失的情況下重新建立連接?
謝謝,我已經添加了休眠屬性部分答案。我使用c3p0,但我沒有配置它。我需要添加哪個配置? –
@BabakBehzadi更新了c3p0屬性的答案。你可以試試這些。 –