美文网首页Mysql
JDBC 3.数据库连接池

JDBC 3.数据库连接池

作者: 第二套广播体操 | 来源:发表于2019-03-28 17:30 被阅读0次

    数据库连接池:容器 存放数据库连接的容器

      当系统初始化好后 容器被创建 容器中会申请一些连接对象 当用户来访问数据库时
    从容器中获取连接对象访问后会归还对象给容器
       好处
        1 节约资源
        2 高效

      实现DataSource
    方法 获取连接 getconnection 通过donnection.close() 归还连接

    C3P0 数据库连接池技术
    Druid:数据库连接池技术 由阿里巴巴提供

    步骤:
    1.导入Jar包 c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar
    导入数据库驱动Jar包 mysql-connector-java-5.1.37-bin.jar

    2.定义配置文件
    c3p0.properties 或者c3p0-config.xml
    位置:直接将文件放在 src目录下即可
    3.创建数据库连接池对象 ComPooledDataSource
    4.获取链接的方法getConnection

    读取默认配置获取连接

    /**测试创建数据库连接数量上限 */
    public class C3p0Demo1 {
        public static void main(String[] args) throws SQLException {
    //        获取连接对象 括号中无参数连接默认配置 有参数 连接参数名配置
            DataSource dataSource =new ComboPooledDataSource();
    //        获取连接对象getConnection
            for (int i = 1; i <12; i++) {
                Connection connection = dataSource.getConnection();
                System.out.println(i+"    "+connection);
               if (i==5)
                   connection.close();
            }
        }
    }
    

    上限10 循环11次 其中 5和6次获取的连接地址相同 归还后使用的同一个地址

    5    com.mchange.v2.c3p0.impl.NewProxyConnection@2df9b86 [wrapping: com.mysql.jdbc.JDBC4Connection@37654521]
    6    com.mchange.v2.c3p0.impl.NewProxyConnection@101952da [wrapping: com.mysql.jdbc.JDBC4Connection@37654521]
    

    配置

    <c3p0-config>
      <!-- 使用默认的配置读取连接池对象 -->
      <default-config>
        <!--  连接参数 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
        <property name="user">root</property>
        <property name="password">root</property>
        
        <!-- 连接池参数 -->
        <!--初始化连接数-->
        <property name="initialPoolSize">5</property>
        <!--连接数上限-->
        <property name="maxPoolSize">10</property>
        <!--无连接等待时长-->
        <property name="checkoutTimeout">3000</property>
      </default-config>
    </c3p0-config>
    

    带参数配置

    <named-config name="otherc3p0"> 
        <!--  连接参数 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/day25</property>
        <property name="user">root</property>
        <property name="password">root</property>
        
        <!-- 连接池参数 -->
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">8</property>
        <property name="checkoutTimeout">1000</property>
    

    Druid数据库连接池
    导入jar包 druid-1.0.9.jar
    配置 druid.properties 可以放在任意位置 任意名字 手动导入

    配置文件

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/db1
    username=root
    password=root
    initialSize=5
    maxActive=10
    maxWait=3000
    

    通过工厂来获取DruidDataSourceFactory.createDataSource(properties)
    获取链接 getConnection

    public class DruidDemo1 {
        public static void main(String[] args) {
    //        导入配置文件
            ClassLoader loader = DruidDemo1.class.getClassLoader();
            InputStream inputStream = loader.getResourceAsStream("druid.properties");
    
    
            Properties properties = new Properties();
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (inputStream!=null)
                {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    
            try {
    //            获取连接池对象
                DataSource source = DruidDataSourceFactory.createDataSource(properties);
    //           获取连接
                Connection connection = source.getConnection();
                System.out.println(connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Druid工具类创建

    public class DruidUnits {
    
        private static DataSource dataSource = null;
    
        static {
    
            try {
                //    获取连接
                ClassLoader loader = DruidUnits.class.getClassLoader();
                InputStream inputStream = loader.getResourceAsStream("druid.properties");
    
    
                Properties properties = new Properties();
                properties.load(inputStream);
                dataSource = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
    
            }
        }
    //   获取数据库连接池
           public static DataSource getDataSource(){
            return dataSource;
        }
    
    
        public static Connection getConnection() throws SQLException {
    
            return dataSource.getConnection();
        }
        public static void close(Statement statement,Connection connection){
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void close(ResultSet resultSet, Statement statement, Connection connection){
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            close(statement, connection);
    
        }
    
    
    }
    

    测试类 通过线程池 查询

    public class DruidUnitTest {
        public static void main(String[] args) {
            ResultSet resultSet = null;
            PreparedStatement statement = null;
            Connection connection = null;
            try {
                connection = DruidUnits.getDataSource();
    
                String sql = "select name from galary where id = ?";
                statement = connection.prepareStatement(sql);
                statement.setInt(1, 1);
                resultSet = statement.executeQuery();
    
    //            输出查询出来的数据
                while (resultSet.next()) {
                    String name = resultSet.getString("name");
                    System.out.println(name);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DruidUnits.close(resultSet,statement,connection);
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:JDBC 3.数据库连接池

        本文链接:https://www.haomeiwen.com/subject/opxcbqtx.html