菜鸟问一个自己写数据源(数据连接池)的问题

gin_1234 2016-12-16 10:56:30
package com.abc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.ResourceBundle;



public class DataSourceV1 {
private static String initFileName="dbinfo";
private static String username=null;
private static String password=null;
private static String driverClass=null;
private static String url=null;
private static int connectionCount=0;
private static int maxConnectionCount=0;

private static LinkedList<Connection> idleConnections=null;
private static LinkedList<Connection> usedConnections=null;
static{
ResourceBundle rb =ResourceBundle.getBundle(initFileName);
username=rb.getString("username");
password=rb.getString("password");
driverClass=rb.getString("driverClass");
url=rb.getString("url");
connectionCount=Integer.parseInt(rb.getString("connectionCount"));
maxConnectionCount=Integer.parseInt(rb.getString("maxConnectionCount"));

try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
idleConnections=new LinkedList<Connection>();
usedConnections=new LinkedList<Connection>();
for (int i=0;i<connectionCount;i++){
try {
idleConnections.add(DriverManager.getConnection(url, username, password));
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库套接字错误");
}
}
}

/**
* 获得数据库连接方法
* @return
* @throws SQLException
*/
public static Connection getConnection(){
Connection conn=null;
if (idleConnections.size()>0){
conn=idleConnections.removeFirst();
usedConnections.addLast(conn);
}else if (usedConnections.size()<maxConnectionCount){
try {
conn=DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
usedConnections.addLast(conn);
}
return conn;
}

/**
* 关闭资源方法
* @param rs ResultSet
* @param st Statement
* @param connection Connection
*/
public static void closeAll(ResultSet rs,Statement st,Connection connection){
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (st!=null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null){
if (idleConnections.size()+usedConnections.size()>=connectionCount){
usedConnections.remove(connection);
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}else{
idleConnections.addLast(connection);
usedConnections.remove(connection);
}
}
}
}
我这种不使用装饰模式,直接调用Connection,Connection将在两个集合之间流转的想法可以实现吗?有没有什么问题?
...全文
130 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
gin_1234 2016-12-16
  • 打赏
  • 举报
回复

package com.pdbg.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.ResourceBundle;



public class DataSourceV1 {
	private static String initFileName="dbinfo";
	private static String username=null;
	private static String password=null;
	private static String driverClass=null;
	private static String url=null;
	private static int connectionCount=0;
	private static int maxConnectionCount=0;
	
	private static LinkedList<Connection> idleConnections=null;
	private static LinkedList<Connection> usedConnections=null;
	static{
		ResourceBundle rb =ResourceBundle.getBundle(initFileName);
		username=rb.getString("username");
		password=rb.getString("password");
		driverClass=rb.getString("driverClass");
		url=rb.getString("url");
		connectionCount=Integer.parseInt(rb.getString("connectionCount"));
		maxConnectionCount=Integer.parseInt(rb.getString("maxConnectionCount"));
		
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		idleConnections=new LinkedList<Connection>();
		usedConnections=new LinkedList<Connection>();
		for (int i=0;i<connectionCount;i++){
			try {
				idleConnections.add(DriverManager.getConnection(url, username, password));
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println("数据库套接字错误");
			}
		}
	}
	
	/**
	 * 获得数据库连接方法
	 * @return
	 * @throws SQLException 
	 */
	public static Connection getConnection(){
		Connection conn=null;
		if (idleConnections.size()>0){
			conn=idleConnections.removeFirst();
			usedConnections.addLast(conn);
		}else if (usedConnections.size()<maxConnectionCount){
			try {
				conn=DriverManager.getConnection(url, username, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			usedConnections.addLast(conn);
		}
		return conn;
	}
	
	/**
	 * 关闭资源方法
	 * @param rs ResultSet
	 * @param st Statement
	 * @param connection Connection
	 */
	public static void closeAll(ResultSet rs,Statement st,Connection connection){
		if (rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (st!=null){
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (connection!=null){
			if (idleConnections.size()+usedConnections.size()>=connectionCount){
				usedConnections.remove(connection);
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else{
				idleConnections.addLast(connection);
				usedConnections.remove(connection);
			}
		}
	}
}


67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧