[JAVA] Settings related to connection pool in Tomcat.jdbc

Cause: The following error log prevented you from connecting to JDBC.

 Cased by: java.sql.SQLRecoverableException:It is a closed connection.
```
 Countermeasure: Put the verification query of "SELECT 1 FROM DUAL" (Oracle) in setValidationQuery and specify setTestOnBorrow (true) to verify and discard the object and reconnect, but there are many other parameters, so I waved a comment in the list.


    public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
        org.apache.tomcat.jdbc.pool.DataSource datasource = null;
        PoolConfiguration p = new DefaultProperties();
 // Register the connection pool as a ConnectionPoolMBean Initial value is true
        p.setJmxEnabled(true);

 // Whether to issue a validationQuery on the idle connection to check to check the connection with the DB. A threaded version of testOnBorrow, testOnReturn. If true is specified, the object that is actually unable to communicate with the DB is discarded. If validationQuery is not specified, do nothing. This test is performed after the above minEvictableIdleTimeMillis test. The initial value is false.
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(false);
        p.setTestOnReturn(false);

 // Set to true to detect closed omission connections
        p.setRemoveAbandoned(false);

 // Set to output the stack trace of the application that has not closed the connection to the log when a close omission is detected.
        p.setLogAbandoned(false);

 // Set the query used to verify the life and death of the connection. If null or an empty string is specified, no validation will be performed. The default is null.
        p.setValidationQuery(null);

 // If the connections obtained from the pool have been pooled for longer than the interval (milliseconds) specified here, verify the life and death of the connections. The initial value is 10000
        p.setValidationInterval(1800000);

 // Specify the thread execution interval (milliseconds). If it is less than 1, the thread will not run. Therefore, in that case, the following parameter groups have no meaning even if they are specified. The default value is -1, so this thread does not work by default.
        p.setTimeBetweenEvictionRunsMillis(10);

 // Time to consider as a close omission Initial value is 60 sec
        p.setRemoveAbandonedTimeout(180);

 // Time when the idle connection can stay in the pool. If this time has passed since the connection was idle (in milliseconds), it will be disconnected and discarded. Less than 1 means unlimited and will never be destroyed. If you want to take advantage of the following testWhileIdle features, it doesn't make sense to specify unlimited. The initial value is 1800000 (30 minutes).
        p.setMinEvictableIdleTimeMillis(1800000);

 // Initial size of the connection created when the pool starts The initial value is 10
        p.setInitialSize(1);

 // Maximum number of active connections that can be allocated from the pool at the same time Initial value is 100
        p.setMaxActive(1);

 // Maximum number of connections to keep in the pool Initial value is maxActive
        p.setMaxIdle(1);

 // Minimum number of connections to keep in the pool Initial value is initialSize
        p.setMinIdle(1);

 // Maximum time to wait when no connection is available (in milliseconds) Initial value is 30000 = (30 seconds)
        p.setMaxWait(30000);

 // Time to hold the connection (in milliseconds) If maxAge or more has elapsed from the connection time when returning the connection, close the connection. Do not check with maxAge. The initial value is 0
        p.setMaxAge(180000);

 // Set to true if you want getConnection calls to be treated fairly in the FIFO format. By setting it to true, the org.apache.tomcat.jdbc.pool.FairBlockingQueue implementation is used for the idle connection list, and the initial value is true, which guarantees that connections will be acquired in the order in which threads arrive.
        p.setFairQueue(false);
        datasource = new org.apache.tomcat.jdbc.pool.DataSource();
        datasource.setPoolProperties(p);
        return datasource;
    }
 


Recommended Posts

Settings related to connection pool in Tomcat.jdbc
I tried to implement polymorphic related in Nogizaka.
Settings to display Japanese Javadoc in IntelliJ IDEA
Output settings to debug console in Visual Studio Code
To debug in eclipse
Joystick settings in OpenDS
How to change application.properties settings at boot time in Spring boot
Gradle settings memo (multi-project to all in one) for myself
A story I was addicted to in Rails validation settings