DBCP is a
Java
connection pool project on Apache and a connection pool component used by tomcat. Dbcp requires three separate packages:
common-dbcp.jar
,
common-pool.jar
,
common-collections.jar
due to the establishment of a database connection behavior is a very time-consuming and resource intensive, so the pre-established through connection pooling some of the same database connection, in memory, the application needs to establish a direct connection to the database Apply for one in the pool, and then put it back after use. dbcp does not automatically recover idle connections. DBCP has minor bugs and is generally not recommended.
Method 1: Get database information directly in Java code and establish a database connection pool.
@Test public void test () throws Exception { BasicDataSource source = new BasicDataSource (); // Create a database connection pool source.setDriverClassName ("com.mysql.jdbc.Driver"); // Drive source.setUrl ("jdbc: mysql : // localhost: 3306 / test "); // url source.setUsername (" root "); // username source.setPassword (" 123456 "); // password Connection conn = source.getConnection (); // Get database connection System.out.println (conn); // Print database connection }
Method 2: Obtain database information through configuration file and establish database connection pool
@Test public void throws Exception { Properties pros = new Properties (); InputStream is = DBCPTest. Class .getClassLoader (). GetResourceAsStream ("dbcp.properties" ); pros.load (is); // Get the configuration file DataSource source = BasicDataSourceFactory.createDataSource (pros); // Create a database connection pool. Connection conn = source.getConnection (); // Get connection System.out.println (conn); // Print connection }
The configuration file can be named arbitrarily: xxx.properties is usually named dbcp.properties.
driverClassName = com.mysql.jdbc.Driver
url = jdbc: mysql: // localhost: 3306 / jdbc
username = root
password = 123456
# <!-initial connection->
initialSize = 10
#maximum
number of connections maxActive = 50
# < !
-Maximum idle connection-> maxIdle = 20
# <!-Minimum idle connection->
minIdle = 5
# <!-Timeout waiting time in milliseconds 6000 milliseconds / 1000 equals 60
seconds- > maxWait = 60000
#The format of the connection property attribute attached when the JDBC driver establishes the connection must be as follows: [property name = property;] #Note
: The two properties "user" and "password" will be passed explicitly, so there is no need to include them here .
connectionProperties = useUnicode = true;
characterEncoding = gbk #Specify the auto-commit status of connections created by the connection pool.
defaultAutoCommit = true
#driver default specifies the read-only status of the connection created by the connection pool.
#If the value is not set, the "setReadOnly" method will not be called. (Some drivers do not support read-only mode, such as Informix)
defaultReadOnly =
#driver default Specifies the transaction level (TransactionIsolation) of the connection created by the connection pool.
#Available value is one of the following: (For details, see javadoc.) NONE, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation = READ_UNCOMMITTED