在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
数据库连接池 获取一个连接,系统要在背后做很多消耗资源的事情,大多时候,创建连接的时间比执行sql语句的时间还要长。 数据库连接池负责分配,管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个。 c3p0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。c3p0一般是与Hibernate,Spring等框架一块使用的,当然也可以单独使用。 第一种方式:通过xml文件来配置 <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass" >com.mysql.jdbc.Driver</property> <property name="jdbcUrl" >jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK</property> <property name="user" >root</property> <property name="password" ></property> <property name="maxPoolSize" >15</property> <property name="minPoolSize" >5</property> <property name="initialPoolSize" >5</property> </default-config> </c3p0-config> 第二种方式:通过properties文件来配置 c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydb
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.user=root
c3p0.password=
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.maxPoolSize=20
c3p0.maxStatements=100
c3p0.minPoolSize=5
调用main函数来读取配置 public static void main(String[] args) throws Exception { ComboPooledDataSource cb = new ComboPooledDataSource(); Connection conn=cb.getConnection(); System.out.println(conn.isClosed()); conn.close(); } 返回结果为false 通过下面的实验,来验证一下连接池的作用: package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.util.Calendar; import com.mchange.v2.c3p0.ComboPooledDataSource; public class Test { public static void main1111(String[] args) throws Exception { long start = Calendar.getInstance().getTimeInMillis(); for(int i=0; i<100;i++){ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root",""); System.out.println(conn.isClosed()); conn.close(); } long end = Calendar.getInstance().getTimeInMillis(); System.out.println(end-start); } public static void main(String[] args) throws Exception { long start = Calendar.getInstance().getTimeInMillis(); ComboPooledDataSource cb = new ComboPooledDataSource(); for(int i=0; i<100;i++){ Connection conn=cb.getConnection(); System.out.println(conn.isClosed()); conn.close(); } long end = Calendar.getInstance().getTimeInMillis(); System.out.println(end-start); } } main1111的函数是采用最基本的方式连接数据库,main函数是用连接池连接数据库,同样循环一百次第一个结果为8000毫秒,第二个约为1000毫秒。 既然可以通过xml文件和properties文件来配置连接池,那么我们也可以通过别的方式进行配置: (1)直接在main函数中定义 public static void main11(String[] args) throws Exception { ComboPooledDataSource cd = new ComboPooledDataSource(); cd.setDriverClass("com.mysql.jdbc.Driver"); cd.setJdbcUrl("jdbc:mysql://localhost:3306/mydb"); cd.setUser("root"); cd.setPassword(""); cd.setMinPoolSize(5); cd.setMaxPoolSize(10); Connection conn = cd.getConnection(); System.out.println(conn.isClosed()); conn.close(); } (2)通过读取properties配置采用${}方式在xml中读取 driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK user=root password= minPoolSize=5 maxPoolSize=15 initialPoolSize=5 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:property-placeholder location="classpath:db.properties"/> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="minPoolSize" value="${minPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property> </bean> </beans> public static void main3333(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource d = (DataSource)context.getBean("dataSource"); Connection conn = d.getConnection(); System.out.println(conn.isClosed()); conn.close(); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论