简体   繁体   English

如何在 Hibernate 中禁用连接池

[英]How to disable connection pooling in Hibernate

I have a web application that currently uses c3p0 and Hibernate to connect to a Firebird 1.5 database.我有一个 web 应用程序,它当前使用 c3p0 和 Hibernate 连接到 Firebird 1.5 数据库。

I am facing a problem from time to time where the database just stops responding, even trying to manually restart the service doesn't have any effect, and it doesn't generate any logs, so I have to manually reboot the machine to get it working again.我不时遇到一个问题,数据库只是停止响应,即使尝试手动重新启动服务也没有任何效果,并且它不会生成任何日志,所以我必须手动重新启动机器才能获得它再次工作。

I think that maybe Firebird hangs when the pool tries to acquire a specific number of connections or something like that.我认为当池尝试获取特定数量的连接或类似的东西时,Firebird 可能会挂起。 So, I need to test my app without connection pooling, to check if this is or is not the problem.所以,我需要在没有连接池的情况下测试我的应用程序,以检查这是否是问题。

I can't simply remove c3p0 configs from persistence because this way Hibernate would use its own integrated connection pool.我不能简单地从持久性中删除 c3p0 配置,因为这样 Hibernate 将使用自己的集成连接池。 So how to do it?那么该怎么做呢?

The most flexible solution is to use an explicit DataSource , instead of configuring the connection pooling through Hibernate. 最灵活的解决方案是使用显式DataSource ,而不是通过Hibernate配置连接池。 One option to configure a non-pooling DataSource is by using DriverManagerDataSource : 配置非池化DataSource一个选项是使用DriverManagerDataSource

@Override
protected Properties getProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    //log settings
    properties.put("hibernate.hbm2ddl.auto", "update");
    //data source settings
    properties.put("hibernate.connection.datasource", newDataSource());
    return properties;
}

protected ProxyDataSource newDataSource() {
    DriverManagerDataSource actualDataSource = new DriverManagerDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUsername("sa");
    actualDataSource.setPassword("");
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}

This way you can choose a pooling or a non-pooling DataSource . 这样您就可以选择池化或非池化DataSource

To get a better understanding of you connection pooling resources usage, you can configure FlexyPool to collect metrics for: 要更好地了解连接池资源使用情况,可以配置FlexyPool以收集以下指标:

  • concurrent connections 并发连接
  • concurrent connection requests 并发连接请求
  • data source connection acquiring time 数据源连接获取时间
  • connection lease time 连接租约时间
  • maximum pool size 最大泳池大小
  • total connection acquiring time 总连接获取时间
  • overflow pool size 溢出池大小
  • retries attempts 重试尝试

I found documentation for hibernate 3.3 and 4.3 that says: 我找到了hibernate 3.34.3的文档说:

Just replace the hibernate.connection.pool_size property with connection pool specific settings. 只需用连接池特定设置替换hibernate.connection.pool_size属性即可。 This will turn off Hibernate's internal pool. 这将关闭Hibernate的内部池。

Hibernate will use its org.hibernate.connection.C3P0ConnectionProvider for connection pooling if you set hibernate.c3p0.* properties 如果设置hibernate.c3p0。*属性,Hibernate将使用其org.hibernate.connection.C3P0ConnectionProvider进行连接池

So remove hibernate.connection.pool_size and any hibernate.c3p0... properties from configuration, than connection pooling is disabled. 因此,从配置中删除hibernate.connection.pool_size和任何hibernate.c3p0 ...属性,而不是禁用连接池。

Adding to Vlad's answer:添加到弗拉德的答案:

If somebody still faces this:如果有人仍然面临这个问题:

  • Be sure to remove " hibernate-c3p0 " from your classpath, if exists, since this will automatically enable MChange c3p0 connection pool.如果存在,请务必从类路径中删除“ hibernate-c3p0 ”,因为这将自动启用 MChange c3p0 连接池。

  • Another option that, you can close the connection manually when closing the entity manager:另一个选项,您可以在关闭实体管理器时手动关闭连接:

     .... SessionImpl ses = (SessionImpl) session; close(ses.connection()); try { session.close(); } catch (Exception e) { logger.error(e); }........

Note : the above manual closing will work with the default pool of hibernate, not hibernate default one.注意:上述手动关闭将使用默认池 hibernate,而不是 hibernate 默认池。

Good Luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM