Java Postgres限制了连接,如何关闭它!致命:抱歉,已经太多客户了



我需要发布与Postgres

的连接

我的Java应用程序与Postgres交互,我的问题是我需要调用此功能超过200个时间postgres崩溃,正常的事情...因为Postgres的连接有限,并且该脚本在每次执行时都会打开连接。

@Override
    public Object[] getExtrasInfosSource(int idSource) {
    StringBuffer sql = new StringBuffer();
        final SessionFactoryImplementor sfi = (SessionFactoryImplementor) getSessionFactory();
        final Settings settings = sfi.getSettings();
        // Appel du procedure stocké "getExtrasInfosForSource(idSource integer)" au niveau de bd
        sql.append("SELECT * from "+settings.getDefaultSchemaName()+".getExtras_Infos_For_Source(:idSource)");
        SQLQuery query = this.getSession().createSQLQuery(sql.toString());
        query.setInteger("idSource", idSource);
        return (Object[]) query.setMaxResults(1).uniqueResult();...

您可以告诉我如何关闭此脚本的Postgres中打开的连接,以避免每次加载Postgres连接时应用程序崩溃。

根据Hibernate的Javadocs,您可以在会话中调用.close()

close()

通过释放JDBC连接并清理结束会话。

在返回查询结果之前:this.getSession().close();

我通过将这些变量添加到我的服务器配置中解决了此问题,它刷新了indle connections(或so)在postgresql

中左右打开
<?xml version="1.0" encoding="utf-8"?>
<Context>
    <Resource name="jdbc/database"
              auth="Container"
              type="javax.sql.DataSource"
              driverClassName="org.postgresql.Driver"
              url="jdbc:postgresql://localhost:5432/DBA"
              username="DBA"
              password="DBA"
              initialSize="34"
              maxActive="80"
              maxIdle="5"
              timeBetweenEvictionRunsMillis="1000"
              minEvictableIdleTimeMillis="2000"
              validationQuery="SELECT 1"
              validationInterval="3000"
              testOnBorrow="true"
              removeAbandoned="true"
              removeAbandonedTimeout="3"/>  
</Context>

相关内容

最新更新