1

我正在使用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 会在失败后重试连接吗?

谢谢!

4

1 回答 1

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 配置:

春天.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>

持久性.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>
于 2012-07-02T04:56:19.693 回答