• 建立连接池的几种方式


    第一种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
  • 相关阅读:
    基于连通性状态压缩的动态规划问题
    2005年IT行业趋势Top10
    企业应用之性能实时度量系统演变
    云计算参考架构几例
    在CentOS上构建.net自动化编译环境
    Asp.net SignalR 实现服务端消息推送到Web端
    餐饮行业解决方案之客户分析流程
    餐饮行业解决方案之采购战略制定与实施流程
    餐饮行业解决方案之业务设计流程
    零售连锁专卖信息化解决方案简介之三
  • 原文地址:https://www.cnblogs.com/Damon-Luo/p/5621878.html
Copyright © 2020-2023  润新知