title: Connection Closed error for SQL Server 2008 through Glassfish 3.1 connection pool
body: <p>In my new "hello world" EJB/JPA app using a GlassFish 3.1 connection pool for SQL Server 2008, I always get "Connection closed" errors on my first query, and frequently (though not always) when deploying the project. I must have something configured incorrectly, and I'm hoping someone can give me suggestions on where to look.</p>
  
  <ul>
  <li>Windows 7 64-bit</li>
  <li>Eclipse 4.2 (Juno) 64-bit</li>
  <li>Java 1.6.0_33 64-bit</li>
  <li>JPA Generic 2.0</li>
  <li>EclipseLink 2.4.0 - Juno</li>
  <li>Internal GlassFish 3.1.2.2</li>
  <li>Microsoft JDBC Driver 4.0 for SQL Server (sqljdbc4.jar)</li>
  <li>Microsoft SQL Server 2008 R2 (SQLEXPRESS)</li>
  </ul>
  
  <p>I'm using a connection pool (through JTA) that I created using the GlassFish admin page with the default settings. "Ping" succeeds. Here is the pool definition from domain.xml (I blanked out the password):</p>
  
  <pre><code>&lt;jdbc-connection-pool driver-classname="" datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource" res-type="javax.sql.ConnectionPoolDataSource" description="" name="DemoDatabase"&gt;
      &lt;property name="User" value="demo"&gt;&lt;/property&gt;
      &lt;property name="DatabaseName" value="demo"&gt;&lt;/property&gt;
      &lt;property name="ApplicationName" value="Microsoft JDBC Driver for SQL Server"&gt;&lt;/property&gt;
      &lt;property name="LockTimeout" value="-1"&gt;&lt;/property&gt;
      &lt;property name="Password" value="******"&gt;&lt;/property&gt;
      &lt;property name="SendTimeAsDatetime" value="true"&gt;&lt;/property&gt;
      &lt;property name="PacketSize" value="8000"&gt;&lt;/property&gt;
      &lt;property name="SendStringParametersAsUnicode" value="true"&gt;&lt;/property&gt;
      &lt;property name="XopenStates" value="false"&gt;&lt;/property&gt;
      &lt;property name="TrustServerCertificate" value="false"&gt;&lt;/property&gt;
      &lt;property name="URL" value="jdbc:sqlserver://"&gt;&lt;/property&gt;
      &lt;property name="PortNumber" value="1433"&gt;&lt;/property&gt;
      &lt;property name="LoginTimeout" value="15"&gt;&lt;/property&gt;
      &lt;property name="ResponseBuffering" value="adaptive"&gt;&lt;/property&gt;
      &lt;property name="MultiSubnetFailover" value="false"&gt;&lt;/property&gt;
      &lt;property name="SelectMethod" value="direct"&gt;&lt;/property&gt;
      &lt;property name="LastUpdateCount" value="true"&gt;&lt;/property&gt;
      &lt;property name="WorkstationID" value="drizzle"&gt;&lt;/property&gt;
      &lt;property name="Encrypt" value="false"&gt;&lt;/property&gt;
      &lt;property name="ApplicationIntent" value="readwrite"&gt;&lt;/property&gt;
  &lt;/jdbc-connection-pool&gt;
  &lt;jdbc-resource pool-name="DemoDatabase" description="" jndi-name="jdbc/DemoDatabase"&gt;&lt;/jdbc-resource&gt;
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  &lt;persistence-unit name="DemoEJB"&gt;
      &lt;jta-data-source&gt;jdbc/DemoDatabase&lt;/jta-data-source&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I have also set up a similar GlassFish connection pool for MySQL. When I use that one, I do not get "Connection Closed" errors. I presume from this that the problem is in my SQL Server pool configuration.</p>
  
  <p>Here is the error from the GlassFish log:</p>
  
  <pre><code>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
  </code></pre>