今天在网上看了一下数据库连接池方面的程序,感觉大多写的好长(当然功能可能很强大),我自已花了十分钟的时候也写了一个,也不知道写的对不对,大家看看,如果不对,请指正一下。我也不知道下面这个算不算是个连接池,别笑我:)
/*数据库连接池*/import java.sql.*;
import java.util.*;public class DBConnectionPool
{
private int size; //连接池中的连接数
private Vector<Connection> freeConnList; //空闲连接列表
public DBConnectionPool()
{
this(10);
}
public DBConnectionPool(int size)
{
this.size=size;
freeConnList=new Vector<Connection>(size);
init();
}
//初始化
private void init()
{
for(int i=0;i<size;i++)
{
try
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String sqlConn="jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;user=sa;password=123567";
Connection connection = DriverManager.getConnection(sqlConn);
connection.setCatalog("myDB");
freeConnList.addElement(connection);
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
//返回一个可用的连接
public synchronized Connection getConnection()
{
if(freeConnList.size()>0)
return (Connection)(freeConnList.remove(0));
else
return null;
}
//连接使用完毕,重新加入空闲列表
public void addFreeConnection(Connection conn)
{
freeConnList.addElement(conn);
}
//还有空闲的连接吗?
public synchronized boolean hasFreeConnection()
{
return freeConnList.size()>0;
}
//返回连接池大小
public int getSize()
{
return size;
}
public static void main(String[] args)
{
DBConnectionPool dbcp=new DBConnectionPool();
System.out.println("连接池大小:" + dbcp.getSize());
System.out.println("还有空闲:" + dbcp.hasFreeConnection());
}
}
/*数据库连接池*/import java.sql.*;
import java.util.*;public class DBConnectionPool
{
private int size; //连接池中的连接数
private Vector<Connection> freeConnList; //空闲连接列表
public DBConnectionPool()
{
this(10);
}
public DBConnectionPool(int size)
{
this.size=size;
freeConnList=new Vector<Connection>(size);
init();
}
//初始化
private void init()
{
for(int i=0;i<size;i++)
{
try
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String sqlConn="jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;user=sa;password=123567";
Connection connection = DriverManager.getConnection(sqlConn);
connection.setCatalog("myDB");
freeConnList.addElement(connection);
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
//返回一个可用的连接
public synchronized Connection getConnection()
{
if(freeConnList.size()>0)
return (Connection)(freeConnList.remove(0));
else
return null;
}
//连接使用完毕,重新加入空闲列表
public void addFreeConnection(Connection conn)
{
freeConnList.addElement(conn);
}
//还有空闲的连接吗?
public synchronized boolean hasFreeConnection()
{
return freeConnList.size()>0;
}
//返回连接池大小
public int getSize()
{
return size;
}
public static void main(String[] args)
{
DBConnectionPool dbcp=new DBConnectionPool();
System.out.println("连接池大小:" + dbcp.getSize());
System.out.println("还有空闲:" + dbcp.hasFreeConnection());
}
}
1。这个类最好是Singleton(单例)模式的。
2。连接个数要能动态增加/减少
3。扫尾工作(释放所有连接)进一步:定时扫描所谓的空闲连接是否真的可用(因为你的连接池不能保证与数据库的连接没时每刻都正常。例如,网络原因,数据库崩溃等等)
不太懂。
1、某个类只能有一个实例
2、必须自行创建这个实例
3、必须向整个系统提供这个实例 Singleton模式的实现方法
1、饿汉式singleton
public class EagerSingleton
{
private static final EagerSingleton m_instance = new Eagersingleton();
private Eagersingleton(){}public static EagerSingleton getInstance()
{
return m_instance;
}}2、懒汉式singleton
public class LazySingleton
{
private static LazySingleton m_instance = null;
private LazySingleton(){};
synchronized public static LazySingleton getInstance()
{
if( m_instance == null )
{
m_instance = new LazySingleton();
}
return m_instance;
}
}3、登记式singleton
import java.util.HashMap;
public class RegSingleton
{
static private HashMap m_registry = new HashMap();
static
{
RegSingleton x = new regSingleton();
m_registry.put(x.getClass().getName(), x);
}
protect RegSingleton(){}
static public RegSingleton getInstance(String name)
{
if(name == null )
{
name = "RegSingleton";
}
if(m_registry.get(name ) == null )
{
m_registry.put(name, Class.forName(name).newInstance();
}
catch(Exception e)
{
System.out.println("Error happened.");
}
return (RegSingleton)(m_registry.get(name));
}
}三种Singleton模式的比较饿汉式 类被加载时就被实例化。
懒汉式 类加载时,不被实例化,在第一次引用时实例化。
饿汉式、懒汉式都不能被继承
而登记式可以被继承。可以在Tomcat5.0的conf文件夹中的server.xml中设置连接池,具体请参照<<jsp应用开发详解>>这本书。