日期:2014-05-16  浏览次数:20887 次

java桌面程序:apache池化多个数据源的示例


package datatransfer;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ApacheCPDS {

    static SharedPoolDataSource omcsp1_pm;
    static SharedPoolDataSource omcsp9_pm;
    static SharedPoolDataSource omcsp11_pm;
    static SharedPoolDataSource omcsp1_cm;
    static SharedPoolDataSource omcsp9_cm;
    static SharedPoolDataSource omcsp11_cm;
    private static final int MaxActive = 3;
    private static final int MaxWait = 3;

    public static void init_DataSource_mot_pm() {
        omcsp1_pm = get_spds("omcsp1", "xxxx", "xxxx");
        omcsp9_pm = get_spds("omcsp9", "xxxx", "xxxx");
        omcsp11_pm = get_spds("omcsp11", "xxxx", "xxxx");
        Main.StringMapCPDS.put("OMCSP1", omcsp1_pm);
        Main.StringMapCPDS.put("OMCSP9", omcsp9_pm);
        Main.StringMapCPDS.put("OMCSP11", omcsp11_pm);
        System.out.println("Main.StringMapCPDS.keySet() " + Main.StringMapCPDS.keySet());
    }

    public static void init_DataSource_mot_cm() {
        omcsp1_cm = get_spds("omcsp1_mib", "xxxx", "xxxx");
        omcsp9_cm = get_spds("omcsp9_mib", "xxxx", "xxxx");
        omcsp11_cm = get_spds("omcsp11_mib", "xxxx", "xxxx");
        Main.StringMapCPDS.put("OMCSP1_MIB", omcsp1_cm);
        Main.StringMapCPDS.put("OMCSP9_MIB", omcsp9_cm);
        Main.StringMapCPDS.put("OMCSP11_MIB", omcsp11_cm);
        System.out.println("Main.StringMapCPDS.keySet() " + Main.StringMapCPDS.keySet());
    }

    public static SharedPoolDataSource get_spds(String omc, String user, String pwd) {
        try {
            DriverAdapterCPDS DAcpds = new DriverAdapterCPDS();
            DAcpds.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
            DAcpds.setUrl("jdbc:odbc:" + omc);
            DAcpds.setUser(user);
            DAcpds.setPassword(pwd);
            SharedPoolDataSource spds = new SharedPoolDataSource();
            spds.setConnectionPoolDataSource(DAcpds);
            spds.setMaxActive(MaxActive);
            spds.setMaxWait(MaxWait);
            return spds;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ApacheCPDS.class.getName()).log(Level.SEVERE, null, e