Java Configuration DBCP Connection pool (database connection pool) sample sharing _java

Source: Internet
Author: User
Tags connection pooling tomcat

Use jar Packs: Commons-dbcp-1.4.jar, Commons-pool-1.6.jar, Commons-logging-1.1.3.jar, plus JDBC driver for the database, for JAVA6 and above platforms

Connection Pooling Management class

Copy Code code as follows:

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
*
* @author Song Xinqiang
* @mail songxinqiang123@gmail.com
*
* @time 2013-12-27
*
*/
public class Dbmanager {
private static Final 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 = 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 ("Initializing Connection Pool failed:" + e);
}
}

Private Dbmanager () {
}

 /**
  * Get the link and remember to close the
  *
  * @see {@link dbmanager#closeconn (Connection)}
  * @return
  */
 public static final Connection getconn () {
  connection conn = null;
  try {
   conn = datasource.getconnection ();
&NBSP;&NBSP} catch (SQLException e) {
   log.error ("Get Database Connection failed:" + e);
&NBSP;&NBSP}
  return conn;
 .}

/**
* Close Connection
*
* @param Conn
* Connections that 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 ("Shutdown database Connection failed:" + e);
}
}

}

Connection configuration file

Copy Code code as follows:

#数据库驱动
Driverclassname=com.mysql.jdbc.driver
#数据库连接地址
Url=jdbc:mysql://localhost/ctg
#用户名
Username=root
#密码
password=123456

#连接池的最大数据库连接数. Set to 0 means no limit
Maxactive=30
#最大空闲数 The maximum idle time for a database connection. More than idle time, the database company
#接将被标记为不可用, and then be released. Set to 0 means no limit
maxidle=10
#最大建立连接等待时间. If you exceed this time, you will receive an exception. Set to-1 means no limit
maxwait=1000

#超过removeAbandonedTimeout时间后, Recycle (default is False, adjust to true) for unused connection (obsolete)
Removeabandoned=true
#超过时间限制, recycle No (obsolete) connections (defaults to 300 seconds, adjusted to 180)
removeabandonedtimeout=180

Test Connection pool productivity while demonstrating how to get connections and close connections

Copy Code code as follows:

public class Connectionsource {
public static void Main (string[] args) {
Long Begin=system.currenttimemillis ();
for (int i=0;i<10000;i++) {
Connection Conn=dbmanager.getconn ();
System.out.print (i+ "");
DBMANAGER.CLOSECONN (conn);
}
Long End=system.currenttimemillis ();
System.out.println ("spents:" + (End-begin));
}
}

Related Article

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.