行业知识
apache commons-dbcp Apache Commons DBCP 软件实现数据库连接池 commons-dbcp2
DBCP组件
许多Apache项目支持与关系型数据库进行交互。为每个用户创建一个新连接可能很耗时(通常需要多秒钟的时钟时间),以执行可能需要毫秒级时间的数据库事务。对于一个公开托管在互联网上的应用程序,在同时在线用户数量可能非常大的情况下,为每个用户打开一个连接可能是不可行的。因此,开发人员通常希望在所有当前应用程序用户之间共享一组“池化”的打开连接。在任何给定时间实际执行请求的用户数量通常只是活跃用户总数的非常小的百分比,在请求处理期间是唯一需要数据库连接的时间。应用程序本身登录到DBMS,并在内部处理任何用户账户问题。
()已经有几个数据库连接池可用,包括Apache产品内部和其他地方。这个Commons包提供了一个机会,来协调创建和维护一个高效、功能丰富的包,以Apache许可证发布。
commons-dbcp2依赖于commons-pool2中的代码,以提供底层的对象池机制。
()不同版本
DBCP现在有四个不同的版本,支持不同版本的JDBC。
它的工作原理如下:
开发中
DBCP 2.5.0及以上版本在Java 8(JDBC 4.2)及以上版本下编译和运行。
DBCP 2.4.0在Java 7(JDBC 4.1)及以上版本下编译和运行。
运行中
应用程序运行在Java 8及以上版本的情况下,应使用DBCP 2.5.0及以上版本的二进制文件。 应用程序在Java 7下运行时应使用DBCP 2.4.0的二进制文件。 DBCP 2基于Apache Commons Pool,并提供了与DBCP 1.x相比性能增强、JMX支持以及许多其他新功能。升级到2.x的用户应该注意到Java包名称已更改,以及Maven坐标已更改,因为DBCP 2.x与DBCP 1.x不是二进制兼容的。用户还应该注意,一些配置选项(例如maxActive到maxTotal)已更名以与Commons Pool使用的新名称对齐。
入门例子
您可以从我们的下载页面下载源代码和二进制文件。
或者,您可以从中央 Maven 存储库中提取它:
maven 引入
org.apache.commons commons-dbcp2 2.9.0
代码
https://github.com/apache/commons-dbcp/tree/HEAD/doc
BasicDataSourceExample
这个是最基本的例子,不涉及任何池化能力。
import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.sql.DataSource;//// Here are the dbcp-specific classes.// Note that they are only used in the setupDataSource// method. In normal use, your classes interact// only with the standard JDBC API//import org.apache.commons.dbcp2.BasicDataSource;//// Here's a simple example of how to use the BasicDataSource.////// Note that this example is very similar to the PoolingDriver// example.//// To compile this example, you'll want:// * commons-pool-2.3.jar// * commons-dbcp-2.1.jar // in your classpath.//// To run this example, you'll want:// * commons-pool-2.3.jar// * commons-dbcp-2.1.jar // * commons-logging-1.2.jar// in your classpath.////// Invoke the class using two arguments:// * the connect string for your underlying JDBC driver// * the query you'd like to execute// You'll also want to ensure your underlying JDBC driver// is registered. You can use the "jdbc.drivers"// property to do this.//// For example:// java -Djdbc.drivers=org.h2.Driver \// -classpath commons-pool2-2.3.jar:commons-dbcp2-2.1.jar:commons-logging-1.2.jar:h2-1.3.152.jar:. \// BasicDataSourceExample \// "jdbc:h2:~/test" \// "SELECT 1"//public class BasicDataSourceExample { public static void main(String[] args) { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up data source."); DataSource dataSource = setupDataSource(args[0]); System.out.println("Done."); // // Now, we can use JDBC DataSource as we normally would. // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = dataSource.getConnection(); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while(rset.next()) { for(int i=1;i
上一篇:奥维地图云服务器
下一篇:战区手游怎么重启服务器?