常用的数据库连接池技术

来源:互联网 发布:stm32f103 串行口编程 编辑:程序博客网 时间:2024/06/05 08:16

常用的数据库连接池技术有DBCP和C3P0
 1、DBCP技术,这个连接池的技术的核心类是BasicDataSource,当需要使用这个连接池技术的时候就需要引入相应的jar包,Commons-dbcp.jar和Commons-pool.jar
可以使用硬编码的方式来实现连接池,也可以使用配置文件的方式来实现连接池技术,其中这里的配置文件就是一个Properties文件。示例代码如下:

BasicDataSource dataSouce = new BasicDataSource();  // 连接池参数配置:初始化连接数、最大连接数 / 连接字符串、驱动、用户、密码  dataSouce.setUrl("jdbc:mysql://localhost:3306/test");   //数据库连接字符串  dataSouce.setDriverClassName("com.mysql.jdbc.Driver");  //数据库驱动  dataSouce.setUsername("root");       //数据库连接用户  dataSouce.setPassword("root");        //数据库连接密码  dataSouce.setInitialSize(3);  // 初始化连接  dataSouce.setMaxActive(6);   // 最大连接  dataSouce.setMaxIdle(3000);   // 最大空闲时间  // 获取连接  Connection con = dataSouce.getConnection();  con.prepareStatement("delete from admin where id=3").executeUpdate();  // 关闭  con.close();


 配置文件的方式:
 

 Properties prop = new Properties();  // 获取文件流  InputStream inStream = App_DBCP.class.getResourceAsStream("db.properties");//这里的App_DBCP表示的就是当前类的class文件  // 加载属性配置文件  prop.load(inStream);  // 根据prop配置,直接创建数据源对象  DataSource dataSouce = BasicDataSourceFactory.createDataSource(prop);  // 获取连接  Connection con = dataSouce.getConnection();  con.prepareStatement("delete from admin where id=4").executeUpdate();  // 关闭  con.close();


配置文件:

db.propertiesurl=jdbc:mysql:///jdbc_demodriverClassName=com.mysql.jdbc.Driverusername=rootpassword=rootinitialSize=3maxActive=6maxIdle=3000


 2、C3P0技术:最常用的连接池技术,Spring框架默认支持C3P0连接池技术。核心类是CombopooledDataSource
  硬编码的方式实现连接池:

   // 创建连接池核心工具类   ComboPooledDataSource dataSource = new ComboPooledDataSource();   // 设置连接参数:url、驱动、用户密码、初始连接数、最大连接数   dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_demo");   dataSource.setDriverClass("com.mysql.jdbc.Driver");   dataSource.setUser("root");   dataSource.setPassword("root");   dataSource.setInitialPoolSize(3);   dataSource.setMaxPoolSize(6);   dataSource.setMaxIdleTime(1000);   // ---> 从连接池对象中,获取连接对象   Connection con = dataSource.getConnection();   // 执行更新   con.prepareStatement("delete from admin where id=7").executeUpdate();   // 关闭   con.close();

 

  配置文件方式,只要创建了核心类的对象,就会默认的加载src目录下的C3P0配置文件
 

  // 创建c3p0连接池核心工具类,自动加载src下c3p0的配置文件【c3p0-config.xml】   ComboPooledDataSource dataSource = new ComboPooledDataSource();// 使用默认的配置   // 获取连接   Connection con = dataSource.getConnection();   // 执行更新   con.prepareStatement("delete from admin where id=5").executeUpdate();   // 关闭   con.close();

  配置文件:


 

<c3p0-config><default-config><property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">3</property><property name="maxPoolSize">6</property><property name="maxIdleTime">1000</property></default-config><named-config name="oracle_config"><property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">3</property><property name="maxPoolSize">6</property><property name="maxIdleTime">1000</property></named-config></c3p0-config>
0 0