Hibernate:为什么c3p0超时不起作用?

时间:2016-07-13 06:47:02

标签: java oracle hibernate session c3p0

我使用hibernate处理web java应用程序,并且我总是在我的oracle db上获得大量java非活动会话的内存低错误。我试图用c3p0超时配置关闭这些未使用的会话,但它根本不起作用。 这是我的hibernate配置和几分钟后的开放会话: (下面注释的代码用于当我尝试使用通用dbcp时,请参考类似问题的答案之一,并将测试的超时时间设置为30秒。)

<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:test</property>
    <property name="hibernate.connection.username">tams_test</property>
    <property name="hibernate.connection.password">test1234</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>  
    <property name="hibernate.connection.isolation">2</property>
    <property name="org.hibernate.flushMode">COMMIT</property>
    <property name="hibernate.cache.use_query_cache">false</property>
    <property name="hibernate.transaction.auto_close_session">true</property>
    <property name="hibernate.order_updates">true</property>
    <property name="hibernate.order_inserts">true</property>
    <!--  <property name="hibernate.dbcp.initialSize">3</property>
    <property name="hibernate.dbcp.maxIdle">50</property>
    <property name="hibernate.dbcp.minIdle">0</property>
    <property name="hibernate.dbcp.maxWait">180000</property>
    <property name="hibernate.dbcp.maxConnLifetimeMillis">30000</property>
    <property name="hibernate.dbcp.defaultQueryTimeout">1</property>-->
    <property name="hibernate.c3p0.min_size">1</property>
    <property name="hibernate.c3p0.max_size">150</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.timeout">30</property>
    <property name="hibernate.c3p0.idle_test_period">30</property>
    <property name="hibernate.c3p0.idleConnectionTestPeriod">30</property>
    <property name="c3p0.testConnectionOnCheckout">true</property>
    <property name="testConnectionOnCheckin">true</property>
    <property name="c3p0.maxConnectionAge">30</property>
    <property name="maxIdleTimeExcessConnections">30</property>
    <property name="connection.provider_class">
        org.hibernate.connection.C3P0ConnectionProvider
    </property>

几分钟后在oracle db上打开会话 enter image description here

1 个答案:

答案 0 :(得分:0)

我也遇到了你的问题,而且hibernate不想应用我的c3p0参数,所以我改变了我的数据源,它完全解决了我的非活动会话问题。

尝试像下面这样设置dataSource bean:

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="jdbcUrl" value="jdbc:oracle:thin:@xxxx:1521:xxx"/>
    <property name="user" value="xx"/>
    <property name="password" value="xxxx"/>

    <property name="maxStatements" value="1000"/>
    <property name="maxIdleTime" value="30"/>
    <property name="maxPoolSize" value="100"/>
    <property name="minPoolSize" value="10"/>
    <property name="initialPoolSize" value="10"/>
    <property name="idleConnectionTestPeriod" value="20"/>

</bean>