How Java connection Pooling works (RPM)

Source: Internet
Author: User
Tags connection pooling

The original: How Java connection pooling works

What is a connection?

Connectivity is a way for our programming language to interact with the database. We often hear the phrase "database connections are expensive."

Some people accept this statement, but do not know its true meaning. So, I'll explain what it is. [If you already know, you can jump to its working principle section]

To create a connected code snippet:

" Jdbc:mysql://your.database.domain/yourdbname " ; Class.forName ("com.mysql.jdbc.Driver"= drivermanager.getconnection ( Connurl);

When we create a connection object, what it does internally:

1. "DriverManager" Check and register the driver,
2. "Com.mysql.jdbc.Driver" is our registered driver, which calls "Connect (URL ...)" in the driver class. Method.
3.com.mysql.jdbc.driver's Connect method creates a "socket connection" based on the "Connurl" of our request and connects to the database with the IP "Your.database.domain", which is the default port 3306.
4. The created socket connection will be used to query the database specified by us and eventually let the program return a result.

Why is it expensive?

Now let's talk about why it's "expensive".

It takes more time to create a socket connection than the actual operation of the query.

This is what we call "database connection is expensive" because the number of connections is 1, and it requires a socket connection to access the DB each time it is created.

Therefore, we will use the connection pool.

Connection pooling is initialized when a certain number of connections are created, and then the connection is reused from the connection pool instead of creating a new one at a time.

How does it work?

Next we'll look at how it works and how to manage or reuse existing connections.

We use the connection pool provider, which has a connection pool manager inside it when it is initialized:

1. It creates the default size of the connection pool, such as specifying the creation of 5 connection objects and storing it in any collection or array of "available" states.

For example, code snippets:

String Connurl = "Jdbc:mysql://your.database.domain/yourdbname"; String Driver= "Com.mysql.jdbc.Driver"; PrivateMap<java.sql.connection, string> ConnectionPool =NULL; Private voidInitpool () {Try{ConnectionPool=NewHashmap<java.sql.connection, string>();      Class.forName (driver);  for(intPoolind = poolsize; Poolind < 0; poolind++) {java.sql.Connection con=drivermanager.getconnection (Dburl); Connectionpool.put (Con,"AVAILABLE"); }  }

2. When we call Connectionprovider.getconnection (), then it gets a connection from the collection, and of course the state changes to "not available."

For example, code snippets:

 PublicJava.sql.Connection getconnection ()throwsClassNotFoundException, SQLException {BooleanIsconnectionavailable =true;  for(Entry<java.sql.connection, string>Entry:connectionPool.entrySet ()) {          synchronized(entry) {if(Entry.getvalue () = = "AVAILABLE") {Entry.setvalue ("Notavailable"); return(java.sql.Connection) Entry.getkey (); } isconnectionavailable=false; }      }      if(!isconnectionavailable)          {Class.forName (driver); Java.sql.Connection Con=drivermanager.getconnection (Connurl); Connectionpool.put (Con,"Notavailable"); returncon; }      return NULL; }

3. When we close the resulting connection, ConnectionProvider is not actually closing the connection. Instead, just change the status to "AVAILABLE".

For example, code snippets:

 Public void throws ClassNotFoundException, SQLException {     for (entry<java.sql.connection, string>  Entry:connectionPool.entrySet ()) {        synchronized  (entry) {            if  ( Entry.getkey (). Equals (connection)) {                //Getting back the conncetion to Pool                Entry.setvalue ("AVAILABLE");     }}}

Basically the connection pooling actually works like this, but it's possible to use different ways.

Now, you may have a problem with whether we can create our own connection pooling mechanism?

My advice is to use a connection pooling mechanism that already exists, such as C3P0,DBCP.


English original, oschina.net translation

How Java connection Pooling works (RPM)

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.