赞
踩
每次创建数据库连接的问题
获取数据库连接需要消耗比较多的资源,而每次操作都要重新获取新的连接对象,执
行一次操作就把连接关闭,而数据库创建连接通常需要消耗相对较多的资源。这样数据库连接对象的使用率低。
:连接池就是一个容器,连接池中保存了一些数据库连接,这些连接是可以重复使用的。
启动连接池,连接池就会初始化一些连接
当用户需要使用数据库连接,直接从连接池中取出
当用户使用完连接,会将连接重新放回连接池中
连接池中会保存一些连接,这些连接可以重复使用,降低数据资源的消耗
Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。
在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,可以很好的监控数据库连接池和SQL的执行情况。
Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验
Druid地址:https://github.com/alibaba/druid
initialSize 刚启动连接池时,连接池中包含连接的数量
maxActive 连接池中最多可以放多少个连接
maxWait 获取连接时最大等待时间,单位毫秒(超时则报错)
1.导入druid-1.0.0.jar的jar包
2.编辑druid.properties
3.加载properties文件的内容到Properties对象中
4.创建Druid连接池,使用配置文件中的参数
5.从Druid连接池中取出连接
6.执行SQL语句
7.关闭资源
public static void main(String[] args) throws Exception { Properties properties = new Properties(); //加载properties文件的内容到Properties对象中 properties.load(new FileReader("study\\src\\druid.properties")); //创建Druid连接池,使用配置文件中的参数 DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); //从Druid连接池中取出连接 Connection connection = dataSource.getConnection(); //执行SQL语句 String sql = "INSERT INTO tb_user VALUES (null, ?, ?);"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1,"dog"); statement.setString(2,"8888"); int i = statement.executeUpdate(); System.out.println(i); statement.close(); connection.close(); }
将获取数据库连接封装成工具类
public class DruidDataSourceUtils { private static DataSource dataSource = null; static { try (FileReader fir = new FileReader("study\\src\\druid.properties")) { Properties properties = new Properties(); properties.load(fir); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } //对外暴露一个获取连接的方法 //返回的结果可以尽量选择范围更大的 //提供公共方法的时候,谁调用谁负责 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } }
根据工具类来获取连接
public static void main(String[] args) throws SQLException {
Connection connection = DruidDataSourceUtils.getConnection();
String sql = "INSERT INTO tb_user VALUES (null, ?, ?);";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"pig");
preparedStatement.setString(2,"7777");
int i = preparedStatement.executeUpdate();
System.out.println(i);
preparedStatement.close();
connection.close();
}
Druid中三个参数
首先前5个连接是初始连接数,直接拿到
第二个循环去获取连接,不会等待,直接去拿最大连接
超过最大连接数再去获取连接,超过等待时间然后报错。
若在等待时间内有连接释放,会获取到释放的连接
public static void main(String[] args) throws SQLException { Connection con = null; //initialSize=5 初始化连接数 for (int i = 0; i < 5; i++) { con = DruidDataSourceUtils.getConnection(); System.out.println(con); } //maxActive=10 最大化连接10 先使用最大连接 for (int i = 0; i < 5; i++) { con = DruidDataSourceUtils.getConnection(); System.out.println(con); } con.close(); // maxWait=2000 等待时间2000毫秒 10个连接都用完了,就没有连接可以用,等待之后直接报错 con = DruidDataSourceUtils.getConnection(); System.out.println(con); }
如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。