【发布时间】:2017-10-16 02:26:54
【问题描述】:
我有一个Spring Boot (1.5.3) 应用程序,我在其中自动连接Hibernate (5.0.12) SessionFactory,如下所示:
在application.properties:
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
在配置类中:
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
在@Services:
@Autowired
private SessionFactory sessionFactory;
这很好用。但我的问题是,在每次请求之后,我得到越来越多的空闲 PostgreSQL 进程,并且在多次请求之后,最终我得到了这个:
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
...
Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
...
[ERROR] [16.05.17 20:19:39] DirectJDKLog.java:181 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction;
nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
...
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
...
Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
...
[ERROR] [16.05.17 20:19:39] DirectJDKLog.java:181 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction;
nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
...
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
...
Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
...
[ WARN] [16.05.17 20:19:40] SqlExceptionHelper.java:127 SQL Error: 0, SQLState: 53300
[ERROR] [16.05.17 20:19:40] SqlExceptionHelper.java:129 FATAL: remaining connection slots are reserved for non-replication superuser connections
[ERROR] [16.05.17 20:19:40] DirectJDKLog.java:181 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection] with root cause
org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
我在简单的 SQL 查询中经常使用StatelessSession,并且我有 99.9% 的把握在返回结果之前在相关的@Service 中关闭它们。
我有一种未确认的感觉,对于早期的 Spring Boot 版本和引入的依赖项,问题并不存在。不过不确定...
这些明显泄漏的原因可能是什么?
为了完整起见,这里有一些示例在 @Service: 中使用:
@Transactional(readOnly = true)
@Cacheable(value = "countries", key = "#root.methodName")
public List<Country> getCountries() {
final StatelessSession session = sessionFactory.openStatelessSession();
final Query query = session.createQuery("from Country order by id");
final List<Country> result = query.list();
session.close();
return result;
}
@Transactional(readOnly = true)
public long countTimeZones() {
final StatelessSession session = sessionFactory.openStatelessSession();
final Long result = (Long) session.createQuery("select count(o) from TimeZone o").uniqueResult();
session.close();
return result;
}
@Transactional(readOnly = true)
public List<Map<String, Object>> getPhotoAlbums() {
final StatelessSession session = sessionFactory.openStatelessSession();
final SQLQuery query = session.createSQLQuery("select "
+ "cast(m.id as varchar), "
+ "m.name "
// etc
+ "from media_album m "
+ "where m.account = :account "
+ "and ... "
+ "order by ...");
query.setParameter("account", uuidOfAccount);
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
final List<Map<String, Object>> result = query.list();
session.close();
return result;
}
【问题讨论】:
标签: java postgresql hibernate spring-boot