HikariCP连接池Java示例
时间:2020-01-09 10:35:21 来源:igfitidea点击:
在本文中,我们将介绍如何在JDBC应用程序中使用HikariCP配置连接池。
Mika对HikariCP的依赖
如果使用的是Maven,则可以在pom.xml中添加以下依赖项,该依赖项适用于Java 8至Java 11.
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency>
我们也可以从此处下载jarshttps://search.maven.org/search?q = com.zaxxer.hikaricp
HikariCP连接池Java示例
在示例数据库中使用的是MySQL,它连接到theitroad架构,表是EMPLOYEE,其列为id,FIRST_NAME,LAST_NAME和DEPARTMENT。
创建HIkariCP数据源的最佳方法是实例化HikariConfig类的实例,并提供用于连接数据库和连接池的属性。然后传递该配置实例以创建HikariDataSource对象。
我们需要设置的与数据库相关的配置是驱动程序类,URL,用户名和密码。
我们可以设置的一些与连接池相关的属性如下:
- autoCommit –此属性控制从池返回的连接的默认自动提交行为。默认为true。
- connectionTimeout –此属性控制客户端等待来自池的连接的最大毫秒数。默认值为30秒。
- idleTimeout-此属性控制允许连接在池中保持空闲状态的最长时间。默认值为10分钟。
- maxLifetime –此属性控制池中连接的最大生存期。默认值为30分钟。
- maximumPoolSize-此属性控制允许池达到的最大大小,包括空闲和正在使用的连接。默认值是10.
对于示例数据库凭证和与连接池有关的配置属性,保留在属性文件(db.properties)中。
DB.DRIVER_CLASS=com.mysql.cj.jdbc.Driver DB.DB_URL=jdbc:mysql://localhost:3306/theitroad DB.DB_USER=root DB.DB_PASSWORD=admin DB.MAX_POOL_SIZE=5
通过设置从属性文件读取的数据库属性和连接池属性,以下类用于创建HikariDataSource。
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class DSCreator {
private static HikariDataSource ds;
static {
try {
Properties properties = new Properties();
// Loading properties file from classpath
InputStream inputStream = DSCreator.class
.getClassLoader()
.getResourceAsStream("db.properties");
if(inputStream == null){
throw new IOException("File not found");
}
properties.load(inputStream);
HikariConfig config = new HikariConfig();
// This property is optional now
config.setDriverClassName(properties.getProperty("DB.DRIVER_CLASS"));
config.setJdbcUrl(properties.getProperty("DB.DB_URL"));
config.setUsername(properties.getProperty("DB.DB_USER"));
config.setPassword(properties.getProperty("DB.DB_PASSWORD"));
config.setMaximumPoolSize(Integer.parseInt(properties.getProperty("DB.MAX_POOL_SIZE")));
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
// Create DataSource
ds = new HikariDataSource(config);
}catch(IOException e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return ds;
}
}
测试类,该类使用返回的DataSource对象创建连接并查询数据库。
public class DSDemo {
public static void main(String[] args) {
DSDemo dsDemo = new DSDemo();
dsDemo.displayEmployeeById(16);
}
private void displayEmployeeById(int id){
Connection connection = null;
String selectSQL = "SELECT * FROM EMPLOYEE WHERE id = ?";
PreparedStatement prepStmt = null;
try {
DataSource ds = DSCreator.getDataSource();
connection = ds.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
prepStmt.setInt(1, id);
ResultSet rs = prepStmt.executeQuery();
while(rs.next()){
System.out.println("id: " + rs.getInt("id"));
System.out.println("First Name: " + rs.getString("FIRST_NAME"));
System.out.println("Last Name: " + rs.getString("LAST_NAME"));
System.out.println("Department: " + rs.getString("DEPARTMENT"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

