java数据库连接池
目的:减少频繁开关连接的时间,提高整个系统的响应能力,通过分析发现应具备几个属性
(1)初始大小
(2)每次扩容的大小
(3)连接池的最大个数
(4)空闲连接的死亡时间
各种数据库连接池:
(1)DBCP(几乎没有公司使用了)官方文档
(2)C3P0 官网文档
(3)Druid(Alibaba) githun文档 帮助文档(重点)
(4)hikariCP github文档
(1)dbcp
//数据库连接池操作
public class DbcpTest {
public static void main(String[] args) {
BasicDataSource basc = new BasicDataSource();
basc.setDriverClassName("com.mysql.jdbc.Driver");
basc.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false");
basc.setUsername("root");
basc.setPassword("tiger");
Connection conn = null;
PreparedStatement pre = null;
ResultSet res = null;
try {
conn = basc.getConnection();
String sql = "select * from emp";
pre = conn.prepareStatement(sql);
res = pre.executeQuery();
while (res.next()){
String name = res.getString("name");
System.out.println(name);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
(2)C3P0
//c3p0连接数据库
public class Demo {
public static void main(String[] args) {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
cpds.setUser("root");
cpds.setPassword("tiger");
Connection connection = cpds.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from emp");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String name = resultSet.getString("name");
System.out.println(name);
}
connection.close();
} catch (PropertyVetoException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//配置文件必须名字一样
//c3p0.properties配置
c3po.driverClass = com.mysql.jdbc.Driver
c3p0.jdbcUrl = jdbc:mysql://localhost:3306/test
c3p0.user = root
c3p0.password = tiger
//c3p0-config.xml配置
<?xml version="1.0" encoding="utf-8" ?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">tiger</property>
<property name="acquireIncrement">10</property>
</default-config>
</c3p0-config>
public class Demo {
public static void main(String[] args) {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
// cpds.setDriverClass("com.mysql.jdbc.Driver");
// cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
// cpds.setUser("root");
// cpds.setPassword("tiger");
Connection connection = cpds.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from emp");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String name = resultSet.getString("name");
System.out.println(name);
}
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
cpds.close();
}
}
}
直接在类的方法中设置连接的参数,一般没有人使用,不太建议,最好使用配置文件
(3)druid
这是公司重点的使用的数据库连接池
public class Druid {
public static void main(String[] args) throws Exception {
Properties pro = new Properties();
//属性配置和c3p0效果一样
FileInputStream fileInputStream = new FileInputStream("./src/Druid/druid.properties");
pro.load(fileInputStream);
DataSource dataSource = new DruidDataSourceFactory().createDataSource(pro);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
(4)HiKariCP
方式一(直接设置)
public class HiKariCpTest {
public static void main(String[] args) throws SQLException {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/test");
config.setUsername("root");
config.setPassword("tiger");
HikariDataSource ds = new HikariDataSource(config);
Connection connection = ds.getConnection();
System.out.println(connection);
connection.close();
}
}
方式二(属性设置)
public class HiKariCpTest {
public static void main(String[] args) throws SQLException {
HikariConfig config = new HikariConfig("./src/HikariCP/HikariCP.properties");
HikariDataSource ds = new HikariDataSource(config);
Connection connection = ds.getConnection();
System.out.println(connection);
connection.close();
}
}