1

在我使用适用于 SQL Server 2008 的 GlassFish 3.1 连接池的新“hello world”EJB/JPA 应用程序中,我总是在第一次查询时遇到“连接关闭”错误,并且在部署项目时经常(尽管并非总是)。我必须有一些配置不正确,我希望有人可以给我关于在哪里看的建议。

  • Windows 7 64 位
  • Eclipse 4.2 (Juno) 64 位
  • Java 1.6.0_33 64 位
  • JPA 通用 2.0
  • EclipseLink 2.4.0 - 朱诺
  • 内部 GlassFish 3.1.2.2
  • 适用于 SQL Server 的 Microsoft JDBC 驱动程序 4.0 (sqljdbc4.jar)
  • 微软 SQL Server 2008 R2 (SQLEXPRESS)

我正在使用一个连接池(通过 JTA),它是使用 GlassFish 管理页面和默认设置创建的。“Ping”成功。这是 domain.xml 中的池定义(我将密码设为空白):

<jdbc-connection-pool driver-classname="" datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource" res-type="javax.sql.ConnectionPoolDataSource" description="" name="DemoDatabase">
    <property name="User" value="demo"></property>
    <property name="DatabaseName" value="demo"></property>
    <property name="ApplicationName" value="Microsoft JDBC Driver for SQL Server"></property>
    <property name="LockTimeout" value="-1"></property>
    <property name="Password" value="******"></property>
    <property name="SendTimeAsDatetime" value="true"></property>
    <property name="PacketSize" value="8000"></property>
    <property name="SendStringParametersAsUnicode" value="true"></property>
    <property name="XopenStates" value="false"></property>
    <property name="TrustServerCertificate" value="false"></property>
    <property name="URL" value="jdbc:sqlserver://"></property>
    <property name="PortNumber" value="1433"></property>
    <property name="LoginTimeout" value="15"></property>
    <property name="ResponseBuffering" value="adaptive"></property>
    <property name="MultiSubnetFailover" value="false"></property>
    <property name="SelectMethod" value="direct"></property>
    <property name="LastUpdateCount" value="true"></property>
    <property name="WorkstationID" value="drizzle"></property>
    <property name="Encrypt" value="false"></property>
    <property name="ApplicationIntent" value="readwrite"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="DemoDatabase" description="" jndi-name="jdbc/DemoDatabase"></jdbc-resource>

持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="DemoEJB">
    <jta-data-source>jdbc/DemoDatabase</jta-data-source>
</persistence-unit>
</persistence>

我还为 MySQL 设置了一个类似的 GlassFish 连接池。当我使用那个时,我没有收到“连接关闭”错误。我由此推测问题出在我的 SQL Server 池配置中。

这是 GlassFish 日志中的错误:

INFO: WEB0671: Loading application [DemoEAR#Demo.war] at [Demo]
INFO: DemoEAR was successfully deployed in 1,066 milliseconds.
INFO: RAR7114: Unable to get ClientInfo for connection 
INFO: RAR7115: Unable to set ClientInfo for connection
INFO: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Error Code: 0
Call: SELECT ID, VAL FROM entity1 ORDER BY ID ASC
Query: ReadAllQuery(referenceClass=Entity1 sql="SELECT ID, VAL FROM entity1 ORDER BY ID ASC").
INFO: RAR7114: Unable to get ClientInfo for connection 
INFO: RAR7115: Unable to set ClientInfo for connection
INFO: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Error Code: 0
Call: SELECT ID, VAL FROM entity1 ORDER BY ID ASC
Query: ReadAllQuery(referenceClass=Entity1 sql="SELECT ID, VAL FROM entity1 ORDER BY ID ASC").
INFO: RAR7114: Unable to get ClientInfo for connection 
INFO: RAR7115: Unable to set ClientInfo for connection
INFO: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Error Code: 0
Call: SELECT ID, VAL FROM entity1 ORDER BY ID ASC
Query: ReadAllQuery(referenceClass=Entity1 sql="SELECT ID, VAL FROM entity1 ORDER BY ID ASC").
INFO: RAR7114: Unable to get ClientInfo for connection 
INFO: RAR7115: Unable to set ClientInfo for connection
INFO: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Error Code: 0
Call: SELECT ID, VAL FROM entity1 ORDER BY ID ASC
Query: ReadAllQuery(referenceClass=Entity1 sql="SELECT ID, VAL FROM entity1 ORDER BY ID ASC").
INFO: RAR7114: Unable to get ClientInfo for connection 
INFO: RAR7115: Unable to set ClientInfo for connection
INFO: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Error Code: 0
Call: SELECT ID, VAL FROM entity1 ORDER BY ID ASC
Query: ReadAllQuery(referenceClass=Entity1 sql="SELECT ID, VAL FROM entity1 ORDER BY ID ASC").
WARNING: Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Error Code: 0
Call: SELECT ID, VAL FROM entity1 ORDER BY ID ASC
Query: ReadAllQuery(referenceClass=Entity1 sql="SELECT ID, VAL FROM entity1 ORDER BY ID ASC")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1494)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:636)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1717)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:566)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:264)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:646)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2611)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2570)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:420)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1081)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1586)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:593)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5464)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1552)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1586)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:593)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5464)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1552)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1586)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:593)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5464)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1552)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1586)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:593)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5464)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1552)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742)
at com.centricsoftware.demo.module1.Module1.addEntity1(Module1.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
at $Proxy216.addEntity1(Unknown Source)
at com.centricsoftware.demo.module1.__EJB31_Generated__Module1__Intf____Bean__.addEntity1(Unknown Source)
at com.centricsoftware.demo.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolProxy.checkClosed(SQLServerConnectionPoolProxy.java:55)
at com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolProxy.prepareStatement(SQLServerConnectionPoolProxy.java:207)
at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:562)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
at com.sun.gjc.spi.ManagedConnection.prepareCachedStatement(ManagedConnection.java:993)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1474)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1423)
at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.prepareStatement(DatabaseCall.java:697)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:585)
... 98 more
4

2 回答 2

0

You must add the .jar jdbc sqlserver driver in your glassfish server as folow: {installationPth}\glassfish4\glassfish\domains\domain1\lib\ext

enjoy Kahoo:

(_/) (='.'=) (")_(")

于 2014-01-28T10:37:14.207 回答
0

您的数据库服务器是否正在运行?

也许它与您的 LoginTimeout 属性有关,您的登录可能需要更长的时间。尝试从连接池配置中删除除必需属性之外的所有属性。

也可能是防火墙、网络或数据库问题使您的连接超时。

您还可以在连接池中启用连接测试,以在返回连接之前验证连接。

您也可以始终使用 EclipseLink 连接池,或者尝试使用它来查看是否遇到相同的问题。

另外,包括您的 persistence.xml。

于 2012-08-08T12:31:10.517 回答