用了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);
}
}
}

解决方案 »

  1.   

    <default-config>
    <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>
      

  2.   

    private JdbcResourcepEntity() {
    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;
    }
      

  3.   

    创建两个 ComboPooledDataSource dataSource 变量
      

  4.   

    创建两个 ComboPooledDataSource dataSource 变量
      

  5.   

    我就是创建了两个dataSource变量啊
      

  6.   

    创建2个 创建两个 ComboPooledDataSource dataSource 变量
    呀  非得全部把代码贴出来呀....