JDBC方象坿(DataSource)議酒汽糞?
扮寂:2011-07-25 51cto鴬人 leizhimin
方象坿室宝頁Java荷恬方象垂議匯倖載購囚室宝?送佩議隔消晒崇尺脅宣音蝕方象坿議哘喘。
方象坿戻工阻匯嶽酒汽資函方象垂銭俊議圭塀?旺嬬壓坪何宥狛匯倖学議字崙栖鹸喘方象垂銭俊?宸 劔祥寄寄受富幹秀方象垂銭俊議肝方?戻互阻狼由來嬬。
斤噐方象坿議哘喘?匯違脅僉夲糞喘蝕坿議方象坿賜方象垂銭俊学栖聞喘?曳泌?械需議嗤DBCP、 C3P0、Proxool吉吉。徽喘軟栖嗤乂運嶷才醍**。和中徭失返強糞?倖娼酒議方象坿?旗鷹泌和?
package com.lavasoft.simpledatesource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.sql.DataSource;
import java.util.Collections;
import java.util.LinkedList;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.io.PrintWriter;
/**
* 匯倖酒汽議DataSource糞?
*
* @author leizhimin 2010-1-14 0:03:17
*/
public class SimpleDateSource implements DataSource {
private static Log log = LogFactory.getLog (SimpleDateSource.class);
private static final String dirverClassName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://127.0.0.1:3306/testdb";
private static final String user = "root";
private static final String pswd = "leizhimin";
//銭俊学
private static LinkedList<Connection> pool = (LinkedList<Connection>) Collections.synchronizedList(new LinkedList<Connection>());
private static SimpleDateSource instance = new SimpleDateSource ();
static {
try {
Class.forName(dirverClassName);
} catch (ClassNotFoundException e) {
log.error("孀音欺駁強窃?", e);
}
}
private SimpleDateSource() {
}
/**
* 資函方象坿汽箭
*
* @return 方象坿汽箭
*/
public SimpleDateSource instance() {
if (instance == null) instance = new SimpleDateSource();
return instance;
}
/**
* 資函匯倖方象垂銭俊
*
* @return 匯倖方象垂銭俊
* @throws SQLException
*/
public Connection getConnection() throws SQLException {
synchronized (pool) {
if (pool.size() > 0) return pool.removeFirst();
else return makeCo
|