The database connection pool is the factory of the Connection object:
Because a physical connection is opened when a database connection is created, performance is degraded when it is closed when it is finished. So we will use the database connection pool, in the database connection pool we will create a certain number of connections, when we
To connect to the database, remove the connection from the pool and put it back in the pool when it is exhausted. If the connection in the pool is used. Will wait, and if the wait time is exceeded, the exception will be reported.
For database connection pooling. We can use Apache's Basicdatasource, a class that implements the DataSource interface primarily. For the use of this class we want to put: Commons.pool.jar,commons.dbcp.jar,commons.collections.jar.
1 PackageCom.devil.util;2 3 Importjava.sql.Connection;4 Importjava.sql.SQLException;5 6 ImportOrg.apache.commons.dbcp.BasicDataSource;7 8 Public classDatabasepool {9 Ten Private Static FinalDatabasepool instance =NewDatabasepool (); OneBasicdatasource ds =NewBasicdatasource (); A PrivateDatabasepool () { - Ds.setpassword (Xmldateparse.getpassword ()); - Ds.seturl (Xmldateparse.geturl ()); the Ds.setusername (Xmldateparse.getusername ()); - Ds.setdriverclassname (Xmldateparse.getdriver ()); - - //set the Max data connection pool connection active Count +Ds.setmaxactive (10); - //set the initial connection size +Ds.setinitialsize (2); A //set the min idle size atDs.setminidle (2); - //set the max wait mins -Ds.setmaxwait (2000); - - } - PublicConnection getconnection () { in -Connection conn =NULL; to Try { +conn =ds.getconnection (); -}Catch(SQLException e) { the e.printstacktrace (); * } $ Panax Notoginseng returnConn; - } the + Public StaticDatabasepool getinstance () { A returninstance; the } + - Public voidClose () { $ Try { $ if(ds! =NULL){ - ds.close (); - } the}Catch(SQLException e) { - }Wuyi } the}
Learn about database connection pooling