C3P0和DBCP连接池

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_40662281/article/details/79969033

C3P0连接池

  • 导包
    这里写图片描述
  • 要配置一个c3p0-config.xml
  • C3P0Demo:
package bull03.C3P0;



import java.sql.Connection;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Demo_01 {
    @Test
    public void demo() throws Exception {
        //1.获得连接池(数据源)
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //2.设置基本项
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUser("root");
        dataSource.setPassword("root");

        //3.设置其他项
        //初始化连接池中的个数
        dataSource.setInitialPoolSize(5);
        dataSource.setMaxPoolSize(10);
        dataSource.setMinPoolSize(2);
        //最大空闲数
        dataSource.setMaxIdleTime(60);
        //每次增长个数
        dataSource.setAcquireIncrement(2);

        //4.获得连接
        Connection conn = dataSource.getConnection();
        System.out.println(conn);
    }
}
  • 工具类:
package bull03.C3P0;

import java.sql.Connection;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/*
 * C3P0工具类
 */
public class C3P0Utils {
    //1.采用默认方式
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //2.使用命名配置,c3p0-config.xml里面获取
    private static ComboPooledDataSource dataSource1 = new ComboPooledDataSource("bull");

    //获得连接池
    public static DataSource getDataSource() {
        return dataSource;//dataSource1
    }

    //获得连接
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

DBCP连接池

  • 导包:
    这里写图片描述
  • 要配置一个dbcp-config.properties文件
  • DBCPDemo:
package bull04.DBCP;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;

public class DBCPDemo_01 {
    @Test
    public void method () throws SQLException {
        //1.获得连接池
        BasicDataSource dataSource = new BasicDataSource();
        //2.配置基本项: 驱动、URL、user、password
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        //3.配置其他项
        //设置初始连接个数
        dataSource.setInitialSize(5);
        //最大活动数
        dataSource.setMaxActive(10);

        //4.获得连接
        Connection conn = dataSource.getConnection();
        System.out.println(conn);
    }
}
  • 工具类:
package bull04.DBCP;

import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

/*
 * DBCP工具类
 */
public class DBCPUtils {
    private static DataSource dataSource;

    static{
        try {
            //1.加载配置文件,获得文件流,运用类的加载:DBCPUtils.class.getClassLoader()
            InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("dbcp-config.properties");
            //2.使用Properties处理配置文件
            Properties prop = new Properties();
            prop.load(is);
            //3.使用工具类创建连接池
            dataSource = BasicDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //获得连接池
    public static DataSource getDataSource() {
        return dataSource;
    }


    //获得连接
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
阅读更多

没有更多推荐了,返回首页