Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Postgres空闲连接:CannotAcquireResourceException_Spring_Hibernate_Postgresql - Fatal编程技术网

Spring Postgres空闲连接:CannotAcquireResourceException

Spring Postgres空闲连接:CannotAcquireResourceException,spring,hibernate,postgresql,Spring,Hibernate,Postgresql,我有一个Java/Wicket的web应用程序,使用Hibernate4.1.1和Spring3.1.1以及Postgres9.2。我还使用spring事务api和c3p0连接池 我的问题是,没有释放可用的连接,在执行一些由gui引起的select语句后,我得到以下异常 com.mchange.v2.resourcepool.CannotAcquireResourceException:A ResourcePool无法从其主工厂或服务器获取资源 来源。从pg_stat_活动中执行SELECT*显

我有一个Java/Wicket的web应用程序,使用Hibernate4.1.1和Spring3.1.1以及Postgres9.2。我还使用spring事务api和c3p0连接池

我的问题是,没有释放可用的连接,在执行一些由gui引起的select语句后,我得到以下异常

com.mchange.v2.resourcepool.CannotAcquireResourceException:A ResourcePool无法从其主工厂或服务器获取资源 来源。从pg_stat_活动中执行
SELECT*显示有
有许多空闲查询是这样的:
选择NULL作为表\u CAT,n.nspname
作为表(SCHEM),c.relname作为表(NAME),CASE n.nspname~“^pg”或
n、 nspname='information\u schema'如果为true,则为CASE如果为n.nspname
='pg_catalog'或n.nspname='information_schema'然后CASE c.relkind当'r'时,然后'SYSTEM TABLE'当'v'时,然后'SYSTEM VIEW'当'i'
当n.nspname='pg_toast'时,则'SYSTEM INDEX'ELSE NULL结束
然后案例c.relkind当'r'时,然后'SYSTEM TOAST TABLE'当'i'
然后“系统TOAST INDEX”ELSE NULL END ELSE案例c.relkind
当'r'然后'TEMPORARY TABLE'当'i'然后'TEMPORARY INDEX'时
当'S'然后'TEMPORARY SEQUENCE'当'v'然后'TEMPORARY VIEW'时
ELSE NULL END END END当为false时,则为案例c。relkind当为'r'时,则为 当'i'时为'TABLE',当'S'时为'INDEX',当'v'时为'SEQUENCE' 然后是“查看”,当是“c”,然后是“键入”ELSE NULL END ELSE NULL END AS 表类型,d.说明,如pg_catalog.pg_名称空间n中的备注所示, pg_catalog.pg_c类左连接pg_catalog.pg_说明d ON(c.oid =d.objoid和d.ob

这是我的applicationContext.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd"
       default-autowire="byName">


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                    destroy-method="close">
           <property name="driverClass" value="org.postgresql.Driver"/>
           <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/mydatabase"/>
           <property name="user" value="myuser"/>
           <property name="password" value="mypassword"/>

           <!-- configuration pool via c3p0 -->
           <property name="initialPoolSize" value="3"/>
           <property name="minPoolSize" value="3"/>
           <property name="maxPoolSize" value="30"/>
           <property name="idleConnectionTestPeriod" value="600"/>
           <property name="maxIdleTime" value="0"/>
           <property name="maxStatements" value="0"/>
           <property name="maxStatementsPerConnection" value="0"/>
           <property name="acquireIncrement" value="1"/>
           <property name="acquireRetryAttempts" value="3"/>
           <property name="acquireRetryDelay" value="1000"/>
           <property name="autoCommitOnClose" value="false"/>
           <property name="maxConnectionAge" value="14400"/>
           <property name="forceIgnoreUnresolvedTransactions" value="false"/>
           <property name="numHelperThreads" value="20"/>
           <property name="testConnectionOnCheckin" value="false"/>
           <property name="testConnectionOnCheckout" value="false"/>
           <property name="maxAdministrativeTaskTime" value="0"/>
           <property name="debugUnreturnedConnectionStackTraces" value="false"/>
           <property name="maxIdleTimeExcessConnections" value="0"/>
           <property name="breakAfterAcquireFailure" value="false"/>
           <property name="checkoutTimeout" value="0"/>
           <property name="unreturnedConnectionTimeout" value="0"/>
           <property name="usesTraditionalReflectiveProxies" value="false"/>
      </bean>

    <bean id="exampleDao"
        class="com.mycompany.persistence.hibernate.daoimpl.ExampleDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
        <list>
            <value>com.mycompany.model.Example</value>
        </list>
      </property>

      <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
            <prop key="hibernate.auto_close_session">true</prop>
            <prop key="hibernate.c3p0.maxSize">30</prop>
            <prop key="hibernate.c3p0.minSize">1</prop>
            <prop key="hibernate.c3p0.acquireIncrement">1</prop>
            <prop key="hibernate.c3p0.idleTestPeriod">300</prop>
            <prop key="hibernate.c3p0.timeout">1800</prop>
            <prop key="hibernate.c3p0.maxStatements">0</prop>
            <prop key="hibernate.c3p0.unreturnedConnectionTimeout">30</prop> 
            <prop key="hibernate.c3p0.debugUnreturnedConnectionStackTraces">true</prop>
            <prop key="hibernate.c3p0.checkoutTimeout">0</prop>
            <prop key="hibernate.c3p0.preferredTestQuery">SELECT * FROM dual</prop>
        </props>
      </property>
    </bean>

   <!-- Transaction Management -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

com.mycompany.model.Example
org.hibernate.dialogue.PostgreSqlDialogue
真的
真的
org.springframework.orm.hibernate4.SpringSessionContext
真的
30
1.
1.
300
1800
0
30
真的
0
从双重选择中选择*
配置是否正确

我从数据库检索数据的类如下所示:

@Repository
public class ExampleDaoImpl extends BaseDaoImpl implements ExampleDao {
...
    @Override
    @Transactional(readOnly=true)
    public List<Example> findExampleByCompanyAndType(Company company,
            int type) {
        return sessionFactory.getCurrentSession()
                .createCriteria(Example.class)
                .add(Restrictions.eq("company.companyId",
                        company.getCompanyId()))
                .add(Restrictions.eq("type", type)).list();

    }
...
}
@存储库
公共类ExampleDaoImpl扩展BaseDaoImpl实现ExampleDao{
...
@凌驾
@事务(只读=真)
公共列表FindSampleByCompany和Type(公司,
int类型){
返回sessionFactory.getCurrentSession()
.createCriteria(例如.class)
.add(Restrictions.eq(“company.companyId”),
company.getCompanyId())
.add(Restrictions.eq(“type”,type)).list();
}
...
}

PostgreSQL没有实现'dual',因此您可能打算在那里为测试查询选择类似于SELECT 1的内容。我不知道这是否会解决您的问题,但它会帮助容器更好地管理自己。

感谢您的快速回答,但这并没有解决问题。它将空闲查询更改为
设置会话字符ISTICS作为事务读写
和“SHOW TRANSACTION ISOLATION LEVEL``我将继续启用:此时我还可能尝试更改为其他连接池。我很幸运地使用了Apache dbcp以及Tomcat的新内置池。这两种池都可以在容器中配置,并通过JNDI连接到您的应用程序中。