JAKARTA CommONS-DBCP

zhaozj2021-02-16  50

Jakarta Commons-Dbcp1.dbcp Introduction to the design of the network program, many of the interactive operations of the relational database, the general operation mode creates a connection to the database when used, and then performs various operations, this simple operation The problem brought about by the way is the frequent opening and closing of the database, as well as the implementation of TRANSTION, which is a very time and resource operation. So the database buffer pool DBCP appears, this package is developed from the POOL package. 2.DBCP basic process implemented in Example 1. Create enericObjectPool GenericObjectPool pool = new GenericObjectPool (null); 2. Create PoolableConnectionFactory DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory ( "jdbc: mysql: // host / db", "username", "password" );

PoolableConnectionFactory pcf = new PoolableConnectionFactory (CF, pool, null, "SELECT * FROM mysql.db", false, true);. 3. Create and registered PoolingDriver new PoolingDriver () registerPool ( "myPool", pool); so far, The database buffer pool is created. 3. DataSource's implementation process DataSource has been implemented in numerous JSP containers and applications, when the container or application server is started, he read the relevant profile and automatically completes DataSource Creating a job, we generally only need to specify in the server's configuration file, which is posted here, which is convenient for everyone to understand its internal implementation mechanism for easy programming references. import javax.sql.DataSource; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class BasicDataSourceExample {

Public static void main (String [] args) {// creates BasicDataSource System.out.println ("Setting Up Data Source."); DataSource Datasource = SetupDataSource (Args [0]); System.out.Println ("DONE. ");

// Create a JDBC Data Source 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 number (). GetColumncount (); while (rset.next) )) {For (INT i = 1; i <= number) {system.out.print ("/ t" rset.getstring (i));} system.out.println (");}} Catch (Sqlexception E) {E.PrintStackTrace ();} finally {r};} catch (exception e) {} try {stmt.close ();} catch (exception e) {} try {conn .close ();} catch (exception e) {}}} // Create a data source public static datasource setupdataroup (string connecturi) {BasicDataSource DS = New BasicDat Asource (); DS.SetDriverclassName ("Oracle.jdbc.driver.OracleDriver"); DS.SETUSERNAME ("Scott"); DS.SetPassword ("Tiger"); DS.SETURL (CONNECTURI); Return DS;} // presentation data source status public static void printDataSourceStats (dataSource ds) throws SQLException {BasicDataSource bds = (BasicDataSource) ds; System.out.println ( "NumActive:" bds.getNumActive ()); System.out.println ( "NumIdle: " bds.getnumidle ());} // Turn off the data source public static void shutdowndatasource (DataSource DS) throws Sqlexception {BasicDataSource BDS = (BasicDataSource) DS;

BDS.Close ();}}} The example is relatively simple, no longer said ~ 4.Tomcat DBCP configuration here is described as a database as a database.

Use DBCP to set the relevant connection and DBCP parameter values ​​in Server.xml. factory org.apache.commons.dbcp.basicDataSourceFactory maxactive 100 maxidle 30 MaxWait 10000 username root password admin driverclassname org.gjt.mm.mysql.driver URL

jdbc: mysql: // localhost: 3306 / javatest? AutoreConnect = true We can see the DBCP Factory he use is org.apache.commons. DBCP.BasicDataSourceFactory5. Parameter Description * DataSource: DataSource to be connected (usually we do not define in server.xml) * DefaultAutOCommit: Does the transaction AutoCommit, the default value true * defaultReadOnly: Whether you can only read it, the default is False * driverclassname: JDBC Driver Class, * MaxActive: The maximum connection database connection, set 0 is not restricted * Maxidle: The number in the maximum waiting connection, set 0 is not limited * MaxWait: Maximum Waiting second number, unit For MS, the error message will be throwing out of the error * password: The password used by the login database * URL: Connect the database URL * UserName: The account used by the login database * ValidationQuery: Verify that the connection is successful, the SQL SELECT instruction is at least return * Removeabandoned : Whether to be self-interruption, default is false * removeabandONEDTimeout: After a few seconds, it will be self-interrupted. Removeabandoned must be true * logabandoned: Do you record an interrupt event, default is false6. Summary DBCP generally applies to the use of the database is very frequently used, it You can handle a large number of database connection requests, not losing the preferred database configuration for large sites. 7. Reference * Jakarta CommonShttp: //jakarta.apache.org/commons/ jakarta common-dbcphttp: //jakarta.apache.org/commons / dbcp * jakarta commons-dbcp apihttp: //jakarta.apache.org/commons/dbcp/apidocs/index.html * CVS information http: //cvs.a Pache.org/viewcvs.cgi/jakarta-commons/dbcp/doc/ This article is copyrighted, not allowed, please contact, please contact: topojuly@tom.com

转载请注明原文地址:https://www.9cbs.com/read-26323.html

New Post(0)