62,623
社区成员
发帖
与我相关
我的任务
分享
DBPool db = null;
DBPool db_ = (DBPool) session.getAttribute("dbpool");
if (db_ == null) {
db = DBPool.getInstance();
} else {
db = db_;
}
session.setAttribute("dbpool", db);
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.beans.PropertyVetoException;
/**
* DBPool.java User: wanghy Date: 2008-1-9
*/
public class DBPool {
static String DriverClass="oracle.jdbc.driver.OracleDriver";
static String JdbcUrl="jdbc:oracle:thin:@192.168.***.***:1521:ora9i";
static String DatabaseUsername="bookweb";
static String DatabasePassword="bookweb";
static int MaxPoolSize=40;
static int MinPoolSize=5;
static int MaxStatement=140;
private static DBPool dbPool=null;
private ComboPooledDataSource dataSource;
static {
if(dbPool==null)
dbPool = new DBPool();
}
private DBPool() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("bookweb");
dataSource.setPassword("bookweb");
dataSource.setJdbcUrl(JdbcUrl);
dataSource.setDriverClass(DriverClass);
dataSource.setInitialPoolSize(5);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(50000);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static DBPool getInstance() {
return dbPool;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接", e);
}
}
DBPool db = null;
DBPool db_ = (DBPool) session.getAttribute("dbpool");
if (db_ == null) {
db = DBPool.getInstance();
} else {
db = db_;
}
session.setAttribute("dbpool", db);
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.bao.config.Config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.bao.config.Config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* C3P0连接池
*/
public class ConnectionFactory {
private ConnectionFactory(){
}
private static ComboPooledDataSource ds = null;
static {
try {
Logger log = Logger.getLogger("com.mchange");
log.setLevel(Level.WARNING);
ds = new ComboPooledDataSource();
// 设置JDBC的Driver类
ds.setDriverClass(Config.getDriverClass());
// 设置JDBC的URL
ds.setJdbcUrl(Config.getJdbcUrl());
// 设置数据库的登录用户名
ds.setUser(Config.getDatabaseUsername());
// 设置数据库的登录用户密码
ds.setPassword(Config.getDatabasePassword());
// 设置连接池的最大连接数
ds.setMaxPoolSize(Config.getMaxPoolSize());
// 设置连接池的最小连接数
ds.setMinPoolSize(Config.getMinPoolSize());
// 设置连接的失效时间
ds.maxIdleTime(Config.getMaxIdleTime());
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public static synchronized Connection getConnection() {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
return con;
}
// C3P0 end
}