数据库连接池

来源:互联网 发布:sandboxie相同软件 编辑:程序博客网 时间:2024/05/21 19:57

关于数据库连接池的使用,首先我们要明白我们为什么要用它,对应普通的数据库连接操作,通常会涉及到以下一些操作是比较耗时的:

网络通讯,涉及到网络延时及协议通讯身份验证,涉及安全性检查连接合法性检查,主要是检查所连接的数据库是否存在并发控制机制构造并初始化输出缓冲区连接成功后的信息保存,日志存储服务器性能数据库配置优化系统分配内存资源等等~~~状况,导致数据库连接操作比较耗时,~~~而且每次都得花费0.05s~1s的时间但是使用连接池技术,本质上就是在一个请求对应的连接,都由一个线程池来维护着,也就是说“上下文切换”的代价是线程级别(所谓的纳秒级),对于大规模的并发访问,就算以每秒几亿级别的访问量都是不成问题的。

一、在Java平台

之前一段时间曾经弄过数据库连接池,使用的是c3p0这个工具包,

简单记录下c3p0的使用,当作学习的一个巩固;

第一步导入jar包;

第二步:

把jar包导入进来:

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;

第三步:定义变量

private ComboPooledDataSource cpds; 

然后在DbUtil里面使用:

private DbUtil() {
cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass( driver);
cpds.setJdbcUrl( url );
cpds.setUser(user);
cpds.setPassword(passwd);

cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(30);
cpds.setMaxIdleTime(60);

} catch (PropertyVetoException e) {
e.printStackTrace();
}
}

最后每个连接里面都可以用上了:

public Connection getConn() {
try {
return cpds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

完整的使用类:

View Code
package util;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.mchange.v2.c3p0.ComboPooledDataSource;import com.mchange.v2.c3p0.DataSources;public class DbUtil {    private static String url = "jdbc:mysql://localhost:3306/zdbang_net";    private static String user = "root";    private static String passwd = "admin";    private static String driver = "com.mysql.jdbc.Driver";        private ComboPooledDataSource cpds;     private static DbUtil db = new DbUtil();        public static DbUtil getInstance() {        return db;    }        private DbUtil() {         cpds = new ComboPooledDataSource();        try {            cpds.setDriverClass( driver);            cpds.setJdbcUrl( url );            cpds.setUser(user);                                              cpds.setPassword(passwd);                          cpds.setMinPoolSize(5);                                                 cpds.setAcquireIncrement(5);            cpds.setMaxPoolSize(30);            cpds.setMaxIdleTime(60);                    } catch (PropertyVetoException e) {            e.printStackTrace();        }       }        public Connection getConn() {          try {            return cpds.getConnection();        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }        protected void finalize() throws Throwable {          DataSources.destroy(cpds);          super.finalize();    }        public static void closeConn(Connection conn) {        try {            if (conn != null)                conn.close();        } catch (SQLException e) {            e.printStackTrace();        }    }    public static Statement getStmt(Connection conn) {        Statement stmt = null;        try {            stmt = conn.createStatement();            return stmt;        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }    public static void closeStmt(Statement stmt) {        try {            if (stmt != null)                stmt.close();        } catch (SQLException e) {            e.printStackTrace();        }    }    public static PreparedStatement getPs(Connection conn, String sql) {        PreparedStatement ps = null;        try {            ps = conn.prepareStatement(sql);            return ps;        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }    public static void closePs(PreparedStatement ps) {        try {            if (ps != null)                ps.close();        } catch (SQLException e) {            e.printStackTrace();        }    }    public static ResultSet doQuery(Statement stmt, String sql) {        try {            ResultSet rs = stmt.executeQuery(sql);            return rs;        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }    public static void closeRs(ResultSet rs) {        try {            if (rs != null)                rs.close();        } catch (SQLException e) {            e.printStackTrace();        }    }    }

具体的源码还没具体看,不过估计实现上也不会太难,争取这段时间看一下源代码。~~~~~

 二、在.Net

没有使用数据库连接池的情况是这样的:每当一个请求过来根据连接字符串新建一个连接,请求结束,数据库连接关闭。

例如:

View Code
        public static int ExecuteSql(string SQLString)        {            using (SqlConnection connection = new SqlConnection(connectionString))            {                using (SqlCommand cmd = new SqlCommand(SQLString, connection))                {                    try                    {                        connection.Open();                        cmd.CommandTimeout = 600;                        int rows = cmd.ExecuteNonQuery();                        return rows;                    }                    catch (System.Data.SqlClient.SqlException e)                    {                        LogHelper.Log("sqlexception;sql:" + SQLString, e);                        log.Error("\r\n客户机IP:" + HttpContext.Current.Request.UserHostAddress                            + "\r\n错误地址:" + HttpContext.Current.Request.Url + "\r\n", e);                        connection.Close();                        throw e;                    }                }            }        }

请求的字符串就是:connectionString

然后每一个连接字符串都会当作是一个新的连接请求。

这样子对于一些小型的web项目还是可以的,毕竟并发量不会太大。但是对于大型网站,访问量很大,切换连接的开销就会非常的大,对系统而言是一个很大的瓶颈;而使用连接池比较只是在线程基本把连接给屏蔽掉(纳秒级),所以使用连接池对整个网站而言是有很大的好处的;

 

 

 

 

 转自:http://www.cnblogs.com/super-d2/archive/2013/01/24/2875646.html

 

0 0
原创粉丝点击