赞
踩
HikariCP是由日本程序员开源的一个数据库连接池组件,代码非常轻量,并且速度非常的快。根据官方提供的数据,在i7,开启32个线程32个连接的情况下,进行随机数据库读写操作,HikariCP的速度是现在常用的C3P0数据库连接池的数百倍。
在SpringBoot2.0中,官方也是推荐使用HikariCP。
HikariCP之所以快是因为:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
jdbcUrl=jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false
driverClassName=com.mysql.cj.jdbc.Driver
dataSource.user=root
dataSource.password=root
dataSource.maximumPoolSize=10
public class DBUtils {
public static Connection getConnection() {
HikariConfig config = new HikariConfig(DBUtils .class.getClassLoader().getResource("hikaricp.properties").getPath());
HikariDataSource ds = new HikariDataSource(config);
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn){
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws SQLException {
Connection conn = DBUtils.getConnection();
System.out.println(conn);
Statement stmt = conn.createStatement();
String sql = "select * from tb_dept";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getInt("deptno") + "\t" + rs.getString("dname") + "\t" + rs.getString("loc"));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。