Uploaded image for project: 'Commons Pool'
  1. Commons Pool
  2. POOL-363

Evictor Thread prevents Spring Context shutdown in standalone app

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.3, 2.5.0, 2.6.0, 2.6.1
    • 2.6.2
    • Java 1.8.0_171

    Description

      Given a DBCP2 connection pool configured in a Spring Application context with the POOL evictor thread enabled, a standalone main() application hangs on shutdown. Example below:

      Maven pom.xml:

        <dependencies>
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.6.0</version><!-- Uses commons-pool2 : 2.6.1 -->
          </dependency>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
          </dependency>
        </dependencies>
      

      MainTest.java:

      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      
      import javax.sql.DataSource;
      
      import org.apache.commons.dbcp2.BasicDataSource;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      public class MainTest
      {
        @Configuration
        public static class Config 
        {
          @Bean
          public DataSource getBasicDataSource()
          {
            BasicDataSource d = new BasicDataSource();
            d.setUrl("jdbc:mysql://localhost:3306/anydb");
            d.setUsername("myuser");
            d.setPassword("mypass");
            d.setTimeBetweenEvictionRunsMillis(1000); // Turn on Evictor Thread.
            return d;
          }
        }
      
        public static void main(String[] args)
        {
          AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
          ctx.registerShutdownHook(); // Ensures context.close() on JVM shutdown.
          
          try(
              Connection conn = ctx.getBean(BasicDataSource.class).getConnection(); 
              Statement stmt = conn.createStatement();
              ResultSet result = stmt.executeQuery("SELECT 1");
          )
          {
            int records = result.last() ? result.getRow() : 0;
            System.out.println("Records: "+records);
          }
          catch(SQLException e)
          {
            e.printStackTrace();
          }
          System.out.println("End.");
        }
      }
      

      The issue is not present when turning off the eviction thread by setting timeBetweenEvictionRunsMillis to -1.

      Jconsole thread summary attached for reference.

      Attachments

        1. 2019.03.26_screen281.jpg
          120 kB
          Josh Landin

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jlandin Josh Landin
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h