Tomcat连接池连接已关闭

时间:2014-10-02 05:35:44

标签: java tomcat connection-pooling

在我们的应用程序中,我们使用Tomcat DBCP配置了数据库连接池。我们的应用程序有超过150个数据库。我们无法为池设置超过2的最大活动连接数。因为我们的服务器资源有限。在对100个用户执行性能测试时,某些请求因随机抛出以下错误而失败。所以我想知道是否有一种方法可以微调池配置来解决这个问题。

Tomcat的server.xml中的数据源配置

<!-- Data Source -->
<Resource name="jdbc/openedge/xxxxx/services/xxxx" auth="Container"
    factory="main.org.apache.tomcat.jdbc.pool.DataSourceFactory" type="javax.sql.DataSource"
    driverClassName="com.ddtek.jdbc.openedge.OpenEdgeDriver"
    url="jdbc:datadirect:openedge://xxxxx"
    username="xxxxxx" 
    password="xxxxxx" 
    initialSize="2" 
    maxActive="2"
    maxIdle="2" 
    minIdle="2" 
    removeAbandoned="true" 
    removeAbandonedTimeout="900" 
    logAbandoned="true"
    global="jdbc/openedge/xxxxx/services/xxxx" 
    maxWait="-1" 
    testOnBorrow="true"
    validationInterval="30000" 
    validatorClassName="main.java.com.xxxxxxxx.util.DBConnectionValidator" 
    validationTimeout="2"  />

错误堆栈

<SSS-MAT-61771>  2014/Oct/01 10:07:32,599 WARN  [SqlExceptionHelper] SQL Error: 0, SQLState: null
<SSS-MAT-61771>  2014/Oct/01 10:07:32,599 ERROR [SqlExceptionHelper] Connection has already been  closed.
<SSS-MAT-61771>  2014/Oct/01 10:07:32,599 INFO  [BaseDaoImpl] closeSession starting:        BBBCompositeDaoImpl
<SSS-MAT-61771>  2014/Oct/01 10:07:32,599 INFO  [BaseDaoImpl] closeSession completed: BBBCompositeDaoImpl
<SSS-MAT-61771>  2014/Oct/01 10:07:32,599 ERROR [ExceptionHandler] Internal server error
main.java.com.xxxxxxxxx.common.exception.DataServiceException: Internal server error
    at     main.java.com.xxxxxxxxx.xxxxxxxx.services.ManageAssetsRestImpl.getMethodInManageAssests(Unknown     Source)
    at sun.reflect.GeneratedMethodAccessor124.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:193)
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:102)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:218)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:198)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:163)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at main.java.com.xxxxxxxxx.xxxxxxxx.filter.LogFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1600)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

Caused by: org.hibernate.exception.GenericJDBCException: could not inspect JDBC autocommit mode
    at     org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.isAutoCommit(LogicalConnectionImpl.java:395)
    at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.afterNonTransactionalQuery(TransactionCoordinatorImpl.java:195)
    at org.hibernate.internal.SessionImpl.afterOperation(SessionImpl.java:598)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1790)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:226)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)
    at main.java.com.xxxxxxxxx.common.dao.impl.BBBCompositeDaoImpl.getBBBIds(Unknown Source)
    at main.java.com.xxxxxxxxx.xxxxxxxx.business.delegates.ManageAssetsDelegate.getBBBIdentifiers(Unknown Source)
    at main.java.com.xxxxxxxxx.xxxxxxxx.business.delegates.ManageAssetsDelegate.getBBBIds(Unknown Source)
    at main.java.com.xxxxxxxxx.xxxxxxxx.services.ManageAssetsRestImpl.getBBBIds(Unknown Source)
    ... 40 more
Caused by: java.sql.SQLException: Connection has already been closed.
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:117)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)
    at     org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:80)
    at $Proxy166.getAutoCommit(Unknown Source)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.isAutoCommit(LogicalConnectionImpl.java:392)
    ... 49 more

用于在运行时路由数据库的Hibernate配置。

 <?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:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/jee
 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
 default-lazy-init="true" default-autowire="byName">

<bean id="bxxHistoryDao"
    class="main.java.com.xxxxxxxx.common.dao.impl.bxxHistoryDaoImpl"
    scope="prototype">
    <property name="sessionFactory" ref="bxxSessionFactory" />
</bean>

<bean id="bxxDataSource"
    class="main.java.com.xxxxxxxx.xxxxdataservices.openedge.BaseRoutingDataSource">
    <property name="targetDataSources">
        <map key-type="java.lang.String">
            <entry key="AXXXX" value-ref="bxxDataSource_AXXXX" />
            <entry key="EXXXX" value-ref="bxxDataSource_EXXXX" />
        </map>
    </property>
</bean>
<bean id="bxxDataSource_AXXXX"
    class="main.java.com.xxxxxxxx.xxxxdataservices.openedge.BxxRoutingDataSource">
    <property name="targetDataSources">
        <map key-type="java.lang.String">
            <entry key="BXXX1" value-ref="BXXX1DataSource_AXXXX" />
            <entry key="BXXX2" value-ref="BXXX2DataSource_AXXXX" />
        </map>
    </property>
</bean>
<bean id="bxxDataSource_EXXXX"
    class="main.java.com.xxxxxxxx.xxxxdataservices.openedge.BxxRoutingDataSource">
    <property name="targetDataSources">
        <map key-type="java.lang.String">
            <entry key="BXXX1" value-ref="BXXX1DataSource_EXXXX" />
            <entry key="BXXX2" value-ref="BXXX2DataSource_EXXXX" />
        </map>
    </property>
</bean>

<bean id="parentDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
    abstract="true" />
<bean id="BXXX1DataSource_AXXXX" parent="parentDataSource">
    <property name="jndiName"
        value="java:comp/env/jdbc/openedge/BXXX1/rmservices/AXXXX" />
</bean>
<bean id="BXXX2DataSource_AXXXX" parent="parentDataSource">
    <property name="jndiName"
        value="java:comp/env/jdbc/openedge/BXXX2/rmservices/AXXXX" />
</bean>
<bean id="BXXX1DataSource_EXXXX" parent="parentDataSource">
    <property name="jndiName"
        value="java:comp/env/jdbc/openedge/BXXX1/rmservices/EXXXX" />
</bean>
<bean id="BXXX2DataSource_EXXXX" parent="parentDataSource">
    <property name="jndiName"
        value="java:comp/env/jdbc/openedge/BXXX2/rmservices/EXXXX" />
</bean>

<bean id="parentSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    abstract="true">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.ProgressDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
            </prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.connection.autocommit">false</prop>
        </props>
    </property>
</bean>

<bean id="bxxSessionFactory" parent="parentSessionFactory">
    <property name="dataSource" ref="bxxDataSource" />
    <property name="packagesToScan" value="main.java.com.xxxxxxxx.common.domain.box" />
</bean>

<bean id="bxxTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="bxxSessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="bxxTransactionManager" />

0 个答案:

没有答案