数据库连接池:
就是将链接数据库的链接保存到一个池子中,用的时候从池子中获取数据库连接,不用的时候将其放到池子中国,。
连接池就是用来存放数据库的链接。
传统方式:
要连接数据库的时候,获取数据库,然后执行操作,用完之后,关闭数据库连接。
连接池方式:
在启动数据库驱动的时候,就在连接池中存放几条数据库的链接,应用程序要操作数据库的时候,只要去池子中获取连接,执行操作,之后将链接放在池子中,就可以了。
原因:用户每次请求都需要向数据库获得连接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长。
自己自定义数据库连接池:
dataSource.properties配置文件:
jdbcUrl:jdbc:mysql://localhost:3306/jdbc
user:root
password:root
driverName:com.mysql.jdbc.Driver
poolSize:5
自定义数据库连接池:
package com.enterise.always.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;
import javax.sql.DataSource;
/**
* 数据库连接池
*
* @author Always
*
*/
public class MyDataSource implements DataSource {
// 鏈錶連接池
private static LinkedList<Connection> connPool = new LinkedList<Connection>();
private static LinkedList<Connection> prePool = new LinkedList<Connection>();
private static int poolSize = 5;
private static Properties properties = new Properties();
static {
// 1.获取配置信息
InputStream inStream = MyDataSource.class.getClassLoader()
.getResourceAsStream("/dataSource.properties");
// 2.加载配置信息资源
try {
properties.load(inStream);
inStream.close();
// 3.注册驱动器
Class.forName(properties.getProperty("driverName"));
// 4获取数据库连接数
poolSize = Integer.parseInt(properties.getProperty("poolSize"));
// 5.批量添加数据库链接
addConnNum();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 添加数据库链接
*
* @throws SQLException
*/
public static void addConnNum() throws SQLException {
for (int i = 0; i < poolSize; i++) {
Connection connection = DriverManager.getConnection(properties
.getProperty("jdbcUrl"), properties.getProperty("user"),
properties.getProperty("password"));
connPool.add(connection);
}
}
public Connection getConnection() throws SQLException {
Connection conn = null;
if (connPool.size() > 0) {
conn = connPool.getFirst();
prePool.add(conn);
connPool.removeFirst();
} else {
// 再添加链接数
addConnNum();
// 再次链接
getConnection();
}
return conn;
}
/**
* 关闭链接
*
* @param conn
*/
public void closeConn(Connection conn) {
connPool.add(conn);
prePool.remove(conn);
}
public Connection getConnection(String username, String password)
throws SQLException {
return null;
}
public PrintWriter getLogWriter() throws SQLException {
return null;
}
public int getLoginTimeout() throws SQLException {
return 0;
}
public void setLogWriter(PrintWriter out) throws SQLException {
}
public void setLoginTimeout(int seconds) throws SQLException {
}
public boolean isWrapperFor(Class arg0) throws SQLException {
return false;
}
public Object unwrap(Class arg0) throws SQLException {
return null;
}
}
开源的数据库连接池:
DBCP 数据库连接池
C3P0 数据库连接池
DBCP:
DBCP 是 Apache 软件基金组织下的开源连接池实现.
jar包:
Commons-dbcp.jar,Commons-pool.jar
private static BasicDataSource dataSource = new BasicDataSource();
//设置数据源的参数。
dataSource.setUrl("jdbc:mysql://localhost:3306/jdbc");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setInitialSize(10);
或者:
InputStream inStream = DBCPDataSource.class.getClassLoader().getResourceAsStream("/dataSource.properties");
Properties properties = new Properties();
try {
properties.load(inStream);
inStream.close();
DataSource ds = BasicDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
C3P0:
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
static {
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc");
dataSource.setPassword("root");
dataSource.setUser("root");
}
或者:
private static ComboPooledDataSource dataSource = new ComboPooledDataSource("/dataSource.properties");