zoukankan      html  css  js  c++  java
  • 数据库连接池的高效性之时间提升

    数据库连接池的高效性

     

     

    测试数据库直接打开与使用连接池打开时间长短,连接1000次,看各自需要的时间。

    结果图

    1、直接打开花费时间(s):7333
    2、连接池打开花费时间(s):69
    3、速度提升倍数:106

    一、主函数

      package ch6.sql;

    import java.sql.*;

    public class Test_ConnectionPool_Time {

    public static void main(String[] args) {

      long t1=0,len1=0,len2=0;

      t1=System.currentTimeMillis();

      for(int i=0;i<1000;++i)

      {

         Connection con=SqlConnect01.getConnect();

         try {

          con.close();

       } catch (SQLException e) {

          e.printStackTrace();

       }

      }

      len1=System.currentTimeMillis()-t1;

      System.out.println("1、直接打开花费时间(s):"+len1);

      t1=System.currentTimeMillis();

      Test_Pool test=new Test_Pool();

      for(int i=0;i<1000;++i)

      {

         Connection con=test.getConnection();

         test.putConnection(con);

      }

      test.over();

      len2=System.currentTimeMillis()-t1;

      System.out.println("2、连接池打开花费时间(s):"+len2);

      System.out.println("3、速度提升倍数:"+len1/len2);

    }

    }

    二、创建连接池类

       package ch6.sql;

    import java.sql.*;

    import java.util.LinkedList;

    public class Test_Pool {

        LinkedList<Connection> list;

      public Test_Pool(){

         list=new LinkedList<Connection>();

         for(int i=0;i<10;++i)

         {

          try {

             Class.forName("com.mysql.jdbc.Driver");

             Connection con=DriverManager

             .getConnection("jdbc:mysql://localhost:3306/factory","root","mysql");

             list.add(con);

           } catch (Exception e) {

             e.printStackTrace();

           }

         }

      }

      public synchronized Connection getConnection(){

         if(list.size()>0)

         {

            return list.removeFirst();

         }else

            return null;

      }

      public synchronized void putConnection(Connection con){

         list.add(con);

      }

      public void over(){

         for(int i=0;i<list.size();++i)

          try {

             list.get(i).close();

          } catch (SQLException e) {

             e.printStackTrace();

          }

      }

    }

    三、直接连接程序

    package ch6.sql;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.SQLException;

    public class SqlConnect01 {

           // 定义MySQL的数据库驱动程序

                  public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;

                  // 定义MySQL数据库的连接地址

                  public static final String DBURL = "jdbc:mysql://localhost:3306/factory" ;

                  // MySQL数据库的连接用户名

                  public static final String DBUSER = "root" ;

                  // MySQL数据库的连接密码

                  public static final String DBPASS = "mysql" ;

                  public static Connection getConnect(){

                         Connection conn = null ;          // 数据库连接

                         try{

                                Class.forName(DBDRIVER) ;    // 加载驱动程序

                         }catch(ClassNotFoundException e){

                                e.printStackTrace() ;

                         }

                         try{

                                conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;                           

                         }catch(SQLException e){

                                e.printStackTrace() ;

                         }

                         return conn;

                  }

    }

  • 相关阅读:
    Mysql事务隔离级
    51nod1076(tarjan)
    求无向图的割点和桥模板(tarjan)
    51nod1770(xjb)
    51nod1640(kruscal)
    51nod1639(组合数学)
    51nod1625(枚举&贪心)
    51nod1562(set&模拟)
    51nod1483(打表)
    51nod1475(贪心&枚举)
  • 原文地址:https://www.cnblogs.com/duange/p/5941431.html
Copyright © 2011-2022 走看看