DBCP Database Connection Pool Management

Source: Internet
Author: User

This package is primarily a link to manage databases, get links, free connections, set maximum number of connections, initialize connections, reclaim idle time

Creating a connection pool when connecting to a database makes it easy to manage all database links and efficiently use database connections.

The main two objects are Basicdatasource and basicdatasourcefactory, producing Basicdatasource and datasource two data sources.

1) Basicdatasource is configured in the code, the code is as follows

Basicdatasource DataSource =NewBasicdatasource (); Datasource.seturl ("jdbc:mysql://localhost:3306/mydb?usessl=true"); Datasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.setusername ("Root"); Datasource.setpassword ("123456"); Datasource.setinitialsize (5); Datasource.setmaxactive ( -); Datasource.setmaxidle ( -); Try{Connection conn=datasource.getconnection ();        Conn.close (); } Catch(SQLException e) {E.printstacktrace (); }

2) Basicdatasourcefactory generates DataSource object according to the configuration file, the code is as follows

Properties props =NewProperties (); InputStream instream= This. GetClass (). getResourceAsStream ("/db.properties"); Try{props.load (instream); DataSource DataSource=Basicdatasourcefactory.createdatasource (props); Connection Conn=datasource.getconnection (); String SQL="SELECT * from user where id=?"; Queryrunner qr=NewQueryrunner (); User User= Qr.query (conn, SQL,NewBeanhandler<user> (User.class),2); System. out. println (user);                    Conn.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }

DBCP Database Connection Pool Management

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.