测试 jdbc 中连接关闭的时机

测试 jdbc 中连接关闭的时机

  • 写一段程序,测试 jdbc 连接的关闭情况
    /**
         * 测试 jdbc 连接的关闭情况
         */
        public static void testOpenCon(){
            //打开连接
            // getOpenCon();
            try {
                //关闭连接
                // CON.close();
                //如果CON不为空,已连接过
                if(CON!= null){
                    //如果CON是false  
                    // isClosed()该方法判断是否关闭连接   返回true false  返回true是已经关闭  返回fasle在连接
                    if(!CON.isClosed()){
                        System.out.println("Connection连接中");
                    }
                    else {
                        System.out.println("Connection已关闭");
                    }
                }
                //如果CON为空,为初始话对象 CON= DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
                else {
                    System.out.println("未初始化Connection对象");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    View Code
  • 使用 show processlist 查看所有数据库连接
      在数据库进行用show processlist进行查询,得出9个字段

      

      首先做个类进行接收这九个字段

    • package com.wbg.test;
      
      public class ProcessList {
          int id;
          String user;
          String host;
          String db;
          String command;
          String time;
          String state;
      
          @Override
          public String toString() {
              return "ProcessList{" +
                      "id=" + id +
                      ", user='" + user + '\'' +
                      ", host='" + host + '\'' +
                      ", db='" + db + '\'' +
                      ", command='" + command + '\'' +
                      ", time='" + time + '\'' +
                      ", state='" + state + '\'' +
                      ", info='" + info + '\'' +
                      ", progress='" + progress + '\'' +
                      '}';
          }
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getUser() {
              return user;
          }
      
          public void setUser(String user) {
              this.user = user;
          }
      
          public String getHost() {
              return host;
          }
      
          public void setHost(String host) {
              this.host = host;
          }
      
          public ProcessList(int id, String user, String host, String db, String command, String time, String state, String info, String progress) {
              this.id = id;
              this.user = user;
              this.host = host;
              this.db = db;
              this.command = command;
              this.time = time;
              this.state = state;
              this.info = info;
              this.progress = progress;
          }
      
          public String getDb() {
              return db;
          }
      
          public void setDb(String db) {
              this.db = db;
          }
      
          public String getCommand() {
              return command;
          }
      
          public void setCommand(String command) {
              this.command = command;
          }
      
          public String getTime() {
              return time;
          }
      
          public void setTime(String time) {
              this.time = time;
          }
      
          public String getState() {
              return state;
          }
      
          public void setState(String state) {
              this.state = state;
          }
      
          public String getInfo() {
              return info;
          }
      
          public void setInfo(String info) {
              this.info = info;
          }
      
          public String getProgress() {
              return progress;
          }
      
          public void setProgress(String progress) {
              this.progress = progress;
          }
      
          String info;
          String progress;
      
          public ProcessList() {
          }
      }
      View Code

       使用jdbc进行查看

      /**
           * 使用 show processlist 查看所有数据库连接
           */
          public static void processList(){
              List<ProcessList> list=new ArrayList<>();
              ProcessList processList=null;
              ResultSet rs=executeQuery("show processlist");
              try {
                  while (rs.next()){
                      processList=new ProcessList(
                              rs.getInt("id"),
                              rs.getString("user"),
                              rs.getString("host"),
                              rs.getString("db"),
                              rs.getString("command"),
                              rs.getString("time"),
                              rs.getString("state"),
                              rs.getString("info"),
                              rs.getString("progress")
                      );
                      list.add(processList);
                  }
              } catch (SQLException e) {
                  e.printStackTrace();
              }
              for (int i = 0; i <list.size() ; i++) {
                  System.out.println(list.get(i));
              }
          }
      View Code
    • 测试调用 conn.close 之后,连接的变化情况

     

     
/**
     * 测试 jdbc 连接的关闭情况
      */
      public static void testOpenCon(){
        //打开连接
         getOpenCon();
        try {
            //关闭连接
           CON.close();
            //如果CON不为空,已连接过
            if(CON!= null){
                //如果CON是false
                // isClosed()该方法判断是否关闭连接   返回true false  返回true是已经关闭  返回fasle在连接
                if(!CON.isClosed()){
                    System.out.println("Connection连接中");
                }
                else {
                    System.out.println("Connection已关闭");
                }
            }
            //如果CON为空,为初始话对象 CON= DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
            else {
                System.out.println("未初始化Connection对象");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
View Code
    • 测试如果不写 conn.close 而是调用 System.gc() 之后,连接的变化情况
        • System.gc()该方法是只是回收垃圾,比如StringBuffer进行回收,gc方法不是直接进行回收的,他还需要等待时间,没有关闭连接

    

    
 /**
     * 测试 jdbc 连接的关闭情况
     */
    public static void testOpenCon(){
        //打开连接
         getOpenCon();
        try {
            //关闭连接
            System.gc();
            //如果CON不为空,已连接过
            if(CON!= null){
                //如果CON是false
                // isClosed()该方法判断是否关闭连接   返回true false  返回true是已经关闭  返回fasle在连接
                if(!CON.isClosed()){
                    System.out.println("Connection连接中");
                }
                else {
                    System.out.println("Connection已关闭");
                }
            }
            //如果CON为空,为初始话对象 CON= DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
            else {
                System.out.println("未初始化Connection对象");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
View Code

     

 创建一张表,插入随机的 10w 条数据.

  • 使用存储过程的方式插入数据,存储过程的参数是插入数据的条数
  • 需要使用到生成随机字符串的功能,使用一个函数来实现
  • [扩展,可选] 了解 out 类型的参数
  • [扩展,可选] 了解游标的使用
  • [扩展,可选] 使用 jdbc 调用 mysql 存储过程,详细了解

   

DROP database if exists randdatabase;
-- 创建数据库
create database randdatabase;
-- 打开数据库
use randdatabase;
-- 创建表
create table  randtable(
id int primary key auto_increment,
str varchar(62), -- 字符串
sums int                -- 字符串长度
);

DELIMITER $$ 
-- n随机个数
CREATE FUNCTION `rand_string` (n INT) RETURNS VARCHAR(255) CHARSET 'utf8'
BEGIN 
        -- 首先定义所有字母和数字,包括大小写数字
    DECLARE char_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        -- 定义接收字符串
    DECLARE return_str varchar(255) DEFAULT '';
        --    用于计数
    DECLARE i INT DEFAULT 0;
    WHILE i < n DO
                    -- concat('abc',d) 在abc上加d  得到abcd
                    --    substring(str,5,1)从第5个取1个
                    -- 用return_str接收字符
        SET return_str = concat(return_str, substring(char_str, FLOOR(1 + RAND()*62), 1));
        --    用i进行计数,当i等于n的时候,就会跳出
        SET i = i+1;
    END WHILE;
    -- 返回字符串
    RETURN return_str;
END $$
delimiter ;
-- 定好结束符为"$$"
delimiter $$
-- 创建存储过程
create procedure randtableadd(n int)
begin
        -- 关闭事务
        set autocommit=0;
        -- 用于计次数
        set @i = 0;
        -- 用于接收随机生成个数
         set @s = 0;
         -- 进行循环n次
     WHILE @i < n DO
             -- 生成随机数字1-62之间
            set @s=floor(1+rand()*62);
            -- 添加到表中
            insert into randtable(str,sums) values(rand_string(@s),@s);
            -- 进行加i
            set @i=@i+1;
            -- 退出
     END WHILE;
     -- 提交事务
    commit;
            select '成功录入';
end $$
delimiter ;

call randtableadd(100000);


        
          

猜你喜欢

转载自www.cnblogs.com/weibanggang/p/9660479.html