Java uses a standalone database connection pool (dbcp as an example)

Source: Internet
Author: User
Tags connection pooling getmessage tomcat server

At present, the vast majority of software systems use the database, and after the software is built, access to the database becomes the software system performance of the short board (I/O operations). In general, a database connection is required to access the database at one time. Each time you create a database connection, you need access, allocate idle resources, consume resources, release resources, and end access. It's very time-consuming and space-intensive.

So the data connection pool technology is produced, the principle is to establish a database connection management layer between the data requester and the database. Assign a certain number of connections when the system is started, and maintain these connections, keeping the minimum number of connections. At the same time, in order to protect the database and system, set a maximum number of connections, alleviate the database access pressure.

This allows the manager to assign a connection to the data requester each time the data requester needs to connect, and the data requester uses the connection to return the connection to the manager, thus minimizing unnecessary database connection consumption.

The Java Database service JDBC does not provide an actual connectionpool implementation, but leaves out an interface to support a third-party service provider, and I think the most important interface is datasource.

There are actually four ways that Java accesses a database mainly:
First, the use of Drivermananger to direct programming implementation, this is the most basic way.
Drivermananger key steps to implement a database connection are:
1>class.forname ("Com.ibm.db2.jcc.DB2Driver"). newinstance ();
2>connection conn = null;
3>conn = drivermanager.getconnection (URL, "name", "password");

Second, use the server configuration file to implement the connection pool.
Some servers provide their own database connection pooling services, such as the Tomcat server, which provides a data pool connection service supported by COMMONS-DBCP, just fill in the corresponding values in the context.xml.

Third, configuration file configuration DataSource
DataSource is an interface provided in the JAVAAPI, third-party services after the implementation of DataSource, such as the Basicdatasource of DBCP, in order to improve the configuration of the database connection pool, the use of IOC mode (control inversion), Separating the actual bean from the configuration data, the configuration data is placed in the configuration file (typically using XML). To implement control over multiple databases.

After the DataSource object is instantiated, it can be connected via datasource.getconnection ().

Iv. connecting the bridge by configuring the JDBC-ODBC
Have heard, have not used, have no say.

Use the COMMONS-DBCP provided by Apache here to implement a separate, simple database connection pool.

First, you need three kits: Commons-pool.jar,commons-collection.jar,commons-dbcp.jar.

The key implementation snippet is shown below:
Java code
  1. Holds a static database connection pool Object
  2. Private static DataSource DS;
  3. Implement DataSource interface with Basicdatasource provided by DBCP
  4. Public static void InitDataSource (String Connecturi, string username,
  5. string password, string driverclass, int initialsize,
  6. int maxactive, int maxidle, int maxwait) {
  7. Basicdatasource ds = new Basicdatasource ();
  8. Ds.setdriverclassname (Driverclass);
  9. Ds.setusername (username);
  10. Ds.setpassword (password);
  11. Ds.seturl (Connecturi);
  12. Ds.setinitialsize (initialsize);
  13. Ds.setmaxactive (maxactive);
  14. Ds.setmaxidle (Maxidle);
  15. Ds.setmaxwait (maxwait);
  16. ds = ds;
  17. }
  18. Get a database connection
  19. Public Connection getconnection () {
  20. Connection con = null;
  21. if (DS! = null) {
  22. try {
  23. con = ds.getconnection ();
  24. } catch (Exception e) {
  25. System.out.println (E.getmessage ()); }
  26. //Set the database connection to not default to automatic commit
  27. try {
  28. Con.setautocommit (false);
  29. } catch (SQLException e) {
  30. System.out.println (E.getmessage ()); }
  31. return con;
  32. }
  33. //When retrieving database connections, use Con.close () directly
  34. return con;
  35. }
  36. Recycle database connections
  37. protected static void Shutdowndatasource () throws SQLException {
  38. Basicdatasource BDS = (basicdatasource) DS;
  39. Bds.close ();
  40. }


The above code is extracted from the actual use of code, I hope to help you.

At present, OpenSource provides a lot of database connection pool technology, the use of processes and patterns, and the above code is similar. At the same time the latest release of BONECP I have never felt, I hope to have friends to share with me the use of experience.

    • Commons-collections-3.2.1.jar (561.9 KB)
    • Download number of times: 73
    • Commons-pool-1.5.5.jar (97.8 KB)
    • Download number of times: 55
    • Commons-dbcp-1.4.jar (156.8 KB)
    • Download number of times: 59

Java uses a standalone database connection pool (dbcp as an example)

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.