至于Java数据库连接池的原理就不细说了,百度一下大把。在这里只是把个人学习中的结果积累下来。
代码上也基本都有注释。
首先写一个读取本地数据库驱动,数据库用户名、数据库密码、连接数的类。
- XML/HTML 代码复制内容到剪贴板
-
- package cn.cate.utils;
-
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Properties;
-
- public class PoolProperties {
-
- private Properties properties;
- private String driver;
- private String url;
- private String user;
- private String password;
- private int maxConn;
- private int minConn;
-
- public PoolProperties(){
-
properties = new Properties();
-
String path = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
-
pathpathpath = path.substring(6, path.length());
-
if (path.indexOf("WEB-INF") > 0) {
-
pathpathpath = path.substring(0, path.indexOf("/WEB-INF") + 9) + "lib/";
- }else{
-
path = "d:";
- }
- try {
- this.properties.load(new FileReader(path + "db.pro"));
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- this.setDriver(properties.getProperty("driver"));
- this.setUrl(properties.getProperty("url"));
- this.setUser(properties.getProperty("user"));
- this.setPassword(properties.getProperty("password"));
- this.setMaxConn(Integer.parseInt(properties.getProperty("maxConn")));
- this.setMinConn(Integer.parseInt(properties.getProperty("minConn")));
- }
-
-
- public Properties getProperties() {
- return properties;
- }
-
- public void setProperties(Properties properties) {
-
this.properties = properties;
- }
-
- public String getDriver() {
- return driver;
- }
-
- public void setDriver(String driver) {
-
this.driver = driver;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
-
this.url = url;
- }
-
- public String getUser() {
- return user;
- }
-
- public void setUser(String user) {
-
this.user = user;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
-
this.password = password;
- }
-
- public int getMaxConn() {
- return maxConn;
- }
-
- public void setMaxConn(int maxConn) {
-
this.maxConn = maxConn;
- }
-
- public int getMinConn() {
- return minConn;
- }
-
- public void setMinConn(int minConn) {
-
this.minConn = minConn;
- }
-
-
- }
db.pro的内容如下:
driver=com.mysql.jdbc.Driver maxConn=10 minConn=1 url=jdbc:mysql://127.0.0.1:3306/cate user=root password=11111
然后定义一个数据库连接池:DBConnectionPool
- Java 代码复制内容到剪贴板
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.Iterator;
-
import java.util.List;
-
-
-
-
-
-
public class DBConnectionPool {
-
-
-
-
-
private List<Connection> freeConnections = new ArrayList<Connection>();
-
private Connection conn = null;
-
private int connect = 0;
-
private int maxConn;
-
private String driver;
-
private String url;
-
private String user;
-
private String password;
-
-
public DBConnectionPool(String driver,String URL,String user,String password,int maxConn){
-
this.driver = driver;
-
this.url = URL;
-
this.user = user;
-
this.password = password;
-
this.maxConn = maxConn;
- poolInfo();
- }
-
-
-
-
-
private void poolInfo(){
-
Connection conn = this.newConnection();
- freeConnections.add(conn);
-
for(int i = 0;i < this.maxConn - 1;i++){
- Connection freeConn = conn;
- freeConnections.add(freeConn);
- }
- }
-
-
-
-
-
public synchronized void freeConnection(Connection conn){
-
this.freeConnections.add(conn);
-
this.connect--;
- }
-
-
-
-
-
public synchronized Connection getConnection(){
-
if(this.freeConnections.size() > 0){
-
-
-
-
conn = this.freeConnections.get(0);
-
this.freeConnections.remove(0);
- }
-
-
-
-
if(conn == null){
- conn = getConnection();
-
}else{
- conn = newConnection();
- }
-
if(this.maxConn == 0 || this.maxConn < this.connect){
-
-
-
-
conn = null;
- }
-
if(conn != null){
-
this.connect++;
- }
-
return conn;
- }
-
-
-
-
-
public synchronized void release(){
-
Iterator<Connection> allConns = this.freeConnections.iterator();
-
while(allConns.hasNext()){
- Connection conn = (Connection)allConns.next();
-
try{
-
if(null != conn){
- conn.close();
- }
-
conn = null;
-
}catch(SQLException e){
- e.printStackTrace();
- }
- }
-
this.freeConnections.clear();
- }
-
-
-
-
-
private Connection newConnection(){
-
try{
- Class.forName(driver);
-
}catch(ClassNotFoundException e2){
- e2.printStackTrace();
- }
-
try{
- conn = DriverManager.getConnection(url,user,password);
-
}catch(SQLException e3){
- e3.printStackTrace();
- System.exit(0);
- }
-
return conn;
- }
- }
最后提供一个连接池的管理类:
- Java 代码复制内容到剪贴板
-
-
import java.sql.Connection;
-
import java.util.Enumeration;
-
import java.util.Hashtable;
-
-
import cn.cate.utils.PoolProperties;
-
-
-
-
-
-
public class DBConnectionManager {
-
-
-
-
-
-
private static DBConnectionManager instance;
-
-
-
-
-
private Hashtable<String,DBConnectionPool> pools = new Hashtable<String,DBConnectionPool>();
-
private static String poolName = "MYSQL_MZBA";
-
-
public static synchronized DBConnectionManager getInstance(){
-
if(instance == null){
-
instance = new DBConnectionManager();
- }
-
return instance;
- }
-
-
-
-
-
private DBConnectionManager(){
-
this.init();
- }
-
-
-
-
-
private void init(){
-
PoolProperties poolProperties = new PoolProperties();
-
DBConnectionPool pool = new DBConnectionPool(poolProperties.getDriver(), poolProperties.getUrl(),
- poolProperties.getUser(), poolProperties.getPassword(), poolProperties.getMaxConn());
- pools.put(poolName, pool);
- }
-
-
-
-
-
public Connection getConnection(){
-
DBConnectionPool pool = null;
-
Connection conn = null;
- pool = pools.get(poolName);
-
try{
- conn = pool.getConnection();
-
}catch(Exception e){
- e.printStackTrace();
- }
-
return conn;
- }
-
-
-
-
public synchronized void freeConnection(Connection conn){
- DBConnectionPool pool = pools.get(poolName);
-
if(pool != null){
- pool.freeConnection(conn);
- }
- }
-
-
-
-
public synchronized void release(){
- Enumeration<DBConnectionPool> allpools = pools.elements();
-
while(allpools.hasMoreElements()){
- DBConnectionPool pool = allpools.nextElement();
-
if(pool != null){
- pool.release();
- }
- }
- pools.clear();
- }
-
- }
本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962458,如需转载请自行联系原作者