关于C3P0连接池的使用――适合多数据源

本人不善表达,开门见山,直接用代码说话咯~

首先使用properties配置文件来写数据库信息

jdbc.properties:

#数据库1
test1.jdbcdriver = oracle.jdbc.driver.OracleDriver
test1.url = jdbc:oracle:thin:@127.0.0.1:1521:ora8
test1.user = ora1
test1.password = ora1
#数据库2
test2.jdbcdriver = oracle.jdbc.driver.OracleDriver
test2.url = jdbc:oracle:thin:@127.0.0.1:1521:ora8
test2.user = ora2
test2.password = ora2

然后读取配置文件的类

C.java:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class C {
    private static Properties prop = null;
    static {
        prop = new Properties();
        try {
            prop.load(C.class.getClassLoader()
                    .getResourceAsStream("jdbc.properties"));
                                                                                                                                       
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
                                                                                                                               
    public static String CONFIG_FILE_LOCATION = System.getProperty("user.dir") + "/jdbc.properties";
    //数据库1
    public static final String TEST1_DRIVER = prop.getProperty("test1.jdbcdriver");
    public static final String TEST1_URL = prop.getProperty("test1.url");
    public static final String TEST1_USER = prop.getProperty("test1.user");
    public static final String TEST1_PASSWORD = prop.getProperty("test1.password");
    //数据库2
    public static final String TEST2_DRIVER = prop.getProperty("test2.jdbcdriver");
    public static final String TEST2_URL = prop.getProperty("test2.url");
    public static final String TEST2_USER = prop.getProperty("test2.user");
    public static final String TEST2_PASSWORD = prop.getProperty("test2.password");
}

最后连接池文件

DataSourceFactory.java

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import common.Logger;
public class DataSourceFactory {
                                                                     
    private static final Logger log = Logger.getLogger(DataSourceFactory.class);
                                                                     
    public enum DataSourceName {
        test1_ds(C.TEST1_DRIVER, C.TEST1_URL, C.TEST1_USER, C.TEST1_PASSWORD),
        test2_ds(C.TEST2_DRIVER, C.TEST2_URL, C.TEST2_USER, C.TEST2_PASSWORD);
        private String _driver, _url, _user, _password;
        private DataSourceName(String driver, String url, String user, String password){
            this._driver = driver; this._url = url; this._user = user; this._password = password;
        }
        public String getDriver() {
            return _driver;
        }
        public String getUrl() {
            return _url;
        }
        public String getUser() {
            return _user;
        }
        public String getPassword() {
            return _password;
        }
    }
    private static HashMap<DataSourceName, ComboPooledDataSource> dss = new HashMap<DataSourceName, ComboPooledDataSource>();
                                                                     
    private synchronized static ComboPooledDataSource getDataSource(final DataSourceName dsName){
        ComboPooledDataSource ds = null;
        if((ds = dss.get(dsName)) == null){
            ds = new ComboPooledDataSource();
            try{
                ds.setDriverClass(dsName.getDriver());
                ds.setUser(dsName.getUser());
                ds.setPassword(dsName.getPassword());
                ds.setJdbcUrl(dsName.getUrl());
                ds.setInitialPoolSize(5);
                ds.setMinPoolSize(1);
                ds.setMaxPoolSize(15);
                ds.setMaxStatements(50);
                ds.setMaxIdleTime(60);
                dss.put(dsName, ds);
            }catch(Exception e){
                log.error(e, e);
                throw new RuntimeException("创建连接池失败:"+dsName.getUrl() );
            }
            dss.put(dsName, ds);
        }
        return ds;
    }
    public static Connection getConnection(DataSourceName dsName){
        ComboPooledDataSource ds = getDataSource(dsName);
        Connection conn = null;
        try{
            conn = ds.getConnection();
        }catch(Exception e){
            log.error(e, e);
            throw new RuntimeException("获取连接失败:"+dsName.getUrl() );
        }
        return conn;
    }   
    public static void close(Connection conn){
        if (conn != null) {
            try {
                if (!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                log.error(e);
            }
            conn = null;
        }
    } 
}

以上三个文件都建好以后,如果要使用连接池使用以下代码即可:

Connection conn = DataSourceFactory.getConnection(DataSourceFactory.DataSourceName.test1_ds)


你可能感兴趣的:(java,oracle,jdbc,c3p0)