码迷,mamicode.com
首页 > 数据库 > 详细

数据库连接池的高效性之时间提升

时间:2016-10-09 14:23:58      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

数据库连接池的高效性

 

 

测试数据库直接打开与使用连接池打开时间长短,连接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;

              }

}

 

数据库连接池的高效性之时间提升

标签:

原文地址:http://www.cnblogs.com/duange/p/5941431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!