数据库连接池的使用

3 数据库连接池

JDBC的数据库连接池使用javax.sql.DataSource来表示,DataSource只是一个接口

3.1 c3p0

  • 获取连接方式一:

    //使用C3P0数据库连接池的方式,获取数据库的连接:不推荐
    public static Connection getConnection1() throws Exception{
    	ComboPooledDataSource cpds = new ComboPooledDataSource();
    	cpds.setDriverClass("com.mysql.jdbc.Driver"); 
    	cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
    	cpds.setUser("root");
    	cpds.setPassword("abc123");
    		
    //	cpds.setMaxPoolSize(100);
    	
    	Connection conn = cpds.getConnection();
    	return conn;
    }
    
  • 方式二:

    //使用C3P0数据库连接池的配置文件方式,获取数据库的连接:推荐
    private static DataSource cpds = new ComboPooledDataSource("helloc3p0");
    public static Connection getConnection2() throws SQLException{
    	Connection conn = cpds.getConnection();
    	return conn;
    }
    

    配置文件:c3p0-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    	<named-config name="helloc3p0">
    		<!-- 获取连接的4个基本信息 -->
    		<property name="user">root</property>
    		<property name="password">abc123</property>
    		<property name="jdbcUrl">jdbc:mysql:///test</property>
    		<property name="driverClass">com.mysql.jdbc.Driver</property>
    		
    		<!-- 涉及到数据库连接池的管理的相关属性的设置 -->
    		<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
    		<property name="acquireIncrement">5</property>
    		<!-- 初始化数据库连接池时连接的数量 -->
    		<property name="initialPoolSize">5</property>
    		<!-- 数据库连接池中的最小的数据库连接数 -->
    		<property name="minPoolSize">5</property>
    		<!-- 数据库连接池中的最大的数据库连接数 -->
    		<property name="maxPoolSize">10</property>
    		<!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
    		<property name="maxStatements">20</property>
    		<!-- 每个连接同时可以使用的 Statement 对象的个数 -->
    		<property name="maxStatementsPerConnection">5</property>
    
    	</named-config>
    </c3p0-config>
    

3.2 DBCP

需要导入两个jar包

  1. commons-dbcp-1.4.jar
  2. commons-pool-1.5.5.jar

方式一

public static Connection getConnection3() throws Exception {
	BasicDataSource source = new BasicDataSource();
		
	source.setDriverClassName("com.mysql.jdbc.Driver");
	source.setUrl("jdbc:mysql:///test");
	source.setUsername("root");
	source.setPassword("abc123");
		
	//
	source.setInitialSize(10);
		
	Connection conn = source.getConnection();
	return conn;
}

方式二:

//使用dbcp数据库连接池的配置文件方式,获取数据库的连接:推荐
private static DataSource source = null;
static{
	try {
		Properties pros = new Properties();
		
		InputStream is = DBCPTest.class.getClassLoader().getResourceAsStream("dbcp.properties");
			
		pros.load(is);
		//根据提供的BasicDataSourceFactory创建对应的DataSource对象
		source = BasicDataSourceFactory.createDataSource(pros);
	} catch (Exception e) {
		e.printStackTrace();
	}
		
}
public static Connection getConnection4() throws Exception {
		
	Connection conn = source.getConnection();
	
	return conn;
}

资源文件:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&useServerPrepStmts=false
username=root
password=abc123

3.3 Druid(德鲁伊)

Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、Proxool等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,可以说是目前最好的连接池之一。

添加驱动:druid-1.1.10.jar

方式一:

@Test
public void test1() throws SQLException {
    // 德鲁伊数据库连接池
    // 方式一: 设置属性的形式
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis?useSSL=true");
    dataSource.setUsername("root");
    dataSource.setPassword("password");

    DruidPooledConnection connection = dataSource.getConnection();

    connection.close();
    dataSource.close();
}

方式二:配置文件方式获取连接

@Test
    public void test2() throws Exception {
        // 方式二:配置文件的形式
        Properties pro = new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dataSource.properties");
        pro.load(is);
        // 利用工厂提供的方法创建
        DataSource dataSource = DruidDataSourceFactory.createDataSource(pro);
        Connection connection = dataSource.getConnection();
        
        connection.close();
        dataSource.getClass();
    }

配置文件:

url=jdbc:mysql://localhost:3306/mybatis?useSSL=true
username=root
password=password
driverClassName=com.mysql.jdbc.Driver

initialSize=10
maxActive=20
maxWait=1000
filters=wall

详细参数:

posted @ 2021-12-31 10:41  恸的池塘  阅读(131)  评论(0编辑  收藏  举报