建立连接池的几种方式

第一种C3P0

public class C3p0Connection {
    private static String driverClassName=null;
    private static String url=null;
    private static String username=null;
    private static String password=null;
    private static int maxActive=0;
    private static int maxIdle=0;
    private static int maxWait=0;
    
    private static Connection conn=null;
    
    static{
        Properties properties=new Properties();
        InputStream inputStream = C3p0Connection.class.getResourceAsStream("c3p0.properties");
        try {
            properties.load(inputStream);
            driverClassName = properties.getProperty("driverClassName");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            maxActive =Integer.parseInt(properties.getProperty("maxActive")) ;
            maxIdle = Integer.parseInt(properties.getProperty("maxIdle"));
            maxWait = Integer.parseInt(properties.getProperty("maxWait"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    
    public static Connection getConnection(){
        ComboPooledDataSource cpds=new ComboPooledDataSource();
        try {
            cpds.setDriverClass(driverClassName);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        cpds.setJdbcUrl(url);
        cpds.setUser(username);
        cpds.setPassword(password);
        
        
        cpds.setInitialPoolSize(maxActive);
        cpds.setMaxPoolSize(maxIdle);
        cpds.setMaxIdleTime(maxWait);
        
        try {
            conn = cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
        
    }
}
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:XE
username=project
password=1234
maxActive=10
maxIdle=20
maxWait=1000

第二种JNDI

public class JNDIConnection {
    private static String jndi = "";
    private static Connection conn = null;
    static {
        jndi = "jdbc/oaec";
    }

    public static Connection getConnection() {
        try {
            Context context = (Context) new InitialContext().lookup("java:comp/env");
            DataSource ds = (DataSource) context.lookup("jndi");
            conn = ds.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void closeConn() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

tomcat项目下面的context.xml配置:

<!-- name 自定义的,需要Java代码来获取
            typejava代码里面获取数据源类型
            maxActive 最大连接数
            maxIdle最大空闲个数
            maxwait 最大等待时间 10秒后 报错 连接没有获取到
     -->
<Resource
        name="jdbc/oaec"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@localhost:1521:XE"
        username="project"
        password="1234"
        maxActive="10"
        maxIdle="20"
        maxwait="10000"
          >
</Resource>

第三种DBCP:

public class DBCPConnection {
    private static Properties properties=null;
    private static Connection conn=null;
    
    static{
        properties=new Properties();
        InputStream inputStream=DBCPConnection.class.getResourceAsStream("dbcp.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static Connection getConnetcion(){
        try {
            DataSource ds = BasicDataSourceFactory.createDataSource(properties);
            conn = ds.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:XE
username=project
password=1234
maxActive=10
maxIdle=20
maxWait=1000
removeAbandoned=true
removeAbandonedTimeout=180
原文地址:https://www.cnblogs.com/Damon-Luo/p/5621878.html