用了C3P0连接池,没用C3p0配置文件,就写了这一个类连接类如下所示连接了第一个数据源,第二个就连接不上了。好像有冲突?我把第二个分离出来,另外写一个单例还是不行。。要怎么写才能连接两个数据库啊?
public class DBPool {
private static Logger log = Logger.getLogger(DBPool.class);
private static DBPool dbPool;
private ComboPooledDataSource dataSource;
private ComboPooledDataSource cdnDataSource;
private String dbUrl = null;
private String dbUser = null;
private String dbPwd = null;
private String cdnUrl = null;
private String cdnUser = null;
private String cdnPwd = null;
static {
dbPool = new DBPool();
} public DBPool() {
try {
InputStream is = DBPool.class
.getResourceAsStream("/jdbc.properties");
Properties p = new Properties();
p.load(is);
dbUrl = p.getProperty("tdlive.url");
dbUser = p.getProperty("tdlive.username");
dbPwd = p.getProperty("tdlive.password");
String driver = p.getProperty("driver");
dataSource = new ComboPooledDataSource();
dataSource.setUser(dbUser);
dataSource.setPassword(dbPwd);
dataSource.setJdbcUrl(dbUrl);
dataSource.setDriverClass(driver);
dataSource.setInitialPoolSize(3);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
dataSource.setAcquireIncrement(5);
cdnUrl = p.getProperty("cdn.url");
cdnUser = p.getProperty("cdn.username");
cdnPwd = p.getProperty("cdn.password");
cdnDataSource = new ComboPooledDataSource();
cdnDataSource.setUser(cdnUser);
cdnDataSource.setPassword(cdnPwd);
cdnDataSource.setJdbcUrl(cdnUrl);
cdnDataSource.setDriverClass(driver);
cdnDataSource.setInitialPoolSize(3);
cdnDataSource.setMinPoolSize(1);
cdnDataSource.setMaxPoolSize(10);
cdnDataSource.setMaxStatements(50);
cdnDataSource.setMaxIdleTime(60);
cdnDataSource.setAcquireIncrement(5);
is.close();
} catch (PropertyVetoException e) {
log.error(e.getMessage());
throw new RuntimeException(e); } catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
} public final static DBPool getInstance() {
return dbPool;
} public final Connection getConnection() {
try {
log.debug("DBPool getconnection");
Connection conn = dataSource.getConnection();
return conn;
} catch (SQLException e) {
log.error("5:can't get the connection :" + e);
throw new RuntimeException("unable to connect to the database ", e);
}
}
public final Connection getCdnConnection() {
try {
log.debug("cdn DBPool getconnection");
Connection conn = cdnDataSource.getConnection();
return conn;
} catch (SQLException e) {
log.error("5:can't get the cdn connection :" + e);
throw new RuntimeException("unable to connect to the cdn database ", e);
}
}
}
public class DBPool {
private static Logger log = Logger.getLogger(DBPool.class);
private static DBPool dbPool;
private ComboPooledDataSource dataSource;
private ComboPooledDataSource cdnDataSource;
private String dbUrl = null;
private String dbUser = null;
private String dbPwd = null;
private String cdnUrl = null;
private String cdnUser = null;
private String cdnPwd = null;
static {
dbPool = new DBPool();
} public DBPool() {
try {
InputStream is = DBPool.class
.getResourceAsStream("/jdbc.properties");
Properties p = new Properties();
p.load(is);
dbUrl = p.getProperty("tdlive.url");
dbUser = p.getProperty("tdlive.username");
dbPwd = p.getProperty("tdlive.password");
String driver = p.getProperty("driver");
dataSource = new ComboPooledDataSource();
dataSource.setUser(dbUser);
dataSource.setPassword(dbPwd);
dataSource.setJdbcUrl(dbUrl);
dataSource.setDriverClass(driver);
dataSource.setInitialPoolSize(3);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
dataSource.setAcquireIncrement(5);
cdnUrl = p.getProperty("cdn.url");
cdnUser = p.getProperty("cdn.username");
cdnPwd = p.getProperty("cdn.password");
cdnDataSource = new ComboPooledDataSource();
cdnDataSource.setUser(cdnUser);
cdnDataSource.setPassword(cdnPwd);
cdnDataSource.setJdbcUrl(cdnUrl);
cdnDataSource.setDriverClass(driver);
cdnDataSource.setInitialPoolSize(3);
cdnDataSource.setMinPoolSize(1);
cdnDataSource.setMaxPoolSize(10);
cdnDataSource.setMaxStatements(50);
cdnDataSource.setMaxIdleTime(60);
cdnDataSource.setAcquireIncrement(5);
is.close();
} catch (PropertyVetoException e) {
log.error(e.getMessage());
throw new RuntimeException(e); } catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
} public final static DBPool getInstance() {
return dbPool;
} public final Connection getConnection() {
try {
log.debug("DBPool getconnection");
Connection conn = dataSource.getConnection();
return conn;
} catch (SQLException e) {
log.error("5:can't get the connection :" + e);
throw new RuntimeException("unable to connect to the database ", e);
}
}
public final Connection getCdnConnection() {
try {
log.debug("cdn DBPool getconnection");
Connection conn = cdnDataSource.getConnection();
return conn;
} catch (SQLException e) {
log.error("5:can't get the cdn connection :" + e);
throw new RuntimeException("unable to connect to the cdn database ", e);
}
}
}
解决方案 »
- ssh连接mysql超时
- 请问高手,你们用j2EE都开发什么样的程序??
- SPRING 都有那些版本呀
- 含函数的存储过程
- AXIS 中的java.lang.reflect.invocationtargetexception 急!!!
- 在下用Hibernate + Struts+mysql开放一个WEB程序时出现了一个错误。
- JAVA面试题:那位仁兄能做?
- 向师哥师姐求助——。。Lomboz插件下载!?
- 大家认为现在jboss最稳定,最快的是哪个版本
- 求救!在线等待中…………
- 免费支持jsp,php空间+数据库+tomcat+mysql+ftp 邀请码~~~~~~~
- 痛苦中,javaMail收发邮件问题
<property name="acquireIncrement">2</property>
<property name="initialPoolSize">5</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">100</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">0</property>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@XXX:1521:XXX</property>
<property name="user">XXX</property>
<property name="password">==XXX</property>
<property name="idleConnectionTestPeriod">60</property>
<property name="preferredTestQuery">select 1 from dual</property>
<property name="testConnectionOnCheckin">true</property>
<property name="testConnectionOnCheckout">true</property>
</default-config>
<named-config name="EntityDBConfig">
<property name="acquireIncrement">2</property>
<property name="initialPoolSize">5</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">100</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">0</property>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@XXX:1521:XXX</property>
<property name="user">XXX</property>
<property name="password">XXX</property>
<property name="idleConnectionTestPeriod">60</property>
<property name="preferredTestQuery">select 1 from dual</property>
<property name="testConnectionOnCheckin">true</property>
<property name="testConnectionOnCheckout">true</property>
</named-config>
this.source = new ComboPooledDataSource("EntityDBConfig");
} public final static JdbcResourcepEntity getInstance() {
if (null == jdbc) {
jdbc = new JdbcXXXy();
}
return jdbc;
} public final synchronized Connection getConnect() throws SQLException {
Connection conn = null;
try {
conn = source.getConnection();
} catch (SQLException e) {
throw new RuntimeException("xxx1", e);
}
return conn;
}
呀 非得全部把代码贴出来呀....