zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

delphi 数据库连接池-MySQL之数据库连接池(Druid)

2023-02-18 16:46:38 时间

  目录

  数据库连接池

  每次创建数据库连接的问题

  获取数据库连接需要消耗比较多的资源,而每次操作都要重新获取新的连接对象,执

  行一次操作就把连接关闭,而数据库创建连接通常需要消耗相对较多的资源。这样数据库连接对象的使用率低。

  连接池的概念

  :连接池就是一个容器,连接池中保存了一些数据库连接,这些连接是可以重复使用的。

  连接池的原理

  启动连接池,连接池就会初始化一些连接

  当用户需要使用数据库连接,直接从连接池中取出

  当用户使用完连接delphi 数据库连接池,会将连接重新放回连接池中

  连接池好处

  连接池中会保存一些连接,这些连接可以重复使用,降低数据资源的消耗

  Druid

  Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。

  在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,可以很好的监控数据库连接池和SQL的执行情况。

  Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验

  Druid地址:

  Druid常用的配置参数

   刚启动连接池时,连接池中包含连接的数量

   连接池中最多可以放多少个连接

   获取连接时最大等待时间,单位毫秒(超时则报错)

  Druid连接池使用步骤

  1.导入druid-1.0.0.jar的jar包

  2.编辑druid.

  3.加载文件的内容到对象中

  4.创建Druid连接池delphi 数据库连接池,使用配置文件中的参数

  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 
[1]: https://xuan.ddwoo.top/index.php/archives/386/
[2]: https://xuan.ddwoo.top/index.php/archives/380/
[3]: https://xuan.ddwoo.top/index.php/archives/379/
[4]: https://xuan.ddwoo.top/index.php/archives/381/