title: Could not open JPA EntityManager for transaction, DB connection closing during task excecution
body: <p>I was looking for similar problem, but each I've found is little bit different.</p>
  <p>I'm using SpringBoot (1.5.2.RELEASE) and PostgresSQL DB (42.0.0).<br>
  I describe problem below:<br>
  I have a method which takes some entity, send it to external services, and save result to DB. Each entity is taken one by one (not in the list). This process sometimes takes couple seconds, sometimes even couple minutes (depends on entities to process).<br>
  Everything is going ok, unles sudennly an exception is thrown:</p>
  <pre><code>org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed:
          at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
          at com.service.someClass.settings.SettingsServiceImpl$$EnhancerBySpringCGLIB$$897e9c29.getPropertySettings(&lt;generated&gt;)
          ..rest of stackTrace
  </code></pre>
  <p>Sometimes it takes me 2:15 (minutes:seconds), sometime 2:40 to get this exception.<br>
  After some investigation with DBeaver I've checked connections to DB with <code>select * from pg_stat_activity;</code>. During process excecution I was refreshing <code>pg_stat_activity</code> I noticed that one connection is responsible for creating transaction and commiting. In couple seconds query <code>SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE</code> appears for that connection. In the beginnig I can see 3 connections. During process excecutions I can see that one extra connection is being created and it reads some entity from current working table. Some time later the connection responsible for creating and commiting session disapear. Rest connections has got current query as <code>SELECT 1</code> and at that moment exception in backend is thrown (which I pasted earlier).<br>
  More readable (I hope) version is below:
  <a href="https://i.stack.imgur.com/cUAwt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cUAwt.jpg" alt="enter image description here" /></a></p>
  <p>I've read about DB connections configuration and that sympthoms leads me to Tomcat configuration which looks like:</p>
  <pre><code>tomcat:
        removeAbandoned: true
        removeAbandonedTimeout: 120
        testOnBorrow: true
        validationQuery: SELECT 1;
        validationInterval: 30000
        testWhileIdle: true
        timeBetweenEvictionRunsMillis: 60000
        maxActive: 10
        initialSize: 2
        minIdle: 2
        maxIdle: 5
  </code></pre>
  <p><strong>What was tested?</strong><br>
  After more reading I tried to set <code>removeAbandoned: false</code>. Whole process was completed with no exception. What was different? After one extra connection to DB has been created and process was completed - that connection still appears in <code>pg_stat_activity</code>. <br>
  When I set <code>removeAbandonedTimeout:</code> to the lower value - process crash much faster. I've tried 60, and 10. When I set value 10, process crashed after 30seconds, second time after ~50seconds. When it's set to 120, it crash more after ~2:30-2:50 (min).<br>
  That's why I assume that somehow my main connection which does whole work (creating and commiting transaction) is (wrong or not) recognized as a abadonded connection (as I got know - not closed).<br> <strong>What is important?</strong> Closed connection is all the time &quot;working&quot; - changes value in <code>query</code> column and update start and end time excecution. What is now more curious why is it  closed suddenly.<br>
  <br>
  I'm not very experienced, but I think that maybe something wrong with TransactionManager (Spring) happends.</p>