最近做项目的过程中涉及到一个应用程序连接两个不同的数据库。
我们知道在oracle数据库中,一个数据库可以有多个实例,但一个实例一般只装载一个数据库文件。
针对项目的具体情况,我的解决办法是使用单例模式。
小知识补充:
什么是单例模式?
顾名思义就是只有一个实例。
作为对象的创建模式[GOF95], 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。
单例模式的三个要点:
一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。
现整理如下:
1.一个.JAVA文件创建两个数据库实例
DataSourceFactory.java
package com.lk.util;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.lk.common.Const;
/**
*
* 管理类DataSourceFactory支持对一个 池的访问.客户程序可以调用getDataSourceFactory()方法访问本类的唯一实例.
*/
public class DataSourceFactory {
private static DataSourceFactory dsf = null;
public Log log;
private static SharedPoolDataSource tds = new SharedPoolDataSource();
private static SharedPoolDataSource tdspub = new SharedPoolDataSource();
/**
* 返回唯一实例.如果是第一次调用此方法,则创建实例
*
* @return DataSourceFactory 唯一实例
*/
public static synchronized DataSourceFactory getDataSourceFactory() {
if (dsf == null) {
dsf = new DataSourceFactory();
}
return dsf;
}
public DataSourceFactory() {
init();
}
/**
* 读取属性完成初始化 创建新的连接
*
*/
private void init() {
log = LogFactory.getLog(this.getClass());
try {
Properties dbProps = new Properties();
try {
String configs = Const.CONFIGRATION_PATH + "dbcpConfig.properties";
FileInputStream is = new FileInputStream(configs);
dbProps.load(is);
} catch (Exception e) {
log.error("不能读取属性文件. " + "请确保*.properties在CLASSPATH指定的路径中");
log.fatal("类init出错:" + e);
return;
}
// 调用DBCP的使用DBCP的方法,完成DBCP的使用
createPools(dbProps);
} catch (Exception e) {
log.fatal("类init出错:" + e);
}
}
/**
* 设置属性文件读入到DBCP中获得连接
*/
@SuppressWarnings("rawtypes")
private void createPools(Properties props) {
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
if (name.endsWith(".drivers")) {
String poolName = name.substring(0, name.lastIndexOf("."));
DriverAdapterCPDS cpds = new DriverAdapterCPDS();
String url = props.getProperty(poolName + ".url");
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxActive = props.getProperty(poolName + ".maxActive");
String maxWait = props.getProperty(poolName + ".maxWait");
String drivers = props.getProperty(poolName + ".drivers");
try {
cpds.setDriver(drivers);
cpds.setUrl(url);
cpds.setUser(user);
cpds.setPassword(password);
int maxacti = 30;
int maxwa = 3;
try {
maxacti = Integer.valueOf(maxActive).intValue();
maxwa = Integer.valueOf(maxWait).intValue();
} catch (Exception e) {
log.info("数据库属性配置文件出错,maxActive或者maxWait非数字:" + e);
}
if ("test1".equalsIgnoreCase(poolName))
{
tds.setConnectionPoolDataSource(cpds);
tds.setMaxActive(maxacti);
tds.setMaxWait(maxwa);
}
else if ("test2".equalsIgnoreCase(poolName))
{
tdspub.setConnectionPoolDataSource(cpds);
tdspub.setMaxActive(maxacti);
tdspub.setMaxWait(maxwa);
}
} catch (ClassNotFoundException e) {
log.fatal("没有找到类:" + e);
}
}
}
}
/*
* 获得连接
*/
public Connection getConnection() {
try {
Connection conn = tds.getConnection();
conn.setAutoCommit(false);
return conn;
} catch (SQLException e) {
log.fatal("获取数据库连接出错:" + e);
}
return null;
}
/*
* 获得连接
*/
public Connection getPubConnection() {
try {
Connection conn = tdspub.getConnection();
conn.setAutoCommit(false);
return conn;
} catch (SQLException e) {
log.fatal("获取数据库连接出错:" + e);
}
return null;
}
}
2.数据库配置文件
dbcpConfig.properties
test1.drivers=oracle.jdbc.driver.OracleDriver
test1.url=jdbc:oracle:thin:@192.168.1.205:1521:orcl
test1.user=OGDS
test1.password=OGDS
test1.initialSize=30
test1.minIdle=5
test1.maxIdle=5
test1.maxWait=1000
test1.maxActive=50
test2.drivers=oracle.jdbc.driver.OracleDriver
test2.url=jdbc:oracle:thin:@192.168.1.205:1521:orcl
test2.user=LK
test2.password=LK
test2.initialSize=30
test2.minIdle=5
test2.maxIdle=5
test2.maxWait=1000
test2.maxActive=50