org.apache.openjpa.lib.jdbc.ReportingSQLException:连接断开:“会话已关闭”

时间:2011-12-17 23:21:37

标签: h2 openjpa

对于用Java编写的小型桌面应用程序,我正在使用H2 database 1.3.162和OpenJPA 2.1.1。

出于某种原因,我在代码的随机位置出现以下异常。

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: Connection is broken: "session closed" [90067-162] {SELECT COUNT(t0.id) FROM Visitor t0 WHERE (t0.EngineId = ?)} [code=90067, state=90067]
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:281)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:261)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$000(LoggingConnectionDecorator.java:72)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection.prepareStatement(LoggingConnectionDecorator.java:313)
    at org.apache.openjpa.lib.jdbc.DelegatingConnection.prepareStatement(DelegatingConnection.java:155)
    at org.apache.openjpa.lib.jdbc.ConfiguringConnectionDecorator$ConfiguringConnection.prepareStatement(ConfiguringConnectionDecorator.java:158)
    at org.apache.openjpa.lib.jdbc.DelegatingConnection.prepareStatement(DelegatingConnection.java:155)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$RefCountConnection.prepareStatement(JDBCStoreManager.java:1653)
    at org.apache.openjpa.lib.jdbc.DelegatingConnection.prepareStatement(DelegatingConnection.java:144)
    at org.apache.openjpa.jdbc.sql.SQLBuffer.prepareStatement(SQLBuffer.java:561)
    at org.apache.openjpa.jdbc.sql.SQLBuffer.prepareStatement(SQLBuffer.java:541)
    at org.apache.openjpa.jdbc.sql.SelectImpl.prepareStatement(SelectImpl.java:479)
    at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:420)
    at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:391)
    at org.apache.openjpa.jdbc.sql.LogicalUnion$UnionSelect.execute(LogicalUnion.java:427)
    at org.apache.openjpa.jdbc.sql.LogicalUnion.execute(LogicalUnion.java:230)
    at org.apache.openjpa.jdbc.sql.LogicalUnion.execute(LogicalUnion.java:220)
    at org.apache.openjpa.jdbc.kernel.SelectResultObjectProvider.open(SelectResultObjectProvider.java:94)
    at org.apache.openjpa.kernel.QueryImpl$PackingResultObjectProvider.open(QueryImpl.java:2070)
    at org.apache.openjpa.kernel.QueryImpl.singleResult(QueryImpl.java:1320)
    at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1242)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1007)

我的persistence.xml很简单,没什么特别的

<persistence-unit name="openjpa"  transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>com.mine.reporting.data.IpInfo</class>
    <properties>
        <property name="openjpa.ConnectionURL" value="jdbc:h2:database"/>
        <property name="openjpa.ConnectionDriverName" value="org.h2.Driver"/>
        <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.H2Dictionary"/>

        <property name="openjpa.ConnectionUserName" value="sa"/>
        <property name="openjpa.ConnectionPassword" value=""/>
        <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
        <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>
</persistence-unit>

但如果我用EclipseLink(前TopLink)替换OpenJPA,一切正常,这可以让我得出结论,问题出在OpenJPA中。

我做得不对?

1 个答案:

答案 0 :(得分:2)

尝试设置openjpa.ConnectionRetainMode=always。这将告诉OpenJPA每个EM获得一个连接并保持它。这可能会使问题消失,但它无法解决为什么你的连接似乎变坏的根本问题。

我相信2.1.1 OpenJPA会将DBCP作为JPA和数据库之间的连接池插入,也许检测错误连接时会出现问题。?