这个dbcp异常是否重要?

时间:2012-07-02 03:40:29

标签: spring apache-commons-dbcp

我正在使用spring 3.05 + mysql,程序运行后,我从日志中发现了很多异常:

books.log.6- 09:24:45 Closing JPA EntityManager -org.springframework.orm.jpa.EntityManagerFactoryUtils
books.log.6- 09:24:45 about to close PreparedStatement (open PreparedStatements: 1, globally: 1) -org.hibernate.jdbc.AbstractBatcher
books.log.6- 09:24:45 aggressively releasing JDBC connection -org.hibernate.jdbc.ConnectionManager
books.log.6- 09:24:45 releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] -org.hibernate.jdbc.ConnectionManager
books.log.6- 09:24:45 Cannot release connection [???] -org.hibernate.util.JDBCExceptionReporter
books.log.6: java.sql.SQLException: Already closed.
books.log.6-    at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)
books.log.6-    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
books.log.6-    at org.hibernate.connection.DatasourceConnectionProvider.closeConnection(DatasourceConnectionProvider.java:97)
books.log.6-    at org.hibernate.jdbc.ConnectionManager.closeConnection(ConnectionManager.java:474)
books.log.6-    at org.hibernate.jdbc.ConnectionManager.aggressiveRelease(ConnectionManager.java:429)
books.log.6-    at org.hibernate.jdbc.ConnectionManager.afterStatement(ConnectionManager.java:304)
books.log.6-    at org.hibernate.jdbc.AbstractBatcher.closePreparedStatement(AbstractBatcher.java:572)
books.log.6-    at org.hibernate.jdbc.AbstractBatcher.closeStatement(AbstractBatcher.java:291)
books.log.6-    at org.hibernate.jdbc.AbstractBatcher.closeQueryStatement(AbstractBatcher.java:307)
books.log.6-    at org.hibernate.jdbc.AbstractBatcher.closeQueryStatement(AbstractBatcher.java:234)
books.log.6-    at org.hibernate.loader.Loader.getResultSet(Loader.java:1967)
books.log.6-    at org.hibernate.loader.Loader.doQuery(Loader.java:802)
books.log.6-    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
books.log.6-    at org.hibernate.loader.Loader.doList(Loader.java:2533)
books.log.6-    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
books.log.6-    at org.hibernate.loader.Loader.list(Loader.java:2271)
books.log.6-    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
books.log.6-    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
books.log.6-    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
books.log.6-    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
books.log.6-    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
books.log.6-    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
books.log.6-    at com.tekelec.books.domain.BookInfo.findAllBooks_aroundBody4(BookInfo.java:238)
books.log.6-    at com.tekelec.books.domain.BookInfo.findAllBooks(BookInfo.java:1)
books.log.6-    at com.tekelec.books.web.BooksController.getBooks(BooksController.java:37)
books.log.6-    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
books.log.6-    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
books.log.6-    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
books.log.6-    at java.lang.reflect.Method.invoke(Method.java:616)
books.log.6-    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)

我知道这是由Mysql引起的 - 经过几个小时的闲置后,它始终关闭连接,而它仍然从dbcp端看起来活着。由于Mysql服务于多个数据库实例,因此我需要从应用程序代码端进行更改。

我的问题是,异常真的很重要吗?失败后JPA会重试连接吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您应该向select 1添加验证查询(DataSource)。

<Resource name="jdbc/cfmediaarchive" type="javax.sql.DataSource"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf8"

    auth="Container" username="username" password="password"

    maxIdle="3" maxActive="15" maxWait="10000" logAbandoned="true" removeAbandoned="true" removeAbandonedTimeout="60" 

    validationQuery="select 1" />

配置Spring:

spring.xml

   <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${driverClassName}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    <property name="validationQuery" value="select 1"/>
</bean>

  <bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <!-- this is important to connect JPA and JdbcTemplate transaction control -->
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
        </bean>
    </property>
</bean>

的persistence.xml

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.show_sql" value="false" />

    </properties>
</persistence-unit>