Java database connection pool and java Database Connection

Source: Internet
Author: User

Java database connection pool and java Database Connection

1. About

Java uses jdbc to directly connect to the database, and often gets the connection and runs out of release, which wastes system resources.

2. Code

Java code

Package com. cdv. mam. db; import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. SQLException; import java. util. properties; import javax. SQL. dataSource; import org. apache. commons. dbcp. basicDataSourceFactory; 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> * used as container T deployment environment <br> * You need to 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 = Ba SicDataSourceFactory. 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 ("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 (dbcp. properties)

# Database driver driverClassName = com. mysql. jdbc. driver # database connection Address url = jdbc: mysql: // 192.168.1.41: 3306/dmc # username = root # password = root # 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

Jar used

 

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.