title: Spring transaction/entity manager don't compensate for stale connections (on websphere + openJPA)?
body: <p>The goal is to have a J2EE application running on a WebsphereApplicationServer 7, which accesses a JDBC datasource (DB2) via OpenJPA 2.0. On most of our testservers, my code is working fine; however, we have one testserver where the EntityManager aborts / does not get initialized properly because of a stale connection ("java.net.SocketException: Broken pipe"):</p>
  
  <pre><code>                             &lt;openjpa-2.1.2-SNAPSHOT-r422266:1384519 nonfatal user error&gt; org.apache.openjpa.persistence.ArgumentException: Failed to execute query "select count(x.profSurname) from Surname x where x.profUsrstate = 0". Check the query syntax for correctness. See nested exception for details.
  at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:872)
  at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:794)
  at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
  at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:315)
  at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:331)
  at org.apache.openjpa.persistence.QueryImpl.getSingleResult(QueryImpl.java:359)
      (...)
  Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: [jcc][t4][2030][11211][4.13.127] A communication error occurred during operations on the connection's underlying socket, socket input stream, 
  or socket output stream.  Error location: T4Agent.sendRequest() - flush (-1).  Message: Broken pipe. ERRORCODE=-4499, SQLSTATE=08001 {prepstmnt 1826931080 SELECT COUNT(t0.PROF_SURNAME) FROM EMPINST.SURNAME t0 WHERE (t0.PROF_USRSTATE = CAST(? AS BIGINT))  optimize for 1 row [params=?]} [code=-4499, state=08001]
  at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:281)
  at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:265)
  at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$700(LoggingConnectionDecorator.java:72)
  at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeQuery(LoggingConnectionDecorator.java:1183)
  at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:284)
  at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeQuery(JDBCStoreManager.java:1787)
  at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:274)
  at org.apache.openjpa.jdbc.sql.SelectImpl.executeQuery(SelectImpl.java:499)
  at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:424)
  at com.ibm.ws.persistence.jdbc.sql.SelectImpl.execute(SelectImpl.java:89)
  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)
  at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
  ... 113 more
  ---- Begin backtrace for Nested Throwables
  com.ibm.websphere.ce.cm.StaleConnectionException: [jcc][t4][2030][11211][4.13.127] A communication error occurred during operations on the connection's underlying socket, socket input stream, 
  or socket output stream.  Error location: T4Agent.sendRequest() - flush (-1).  Message: Broken pipe. ERRORCODE=-4499, SQLSTATE=08001
  at sun.reflect.GeneratedConstructorAccessor91.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
  at com.ibm.websphere.rsadapter.GenericDataStoreHelper.mapExceptionHelper(GenericDataStoreHelper.java:607)
  at com.ibm.websphere.rsadapter.GenericDataStoreHelper.mapException(GenericDataStoreHelper.java:666)
  at com.ibm.ws.rsadapter.AdapterUtil.mapException(AdapterUtil.java:2271)
  at com.ibm.ws.rsadapter.jdbc.WSJdbcUtil.mapException(WSJdbcUtil.java:1185)
  at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeQuery(WSJdbcPreparedStatement.java:726)
  at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:286)
  at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeQuery(LoggingConnectionDecorator.java:1181)
  at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:284)
  at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeQuery(JDBCStoreManager.java:1787)
  at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:274)
  at org.apache.openjpa.jdbc.sql.SelectImpl.executeQuery(SelectImpl.java:499)
  at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:424)
  at com.ibm.ws.persistence.jdbc.sql.SelectImpl.execute(SelectImpl.java:89)
  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)
  (...)
  Caused by: java.net.SocketException: Broken pipe
     at java.net.SocketOutputStream.socketWrite0(Native Method)
     at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:103)
     at java.net.SocketOutputStream.write(SocketOutputStream.java:147)
     at com.ibm.db2.jcc.t4.fb.b(fb.java:1685)
     at com.ibm.db2.jcc.t4.fb.a(fb.java:1633)
     at com.ibm.db2.jcc.t4.a.D(a.java:421)
     ... 138 more
  </code></pre>
  
  <p>I am working with OpenJPA using the Springframework 3.0 JpaTransactionManager and LocalContainerEntityManagerFactory to get my persistence context:</p>
  
  <pre><code>&lt;tx:annotation-driven transaction-manager="lctxManager" /&gt; 
    &lt;bean id="lctxManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="lcentityManagerFactory"&gt;&lt;/property&gt;
    &lt;/bean&gt; 
    &lt;bean id="lcentityManagerFactory"  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
     &lt;property name="persistenceUnitName" value="activities"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
        &lt;bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"&gt;
          &lt;property name="showSql" value="false"&gt;&lt;/property&gt;
        &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>The persistence.xml is as follows:</p>
  
  <pre><code>&lt;persistence-unit name="activities" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;non-jta-data-source&gt;jdbc/activities&lt;/non-jta-data-source&gt;
      &lt;!-- My classes --&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
  
      &lt;properties&gt;
          &lt;property name="openjpa.TransactionMode" value="local" /&gt;
      &lt;/properties&gt;
  </code></pre>
  
  <p>I need to restart the server on which the application runs for the Exception to vanish again -- before it (randomly?) pops up again.</p>
  
  <p>On googling the problem, I found a site that mentioned it was faulty code (no commit on transactions) that causes the problem: <a href="http://mikeschubert.com/2006/08/03/javanetsocketex/" rel="nofollow noreferrer">http://mikeschubert.com/2006/08/03/javanetsocketex/</a> 
  However, I am under the impression that the JPATransactionManager is supposed to take care of that.</p>
  
  <p>Other websites mentioned that implementing a connection pool would help (when using Hibernate in communication with a Tomcat-server, e.g. <a href="https://stackoverflow.com/questions/10435799/elegantly-handling-stale-database-connections-in-hibernate-spring-transactions">elegantly handling stale database connections in Hibernate/Spring Transactions</a>); however, the Websphere Application Server already manages a connections pool for the jdbc/activities datasource (minSize: 1; maxSize: 10; Connection timeout: 180 sec; Reap time: 180sec; Unused timeout: 1800 sec, Purge policy: FailingConnectionOnly).</p>
  
  <p>Any hints on where I should start looking into the problem would be great.</p>