我有要保存在Postgres DB中的EJB3 Entity bean。我正在使用Glassfish 3 App服务器来部署EJB。
当我使用我的Web层调用EJB时,会调用DB调用,但Glassfish会抛出以下异常
javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=135;_ThreadName=Thread-1;|javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: could not inspect JDBC autocommit mode
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:635)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:589)
at com.cricinfo.session.service.PlayerService.getPlayer(PlayerService.java:41)
的根本原因是
Caused by: org.hibernate.exception.JDBCConnectionException: could not inspect JDBC autocommit mode
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery(JDBCContext.java:296)
at org.hibernate.impl.SessionImpl.afterOperation(SessionImpl.java:595)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1010)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:614)
... 96 more
Caused by: java.sql.SQLNonTransientConnectionException: No current connection.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Connection.getAutoCommit(Unknown Source)
at com.sun.gjc.spi.base.ConnectionHolder.getAutoCommit(ConnectionHolder.java:307)
at org.hibernate.jdbc.ConnectionManager.isAutoCommit(ConnectionManager.java:212)
at org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery(JDBCContext.java:287)
... 100 more
Caused by: org.apache.derby.client.am.SqlException: No current connection.
与Postgres DB的连接已启动,我可以使用pgAdmin工具查看数据。我可以在日志中看到正在执行的查询。从日志中我们可以看到Glassfish正在使用derby客户端来获取连接。
我需要在Glassfish中进行任何配置吗?
persistence.xml是:
<persistence-unit name="PlayerApp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.cricinfo.domain.Player</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"
/> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"
/> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.connection.url"
value="jdbc:postgresql://localhost:5432/PlayerAppDB" /> <property name="hibernate.show_sql"
value="true" /> <property name="hibernate.format_sql" value="true" /> <property
name="hibernate.connection.username" value="postgres" /> <property name="hibernate.connection.password"
value="postgres" />
</properties>
</persistence-unit>
我可以使用直接使用EntityManagerFactory的独立类来持久化并获取数据。
编辑:解决方案
我更多地阅读了它,并意识到我在persitence.xml中犯了一个错误。现在persistence.xml看起来像这样:
<persistence-unit name="PlayerApp" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/DefaultDS</jta-data-source>
<class>com.cricinfo.domain.Player</class>
</persistence-unit>
交易类型是 JTA ,并且使用了数据源而不是连接属性。
我还登录了Glassfish管理控制台,并从树CommonTasks-&gt;树中配置了JDBC连接池和JDBC资源。资源 - &gt; JDBC
我现在能够获得详细信息。
答案 0 :(得分:0)
我更多地阅读了它,并意识到我在persitence.xml中犯了一个错误。现在persistence.xml看起来像这样:
<persistence-unit name="PlayerApp" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/DefaultDS</jta-data-source>
<class>com.cricinfo.domain.Player</class>
</persistence-unit>
交易类型是 JTA ,并且使用了数据源而不是连接属性。
我还登录了Glassfish管理控制台,并从树CommonTasks-&gt;树中配置了JDBC连接池和JDBC资源。资源 - &gt; JDBC
我现在能够获得详细信息。