廊坊网站seo排名,今天的新闻联播内容摘抄,免费打广告网站,特斯拉公司的发展历程背景 一般我们在项目中操作数据库时#xff0c;都是每次需要操作数据库就建立一个连接#xff0c;操作完成后释放连接。因为jdbc没有保持连接的能力#xff0c;一旦超过一定时间没有使用#xff08;大约几百毫秒#xff09;#xff0c;连接就会被自动释放掉。而每次新建连…背景 一般我们在项目中操作数据库时都是每次需要操作数据库就建立一个连接操作完成后释放连接。因为jdbc没有保持连接的能力一旦超过一定时间没有使用大约几百毫秒连接就会被自动释放掉。而每次新建连接都需要140毫秒左右的时间所以耗费时间比较多。若使用C3P0连接池来池化连接随时取用则平均每次取用只需要10-20毫秒。这在高并发随机访问数据库的时候对效率的提升有很大帮助。 C3P0连接池会根据你的配置来初始化N个数据库连接空闲T时间后连接过期又会自动新建K个连接使得连接池总有空闲的数据库连接等待被取用。我们只需通过dataSourse.getConnection()即可从线程池中取用一个已经连接好的空闲连接执行数据库操作。然后“断开”放回这个连接把这个连接的使用权放回连接池。真正的数据库连接的创建与释放是由C3P0在后台自动完成的我们花的只是取用与释放占用权的时间。全程耗时10毫秒比原来提高了几十倍 下载 
c3p0下载地址https://sourceforge.net/projects/c3p0/files/latest/download?sourcefiles   下载完成后将解压出来有3个jar包 将这三个jar包导入项目中 在src目录下新建一个名叫  c3p0-config.xml  的文件注意必须是这个文件名。  配置  
c3p0-config.xml的示例代码如下图所示 
?xml version1.0 encodingUTF-8?
c3p0-config  !-- 默认配置 --  default-config  property nameinitialPoolSize10/property  property namemaxIdleTime30/property  property namemaxPoolSize100/property  property nameminPoolSize10/property  property namemaxStatements200/property  /default-config  !-- 配置oracle连接池 --  named-config nameoracle  property namedriverClassoracle.jdbc.driver.OracleDriver/property  property namejdbcUrljdbc:oracle:thin:192.168.1.138:1521:ecology/property  property nameusertest1028/property  property namepasswordtest1028/property  property nameinitialPoolSize10/property  property namemaxIdleTime30/property  property namemaxPoolSize100/property  property nameminPoolSize10/property  property namemaxStatements200/property  /named-config  !--配置连接池mysql--named-config namemysql  property namedriverClasscom.mysql.jdbc.Driver/property  property namejdbcUrljdbc:mysql://192.168.1.123:3306/CoupleSpace/property  property nameuserroot/property  property namepasswordroot/property  property nameinitialPoolSize10/property  property namemaxIdleTime30/property  property namemaxPoolSize100/property  property nameminPoolSize10/property  property namemaxStatements200/property  /named-config !-- 配置其他连接池--
/c3p0-config 更细致的配置说明如下图所示 
!--acquireIncrement链接用完了自动增量3个。 --property nameacquireIncrement3/property!--acquireRetryAttempts链接失败后重新试30次。--property nameacquireRetryAttempts30/property!--acquireRetryDelay两次连接中间隔1000毫秒。 --property nameacquireRetryDelay1000/property!--autoCommitOnClose连接关闭时默认将所有未提交的操作回滚。 --property nameautoCommitOnClosefalse/property!--automaticTestTablec3p0测试表没什么用。--property nameautomaticTestTableTest/property!--breakAfterAcquireFailure出错时不把正在提交的数据抛弃。--property namebreakAfterAcquireFailurefalse/property!--checkoutTimeout100毫秒后如果sql数据没有执行完将会报错如果设置成0那么将会无限的等待。 -- property namecheckoutTimeout100/property!--connectionTesterClassName通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径。Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--property nameconnectionTesterClassName/property!--factoryClassLocation指定c3p0 libraries的路径如果通常都是这样在本地即可获得那么无需设置默认null即可。--property namefactoryClassLocationnull/property!--forceIgnoreUnresolvedTransactions作者强烈建议不使用的一个属性。-- property nameforceIgnoreUnresolvedTransactionsfalse/property!--idleConnectionTestPeriod每60秒检查所有连接池中的空闲连接。-- property nameidleConnectionTestPeriod60/property!--initialPoolSize初始化时获取三个连接取值应在minPoolSize与maxPoolSize之间。 -- property nameinitialPoolSize3/property!--maxIdleTime最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。--property namemaxIdleTime60/property!--maxPoolSize连接池中保留的最大连接数。 --property namemaxPoolSize15/property!--maxStatements最大链接数。--property namemaxStatements100/property!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0  --property namemaxStatementsPerConnection/property!--numHelperThreads异步操作提升性能通过多线程实现多个操作同时被执行。Default: 3-- property namenumHelperThreads3/property!--overrideDefaultUser当用户调用getConnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0的数据源时。Default: null-- property nameoverrideDefaultUserroot/property!--overrideDefaultPassword与overrideDefaultUser参数对应使用的一个参数。Default: null--property nameoverrideDefaultPasswordpassword/property!--password密码。Default: null-- property namepassword/property!--preferredTestQuery定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意 测试的表必须在初始数据源的时候就存在。Default: null--property namepreferredTestQueryselect id from test where id1/property!--propertyCycle用户修改系统配置参数执行前最多等待300秒。Default: 300 -- property namepropertyCycle300/property!--testConnectionOnCheckout因性能消耗大请只在需要的时候使用它。Default: false --property nametestConnectionOnCheckoutfalse/property!--testConnectionOnCheckin如果设为true那么在取得连接的同时将校验连接的有效性。Default: false --property nametestConnectionOnCheckintrue/property!--user用户名。Default: null--property nameuserroot/property!--usesTraditionalReflectiveProxies动态反射代理。Default: false--property nameusesTraditionalReflectiveProxiesfalse/property 使用方式 
新建一个c3p0的工具类如下图所示 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3P0Util {	static ComboPooledDataSource dataSource  new ComboPooledDataSource(oracle);/*** 获取数据库连接* return* throws SQLException */public static Connection getConnection() throws SQLException{return dataSource.getConnection();}/*** 关闭数据库连接* throws SQLException */public static void closeConnection(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) throws SQLException{if (connection!null  !connection.isClosed()) {connection.close();}if (preparedStatement!null  !preparedStatement.isClosed()) {preparedStatement.close();}if (resultSet ! null  !resultSet.isClosed()) {resultSet.close();}}
}在jdbc中使用c3p0工具类获得连接并查询数据 public ArrayListDept queryDepts() throws SQLException{ArrayListDept depts  new ArrayListDept();try(Connection connection  C3P0Util.getConnection();PreparedStatement stmtconnection.prepareStatement(select * from ecology09171.bm);ResultSet rsstmt.executeQuery();){while(rs.next()){Dept dept  new Dept();long id  rs.getLong(ID);String departmentName  rs.getString(DEPARTMENTNAME);long supDepID  rs.getLong(SUPDEPID);long subCompanyID1  rs.getLong(SUBCOMPANYID1);String departmentMark  rs.getString(DEPARTMENTMARK);String canceled  rs.getString(CANCELED);dept.setID(id);dept.setSUPDEPID(supDepID);dept.setSUBCOMPANYID1(subCompanyID1);dept.setDEPARTMENTNAME(departmentName);dept.setDEPARTMENTMARK(departmentMark);dept.setCANCELED(canceled);depts.add(dept);}return depts;}}