jdbc 数据源(DruidDataSourceFactory)连接池 —— druid

    
        
            mysql
            mysql-connector-java
            8.0.28
            compile
        

        
            com.alibaba
            druid
            1.2.16
        
    

1、德鲁伊数据源连接池技术

package com.csdn.jdbc;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import java.sql.SQLException;
//演示数据源连接池    druid  德鲁伊数据源连接池技术
public class DataSourceConnectPool {

    public static void main(String[] args) throws SQLException {

        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///fruitdb");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        dataSource.setInitialSize(5);       //初始化数量
        dataSource.setMaxActive(10);        //最大激活数
        dataSource.setMaxWait(5000);        //最大等待时间

        for (int count = 1; count <= 11; count++) {
            DruidPooledConnection conn = dataSource.getConnection();
            System.out.println(conn);
        }
    }
}
D:\Java\jdk-17\bin\java.exe com.csdn.jdbc.DataSourceConnectPool
10月 27, 2023 12:51:59 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
com.mysql.cj.jdbc.ConnectionImpl@408d971b
com.mysql.cj.jdbc.ConnectionImpl@6c6cb480
com.mysql.cj.jdbc.ConnectionImpl@3c46e67a
com.mysql.cj.jdbc.ConnectionImpl@c730b35
com.mysql.cj.jdbc.ConnectionImpl@206a70ef
com.mysql.cj.jdbc.ConnectionImpl@292b08d6
com.mysql.cj.jdbc.ConnectionImpl@22555ebf
com.mysql.cj.jdbc.ConnectionImpl@36ebc363
com.mysql.cj.jdbc.ConnectionImpl@45752059
com.mysql.cj.jdbc.ConnectionImpl@34e9fd99
Exception in thread "main" com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 5003, active 10, maxActive 10, creating 0
	at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1840)
	at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1470)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1450)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1440)
	at com.csdn.jdbc.DataSourceConnectPool.main(DataSourceConnectPool.java:22)

Process finished with exit code 1
  • 获取的10个连接对象都是不同的,最大激活数是10个,如果想获得第11个,等待5秒,就会获取连接对象超时。
package com.csdn.jdbc;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import java.sql.SQLException;
//演示数据源连接池    druid  德鲁伊数据源连接池技术
public class DataSourceConnectPool {

    public static void main(String[] args) throws SQLException {

        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///fruitdb");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        dataSource.setInitialSize(5);       //初始化数量
        dataSource.setMaxActive(10);        //最大激活数
        dataSource.setMaxWait(5000);        //最大等待时间

        for (int count = 1; count <= 11; count++) {
            DruidPooledConnection conn = dataSource.getConnection();
            System.out.println(conn);
            
            if (count % 4 == 0) {
                conn.close();
            }
            
        }
    }
}
D:\Java\jdk-17\bin\java.exe
10月 27, 2023 12:57:41 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
com.mysql.cj.jdbc.ConnectionImpl@6c6cb480
com.mysql.cj.jdbc.ConnectionImpl@3c46e67a
com.mysql.cj.jdbc.ConnectionImpl@c730b35
com.mysql.cj.jdbc.ConnectionImpl@206a70ef
com.mysql.cj.jdbc.ConnectionImpl@206a70ef
com.mysql.cj.jdbc.ConnectionImpl@292b08d6
com.mysql.cj.jdbc.ConnectionImpl@22555ebf
com.mysql.cj.jdbc.ConnectionImpl@36ebc363
com.mysql.cj.jdbc.ConnectionImpl@36ebc363
com.mysql.cj.jdbc.ConnectionImpl@45752059
com.mysql.cj.jdbc.ConnectionImpl@34e9fd99

2、数据源连接池,加载 jdbc.properties 

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///fruitdb
jdbc.user=root
jdbc.pwd=123456
jdbc.initial_size=5
jdbc.max_active=10
jdbc.max_wait=5000
package com.csdn.jdbc;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;
//演示数据源连接池   加载 jdbc.properties
public class DataSourceConnectPoolProperties {

    public static void main(String[] args) throws SQLException, IOException {

        InputStream inputStream = DataSourceConnectPoolProperties.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getProperty("jdbc.driver"));
        dataSource.setUrl(properties.getProperty("jdbc.url"));
        dataSource.setUsername(properties.getProperty("jdbc.user"));
        dataSource.setPassword(properties.getProperty("jdbc.pwd"));

        dataSource.setInitialSize(Integer.parseInt(properties.getProperty("jdbc.initial_size")));       //初始化数量
        dataSource.setMaxActive(Integer.parseInt(properties.getProperty("jdbc.max_active")));        //最大激活数
        dataSource.setMaxWait(Integer.parseInt(properties.getProperty("jdbc.max_wait")));        //最大等待时间

        for (int count = 1; count <= 11; count++) {
            DruidPooledConnection conn = dataSource.getConnection();
            System.out.println(conn);
            if (count % 4 == 0) {
                conn.close();
            }
        }
    }
}

3、数据源连接池,加载升级版 jdbc.properties,key是固定好的,是和连接池工厂中的属性一致的

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///fruitdb
username=root
password=123456
initialSize=5
maxActive=10
maxWait=1000
package com.csdn.jdbc;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
//演示数据源连接池   加载 jdbc.properties , key是固定好的,是和连接池工厂中的属性一致的
public class DataSourceConnectPoolPropertiesFactory {

    public static void main(String[] args) throws Exception {

        InputStream inputStream = DataSourceConnectPoolPropertiesFactory.class.getClassLoader().getResourceAsStream("jdbc2.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        for (int count = 1; count <= 11; count++) {
            Connection conn = dataSource.getConnection();
            System.out.println(conn);
            if (count % 4 == 0) {
                conn.close();
            }
        }
    }
}

你可能感兴趣的:(#,JDBC,数据库,java,jdbc,反射,druid,数据源连接池)