Last active
July 8, 2023 21:26
-
-
Save kasramp/af9769dd0078bc094fb8 to your computer and use it in GitHub Desktop.
JDBC connection pool class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.SQLException; | |
import org.apache.tomcat.jdbc.pool.DataSource; | |
import org.apache.tomcat.jdbc.pool.PoolProperties; | |
public class DataSourceConfig { | |
public static final String DATA_SOURCE_CLASS_NAME = "org.postgresql.Driver"; | |
public static final String DATA_SOURCE_URL = "jdbc:postgresql://%s:%s/%s"; | |
public static final String JDBC_INTERCEPTORS = "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"; | |
private String dataSourceClassName = DATA_SOURCE_CLASS_NAME; | |
private String hostName; | |
private String portNumber; | |
private String userName; | |
private String password; | |
private String dbName; | |
private PoolProperties properties = new PoolProperties(); | |
private DataSource datasource = new DataSource(); | |
public String getHostName() { | |
return hostName; | |
} | |
public String getPortNumber() { | |
return portNumber; | |
} | |
public String getUserName() { | |
return userName; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public String getDbName() { | |
return this.dbName; | |
} | |
public void setHostName(String hostName) { | |
this.hostName = hostName; | |
} | |
public void setPortNumber(String portNumber) { | |
this.portNumber = portNumber; | |
} | |
public void setUserName(String userName) { | |
this.userName = userName; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public void setDbName(String dbName) { | |
this.dbName = dbName; | |
} | |
public void setupPool() { | |
String url = String.format(DATA_SOURCE_URL, hostName, portNumber, dbName); | |
properties.setUrl(url); | |
properties.setDriverClassName(DATA_SOURCE_CLASS_NAME); | |
properties.setUsername(userName); | |
properties.setPassword(password); | |
properties.setJmxEnabled(true); | |
properties.setTestWhileIdle(false); | |
properties.setTestOnBorrow(true); | |
properties.setValidationQuery("SELECT 1"); | |
properties.setTestOnReturn(false); | |
properties.setValidationInterval(30000); | |
properties.setTimeBetweenEvictionRunsMillis(30000); | |
properties.setMaxActive(100); | |
properties.setInitialSize(10); | |
properties.setMaxWait(10000); | |
properties.setRemoveAbandonedTimeout(60000); | |
properties.setMinEvictableIdleTimeMillis(30000); | |
properties.setMinIdle(10); | |
properties.setLogAbandoned(true); | |
properties.setRemoveAbandoned(true); | |
properties.setJdbcInterceptors(JDBC_INTERCEPTORS); | |
datasource.setPoolProperties(properties); | |
} | |
public Connection getConnection() throws SQLException { | |
return datasource.getConnection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment