在Spring Boot中使用Hibernate SessionFactory连接泄漏

时间:2017-05-16 20:32:47

标签: java postgresql hibernate spring-boot

我有一个Add(expr1, expr2)(1.5.3)应用程序,我在其中自动装配Const(5.0.12)Const,如下所示:

Spring Boot

Hibernate

在配置类中:

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; 进行简单的SQL查询,我99.9%肯定在返回结果之前在关联的PostgreSQL中关闭它们。

我有未经证实的感觉,早期的Spring Boot版本和引入的依赖项,问题并不存在。虽然不确定......

这些明显泄漏的原因是什么?

为了完整性,这里有一些示例用于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]

StatelessSession

2 个答案:

答案 0 :(得分:1)

我可以解决"通过更改配置进行泄漏如下(不确定所有这些步骤是否必要):

application.properties:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate5.SpringSessionContext

配置类:

添加@EnableAutoConfiguration,以及:

@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
    HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
    factory.setEntityManagerFactory(emf);
    return factory;
}

这个答案很有帮助:

required a bean of type 'org.hibernate.SessionFactory' that could not be found

答案 1 :(得分:0)

我相信您需要设置以下属性:

spring.datasource.max-active

e.g。spring.datasource.max-active=5

看起来弹簧连接池打开的连接数多于postgresql.conf文件中允许的连接数