什么是数据库连接池
连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用
数据库连接池有什么用
数据库连接池负责分配,管理和释放数据库连接,
它允许应用程序重复使用一个现有的数据库连接,
而不是在重新建立一个,
释放空间时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库链接遗漏。
这项技术明显提高对数据库操作的性能。
有哪些可用的连接池
- 现在很多web服务器都提供了DataSoruce的实现,即连接池的实现,
通常我们把DataSource的实现,,数据源中包含了数据库连接池的实现 - DBCP数据库连接池
- C3P0 数据库连接池
DBCP数据库连接池
- DBCP 是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文件
- Commons-dbcp.jar:连接池的实现
- Commons-pool.jar:连接池实现的依赖库
DBCP 实例代码
1,src 添加文件dbcpconfig.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=REPEATABLE_READ
2,DBCPUtil 封装
public class DBCPUtil {
private static DataSource ds = null;
static {
Properties prop = new Properties();
try {
prop.load(DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
ds = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("服务器忙");
}
}
public static void release(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
3,DBCPUtil 使用
Connection conn = DBCPUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement("select * from user");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBCPUtil.release(conn, ps, rs);
}
C3P0数据库连接池
- 使用C3P0需要添加 c3p0-0.9.1.2.jar
1,src 添加配置文件c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8</property>
<property name="user">root</property>
<property name="password"></property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
</c3p0-config>
C3P0Util 封装
public class C3P0Util {
//得到一个数据源
private static DataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("服务器错误");
}
}
public static DataSource getDataSource() {
return dataSource;
}
public static void setDataSource(DataSource dataSource) {
C3P0Util.dataSource = dataSource;
}
public static void release(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
C3P0Util 使用
PreparedStatement ps = null;
ResultSet rs = null;
Connection conn = C3P0Util.getConnection();
try {
ps = conn.prepareStatement("select * from user");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
C3P0Util.release(conn, ps, rs);
}
优化jdbc
- 从连接数据库优化
- 查询输出结果优化
2016.10.29
网友评论