Java Configuration DBCP Connection pool, tested repeatedly

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" ><strong>java Code </strong></span>
Package cn.songxinqiang.samples.commonsdbcp.util; 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 for Tomcat deployment environment <br> * need to prepare database connection configuration file under Classpath dbcp.properties * * */public class Dbmanag Er {private static final log log = Logfactory.getlog (dbmanager.class); private static final String configfile = "Dbcp.pro   Perties ";   private static DataSource DataSource;  static {Properties dbproperties = new Properties ();   try {dbproperties.load (DBManager.class.getClassLoader (). getResourceAsStream (ConfigFile));     DataSource = Basicdatasourcefactory.createdatasource (dbproperties);   Connection conn = Getconn ();   DatabaseMetaData mdm = Conn.getmetadata (); Log.info ("Connected to" + mdm.getdatabaseproductname () + "" + mdm.getdatabaseproductversion ());   IF (conn! = null) {conn.close ();  }} catch (Exception e) {log.error ("Initialize connection Pool failed:" + e); }} private DBManager () {}/** * get the link, remember to close * * @see {@link dbmanager#closeconn (Connection)} * @return */Pub  Lic static final Connection getconn () {Connection conn = null;  try {conn = datasource.getconnection ();  } catch (SQLException e) {log.error ("Get Database Connection failed:" + e); } return conn; /** * Close Connection * * @param conn * need to close the connection */public static void Closeconn (Connection conn) {try {if (    Conn! = null &&!conn.isclosed ()) {Conn.setautocommit (true);   Conn.close ();  }} catch (SQLException e) {log.error ("Close database connection failed:" + e);  } }  }


Properties file

#数据库驱动driverClassName =com.mysql.jdbc.driver# Database connection address url=jdbc:mysql://localhost/lianjc# user name username=root# Password password=123456  #连接池的最大数据库连接数. Set to 0 to indicate unlimited maxactive=30# maximum idle, maximum idle time for database connections. After idle time, the database connection # will be marked as unavailable and then released. Set to 0 to indicate unlimited maxidle=10# maximum connection wait time. If this time is exceeded, the exception will be received. Set to 1 to indicate unlimited maxwait=1000  


Thread testing, tens of thousands of links without stress

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 ("spents:" + (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 ("spents:" + (End-begin));}}

Required Jar Package



Java Configuration DBCP Connection pool, tested repeatedly

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.