title: When are connections returned to the connection pool with Spring JPA (Hibernate) Entity Manager?
body: <p>In my java process I'm connecting to MySql using the following spring configuration:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySources({ @PropertySource("classpath:/myProperties1.properties"), @PropertySource("classpath:/myProperties2.properties") })
  public class MyConfiguration {
  
      @Autowired
      protected Environment env;
  
      /**
       * @return EntityManagerFactory for use with Hibernate JPA provider
       */
      @Bean(destroyMethod = "destroy")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setJpaVendorAdapter(jpaVendorAdapter());
      em.setPersistenceUnitManager(persistenceUnitManager());
  
      return em;
      }
  
      /**
       * 
       * @return jpaVendorAdapter that works in conjunction with the
       *         persistence.xml
       */
      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
      HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      vendorAdapter.setDatabase(Database.valueOf(env.getProperty("jpa.database")));
      vendorAdapter.setDatabasePlatform(env.getProperty("jpa.dialect"));
      vendorAdapter.setGenerateDdl(env.getProperty("jpa.generateDdl", Boolean.class, false));
      vendorAdapter.setShowSql(env.getProperty("jpa.showSql", Boolean.class, false));
  
      return vendorAdapter;
      }
  
      @Bean
      public PersistenceUnitManager persistenceUnitManager() {
      DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager();
      pum.setPackagesToScan("com.app.dal");
      pum.setDefaultPersistenceUnitName("my-pu");
      pum.setPersistenceXmlLocations("classpath:/META-INF/persistence.xml");
      pum.setDefaultDataSource(dataSource());
  
      return pum;
      }
  
      @Bean(destroyMethod = "close")
      public DataSource dataSource() {
      Properties dsProps = new Properties();
      dsProps.put("driverClassName", env.getProperty("hikari.driverClassName"));
      dsProps.put("username", env.getProperty("hikari.username"));
      dsProps.put("password", env.getProperty("hikari.password"));
      dsProps.put("jdbcUrl", env.getProperty("hikari.source.data.jdbcUrl"));
      dsProps.put("connectionTimeout", env.getProperty("hikari.connectionTimeout", Integer.class));
      dsProps.put("idleTimeout", env.getProperty("hikari.idleTimeout", Integer.class));
      dsProps.put("maxLifetime", env.getProperty("hikari.maxLifetime", Integer.class));
      dsProps.put("maximumPoolSize", env.getProperty("hikari.maximumPoolSize.rtb.source", Integer.class));
      dsProps.put("leakDetectionThreshold", env.getProperty("hikari.leakDetectionThreshold", Integer.class));
      dsProps.put("jdbc4ConnectionTest", env.getProperty("hikari.jdbc4ConnectionTest", Boolean.class));
  
      HikariConfig config = new HikariConfig(dsProps);
      HikariDataSource ds = new HikariDataSource(config);
  
      return ds;
      }
  
      @Bean(name = "sourceTxMgr")
      public PlatformTransactionManager sourceDatatransactionManager() {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setPersistenceUnitName("my-pu");
      transactionManager.setDataSource(dataSource());
  
      return transactionManager;
      }
  
      @Bean
      public PersistencyManager persistencyManager() {
      return new JpaPersistencyManager();
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
      return new PersistenceExceptionTranslationPostProcessor();
      }
  
  }
  </code></pre>
  
  <p>The Entity-Manager is injected to the data access layer by the container:</p>
  
  <pre><code>@PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "my-pu")
  private EntityManager myEntityManager;
  </code></pre>
  
  <p>And my public business logic methods are annotated with the <code>@Transactional</code> annotation.</p>
  
  <p>As far as I understand the container is responsible for ensuring that the entity-manager returns connections to the pool (in my case <a href="https://github.com/brettwooldridge/HikariCP" rel="noreferrer">HikariCP</a>) once a transaction is done but I did not find any official documentation that describes how the connections are managed. Can anyone explain it to me or provide a good reference that can explain when exactly connections are returned to the pool when using such a configuration?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The best related info I could come up with so far (<a href="http://blog.jhades.org/how-does-spring-transactional-really-work/" rel="noreferrer">taken from here</a>):</p>
  
  <p>The persistence context proxy that implements EntityManager is not the only component needed for making declarative transaction management work. Actually three separate components are needed:</p>
  
  <p>The EntityManager Proxy itself
  The Transactional Aspect
  The Transaction Manager
  Let's go over each one and see how they interact.</p>
  
  <p>The Transactional Aspect</p>
  
  <p>The Transactional Aspect is an 'around' aspect that gets called both before and after the annotated business method. The concrete class for implementing the aspect is TransactionInterceptor.</p>
  
  <p>The Transactional Aspect has two main responsibilities:</p>
  
  <p>At the 'before' moment, the aspect provides a hook point for determining if the business method about to be called should run in the scope of an ongoing database transaction, or if a new separate transaction should be started.</p>
  
  <p>At the 'after' moment, the aspect needs to decide if the transaction should be committed, rolled back or left running.</p>
  
  <p>At the 'before' moment the Transactional Aspect itself does not contain any decision logic, the decision to start a new transaction if needed is delegated to the Transaction Manager.</p>
  
  <p>The Transaction Manager</p>
  
  <p>The transaction manager needs to provide an answer to two questions:</p>
  
  <p>should a new Entity Manager be created?
  should a new database transaction be started?
  This needs to be decided at the moment the Transactional Aspect 'before' logic is called. The transaction manager will decide based on:</p>
  
  <p>the fact that one transaction is already ongoing or not
  the propagation attribute of the transactional method (for example  REQUIRES_NEW always starts a new transaction)
  If the transaction manager decides to create a new transaction, then it will:</p>
  
  <p>create a new entity manager
  bind the entity manager to the current thread
  grab a connection from the DB connection pool
  bind the connection to the current thread
  The entity manager and the connection are both bound to the current thread using ThreadLocal variables.</p>
  
  <p>They are stored in the thread while the transaction is running, and it's up to the Transaction Manager to clean them up when no longer needed.</p>
  
  <p>Any parts of the program that need the current entity manager or connection can retrieve them from the thread. One program component that does exactly that is the EntityManager proxy.</p>