Java configures the dbcp connection pool. After repeated tests, java configures the dbcp connection pool.

Source: Internet
Author: User

Java configures the dbcp connection pool. After repeated tests, java configures the dbcp connection pool.

Required jar package


Java code


Import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. SQLException; import java. util. properties; import javax. SQL. dataSource; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import org. apache. tomcat. dbcp. dbcp. basicDataSourceFactory;/*** tomcat database connection pool management class <br> * use to deploy the environment for tomcat <br> * prepare the database connection configuration file dbcp in the class path. properties ***/public class DBManager {Private static final Log log = LogFactory. getLog (DBManager. class); private static final String configFile = "dbcp. properties "; private static DataSource dataSource; static {Properties dbProperties = new Properties (); try {dbProperties. load (DBManager. class. getClassLoader (). getResourceAsStream (configFile); dataSource = basicperformancefactory. createDataSource (dbProperties); Connection conn = get Conn (); DatabaseMetaData mdm = conn. getMetaData (); log.info ("Connected to" + mdm. getDatabaseProductName () + "" + mdm. getDatabaseProductVersion (); if (conn! = Null) {conn. close () ;}} catch (Exception e) {log. error ("failed to initialize connection pool:" + e) ;}} private DBManager () {}/ *** get link, close ** @ see {@ link DBManager # closeConn (Connection)} * @ return */public static final Connection getConn () {Connection conn = null; try {conn = dataSource. getConnection ();} catch (SQLException e) {log. error ("failed to get database connection:" + e);} return conn;}/***** close connection ** @ param conn * need to be closed */Public static void closeConn (Connection conn) {try {if (conn! = Null &&! Conn. isClosed () {conn. setAutoCommit (true); conn. close () ;}} catch (SQLException e) {log. error ("failed to close database connection:" + e );}}}


Properties File

# Database driver driverClassName = com. mysql. jdbc. driver # database connection Address url = jdbc: mysql: // localhost/lianjc # username = root # password = 123456 # maximum number of database connections in the connection pool. If it is set to 0, there is no limit to maxActive = 30 # the maximum number of idle instances and the maximum idle time for database connection. When the idle time is exceeded, the database connection # is marked as unavailable and then released. If it is set to 0, maxIdle = 10 # The maximum connection wait time is set. If this time is exceeded, an exception occurs. If it is set to-1, maxWait = 1000 # If the removeAbandonedTimeout time is exceeded, whether to recycle connections (obsolete) is useless (false by default, adjusted to true) removeAbandoned = true # When the time limit is exceeded, the connections that are not used (discarded) are recycled (300 seconds by default, adjusted to 180) removeAbandonedTimeout = 180


Thread testing with 10 million connections under no pressure

Import java. SQL. connection; public class TheadTest extends Thread {public static void main (String [] args) {for (int I = 0; I <10000; I ++) {new TheadTest2 (). start () ;}@ Overridepublic void run () {long begin = System. currentTimeMillis (); for (int I = 0; I <100000; I ++) {Connection conn = null; try {conn = new JDBCUtils (). getConnection ();} catch (Exception e) {e. printStackTrace ();} // System. out. println (I); new JDBCUtils (). close (null, null, conn);} long end = System. currentTimeMillis (); System. out. println ("time used:" + (end-begin) ;}} class TheadTest2 extends Thread {@ Overridepublic void run () {long begin = System. currentTimeMillis (); for (int I = 0; I <1000; I ++) {Connection conn = XDBManager. getConn (); // System. out. println (I); XDBManager. closeConn (conn);} long end = System. currentTimeMillis (); System. out. println ("time used:" + (end-begin ));}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.