当前位置: 首页 > 知识库问答 >
问题:

Spring Boot中使用Hibernate SessionFactory进行连接泄漏

应俭
2023-03-14
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

在configuration类中:

@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]

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

为了完整起见,这里的一些示例在@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;
}

共有1个答案

沈永新
2023-03-14

我可以通过如下方式更改配置来“解决”泄漏(但不确定所有这些步骤都是必需的):

应用程序.属性:

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;
}
 类似资料:
  • 我第一次使用带有IISExpress的新Visual Studio 2013(以前 ASP.net VS2010上的开发服务器使用)。我在尝试调试项目时遇到问题。 这是我在Chrome看到的: 无法与服务器建立安全连接。这可能是服务器的问题,也可能需要您没有的客户端身份验证证书。错误代码:ERR_SSL_PROTOCOL_ERROR 我更新了我的计划—— 与localhost的连接中断。错误代码:

  • 我正在开发一个在Java服务器上运行的游戏。对于数据库池,我使用的是HikariCP,这是一个优秀的库,但它现在抛出了以下错误: 现在我知道连接泄漏意味着打开的连接在某个地方漂浮,但我不知道如何或在哪里漂浮。 下面是我的数据库类中的一个方法,根据堆栈跟踪,错误应该发生在这里。 这只是启动语句的一个基本方法。调用它时,我使用它,然后调用、和 但它告诉我连接是打开的。 我怎么解决这个?谢了!

  • 我在做java企业应用。该应用程序后端有hibernate框架。由于应用程序中最近的更改,一些代码消耗了weblogic server中的所有JDBC连接池。 应用程序连接是在代码中进行属性处理,对于每个线程,我们使用threadlocal类创建每个会话。所以创建连接没有问题。该应用程序已存在5年以上。 我们怀疑最近的代码更改会导致此主要问题。最后,我们决定使用探查器工具来调查此问题。 在此之前,

  • 问题内容: 我目前正在使用NEST ElasticSearch C#库与ElasticSearch进行交互。我的项目是一个MVC 4 WebAPI项目,该项目基本上构建了一个RESTful Web服务来访问目录服务信息。 我们才刚刚开始使用NEST,并且由于缺乏文档而陷入困境。那里有用,但是有一些很大的孔。当前,我们需要的所有东西都可以正常工作,但是,我们遇到了一个问题,有时连接可能需要一整秒的时

  • 问题内容: 我在使用时遇到了麻烦,因为我之前从未见过它,也不知道我在用它做什么。 基本上,我正在关闭部队,因为我试图在主类上运行连接。有人可以帮我添加代码吗: 问题答案: 只是一个简单的例子,它看起来像: