Posted on 2007-08-28 09:47
天空苍茫 阅读(2843)
评论(0) 编辑 收藏 所属分类:
java知识 、
hibernate
由于在JAVA自动获得新闻,没有建立web的环境,将建立的服务器放置在一台服务器上,数据库放在另一台服务器上,结果造成使用JDBC速度缓慢,因此修改成连接池:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.log4j.Logger;
/**
* @author Administrator
*
*/
public class DBConnectionFactory {
/**
* Logger for this class
*/
private static final Logger logger = Logger
.getLogger(DBConnectionFactory.class);
private static ObjectPool connectionPool=null;
private static String sqlJdbc = "";
private static String sqlUser = "";
private static String sqlPwd = "";
@SuppressWarnings("unchecked")
private static Class driverClass=null;
public static void proDBConnectionFactory(){
if(sqlJdbc.equals("")){
sqlJdbc = "jdbc:jtds:sqlserver://27.0.0.1:1433/finance;tds=8.0;lastupdatecount=true";
}
if(sqlUser.equals("")){
sqlUser = "sa";
}
if(sqlPwd.equals("")){
sqlPwd = "11111";
}
initDataSource();
}
public synchronized static void initDataSource(){
if(driverClass == null){
try {
driverClass = Class.forName("net.sourceforge.jtds.jdbc.Driver");
} catch (ClassNotFoundException e) {
logger.error("在注册驱动名称的时间发生错误:"+e.getMessage(),e);
}
}
if(connectionPool == null){
setupDriver(sqlJdbc);
//printDriverStats();
}else{
System.out.print("连接池已经存在");
}
try {
// printDriverStats();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn(){
proDBConnectionFactory();
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:FinanceDBPool");
} catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void printDriverStats() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
ObjectPool connectionPool = driver.getConnectionPool("FinanceDBPool");
System.out.println("活动的连接: " + connectionPool.getNumActive());
System.out.println("空闲的连接: " + connectionPool.getNumIdle());
}
public static void setupDriver(String connectURI){
try {
connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,sqlUser,sqlPwd);
@SuppressWarnings("unused")
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
//driver.registerPool("FinanceDBPool",connectionPool);
driver.registerPool("FinanceDBPool",connectionPool);
} catch (ClassNotFoundException e) {
logger.error("注册连接池的时间发生错误:"+e.getMessage(),e);
} catch (SQLException e) {
logger.error("注册连接池的时间发生错误:"+e.getMessage(),e);
}
}