title: Connection reset by peer in Spring Boot application with Hibernate and Oracle
body: <p>I'm getting this exception</p>
  
  <pre><code>  Caused by: java.sql.SQLException: An operation is being attempted on a closed connection.
     ------------------------ Cause of Close ---------------------------------
   java.io.IOException: Communication error with xxxxxxxxxx:9401[jdbc-1908517]
  at cs.jdbc.driver.protocol.WireDecoder.processInput(WireDecoder.java:360)
  at cs.jdbc.driver.protocol.ChannelConnection.processInput(ChannelConnection.java:371)
  at cs.jdbc.driver.protocol.DataReader.run(DataReader.java:42)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:748)
        Caused by: java.io.IOException: Connection reset by peer
  at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
  at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
  at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
  at sun.nio.ch.IOUtil.read(IOUtil.java:197)
  at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
  at cs.jdbc.driver.protocol.ChannelConnection.read(ChannelConnection.java:376)
  at cs.jdbc.driver.protocol.WireDecoder.processInput(WireDecoder.java:260)
  </code></pre>
  
  <h2>    ... 5 more</h2>
  
  <pre><code>                  at 
  
  
  cs.jdbc.driver.CompositeConnection.getChannel(CompositeConnection.java:165)
  at cs.jdbc.driver.CompositeConnection.prepareStatement(CompositeConnection.java:390)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
  at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
  at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
  at com.sun.proxy.$Proxy120.prepareStatement(Unknown Source)
  at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
  at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
  ... 104 common frames omitted
  </code></pre>
  
  <p>I'm using Spring Boot + Spring DATA + Hibernate/JPA. After some time, I get this exception. But when restarting server, application remains ok for some time (when such exception is thrown again).</p>
  
  <p>Here is the application.properties</p>
  
  <pre><code>    spring.datasource.url = jdbc:compositesw:dbapi@xxxxxxxxxxxxx:9401?domain=twccorp&amp;dataSource=SFDC&amp;autoReconnect=true
        spring.datasource.username = xxxx
     spring.datasource.password = xxxx
     spring.datasource.driverClassName = cs.jdbc.driver.CompositeDriver
     spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.OracleDialect
     spring.jpa.properties.hibernate.show_sql=true
     spring.jpa.properties.hibernate.use_sql_comments=true
     spring.jpa.properties.hibernate.format_sql=true
  
  
      spring.jpa.properties.hibernate.type=trace 
  
        spring.datasource.dbcp.test-while-idle=true
            spring.datasource.dbcp.validation-query=SELECT 1 from dual
        spring.datasource.dbcp.time-between-eviction-runs-millis=5000
      spring.datasource.dbcp.min-evictable-idle-time-millis=60000
  
       spring.datasource.test-while-idle=true
        spring.datasource.validation-query=SELECT 1 from dual
       spring.datasource.time-between-eviction-runs-millis=5000
           spring.datasource.min-evictable-idle-time-millis=60000
  </code></pre>
  
  <p>Database is Oracle.</p>
  
  <p>Here is the Repository implementation:</p>
  
  <pre><code>   @Repository
   public class AccountRepositoryCustomImpl implements AccountRepositoryCustom {
  
  @PersistenceContext
  private EntityManager em;
  
  @Value("${recordLimit}")
  private String recordLimit;
  
  @Override
  public List&lt;AccountDatabase&gt; searchForAccounts(Map&lt;String, Object&gt; map) {
  
      List&lt;String&gt; whereClause = new ArrayList&lt;&gt;();
  
      StringBuilder queryBuilder = new StringBuilder();
      queryBuilder.append("select ad from AccountDatabase ad ");
  
      for (String key : map.keySet()) {
          if (!key.equals("city") &amp;&amp; !key.equalsIgnoreCase("address")) {
              whereClause.add(" ad." + key + " LIKE :" + key);
          }
      }
  
      queryBuilder.append(" where " + StringUtils.join(whereClause, " and "));
  
      if (map.containsKey("city")) {
          queryBuilder.append("(( ad.accountType = :accountType AND ad.billingCity LIKE :billingCity ) OR "
                  + "( ad.accountType = :accountType2 AND ad.shippingCity LIKE :shippingCity ))");
  
      }
  
      if (map.containsKey("address")) {
          queryBuilder.append("(( ad.accountType = :accountType AND ad.billingStreet LIKE :billingStreet ) OR "
                  + "( ad.accountType = :accountType2 AND ad.shippingStreet LIKE :shippingStreet ))");
  
      }
  
      Query jpaQuery = em.createQuery(queryBuilder.toString(), AccountDatabase.class);
  
      for (String key : map.keySet()) {
          if (!key.equals("city") &amp;&amp; !key.equalsIgnoreCase("address")) {
              jpaQuery.setParameter(key, "%" + map.get(key) + "%");
          }
      }
  
      if (map.containsKey("address")) {
          jpaQuery.setParameter("accountType", "Billing Account");
          jpaQuery.setParameter("billingStreet", "%" + map.get("address") + "%");
          jpaQuery.setParameter("accountType2", "Service Location");
          jpaQuery.setParameter("shippingStreet", "%" + map.get("address") + "%");
  
      }
  
      if (map.containsKey("city")) {
          jpaQuery.setParameter("accountType", "Billing Account");
          jpaQuery.setParameter("billingCity", "%" + map.get("city") + "%");
          jpaQuery.setParameter("accountType2", "Service Location");
          jpaQuery.setParameter("shippingCity", "%" + map.get("city") + "%");
  
      }
  
      int limit = Integer.valueOf(recordLimit);
      List&lt;AccountDatabase&gt; list = jpaQuery.getResultList();
      if (list.size() &gt; limit) {
          list = list.subList(0, limit);
      }
  
      em.close();
      return list;
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here's the pom.xml</p>
  
  <p>http://maven.apache.org/xsd/maven-4.0.0.xsd">
      4.0.0</p>
  
  <pre><code>&lt;artifactId&gt;xxxxxxxx&lt;/artifactId&gt;
  &lt;version&gt;0.0.1&lt;/version&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;parent&gt;
      &lt;groupId&gt;xxxxxxxx&lt;/groupId&gt;
      &lt;artifactId&gt;xxxxxxxx&lt;/artifactId&gt;
      &lt;version&gt;0.0.2&lt;/version&gt;
  &lt;/parent&gt;
  
  &lt;name&gt;egateway-api-service&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;java-version&gt;1.8&lt;/java-version&gt;
  &lt;/properties&gt;
  
  
  &lt;dependencies&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;some.group&lt;/groupId&gt;
          &lt;artifactId&gt;non-maven-proj&lt;/artifactId&gt;
          &lt;version&gt;1&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
  
  
  
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.ehcache&lt;/groupId&gt;
          &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.cache&lt;/groupId&gt;
          &lt;artifactId&gt;cache-api&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;io.springfox&lt;/groupId&gt;
          &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
          &lt;version&gt;2.4.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;io.springfox&lt;/groupId&gt;
          &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
          &lt;version&gt;2.4.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;commons-collections&lt;/groupId&gt;
          &lt;artifactId&gt;commons-collections&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-integration&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
  
      &lt;/dependency&gt;
      &lt;!-- Hibernate 5.2.9 Final --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;!-- Apache Commons DBCP --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
          &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
          &lt;version&gt;2.1.1&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;repackage&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>What am I doing wrong? Regards.</p>