How "Java" Java connection pooling works

Source: Internet
Author: User

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:
String connurl = "Jdbc:mysql://your.database.domain/yourdbname";  Class.forName ("Com.mysql.jdbc.Driver"); Connection con = 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 socket connection created will be used to query the database we specify 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 connection pooling, create a certain number of connections when the connection pool is initialized, and then reuse the connection 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.
String connurl = "Jdbc:mysql://your.database.domain/yourdbname";
String Driver = "Com.mysql.jdbc.Driver";
private map<java.sql.connection, string> connectionpool = null;
private void Initpool () {
try {
ConnectionPool = new hashmap<java.sql.connection, string> ();
Class.forName (driver);
for
(int poolind = 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."
Public
java.sql.Connection getconnection () throws ClassNotFoundException, SQLException
   {  
Boolean isconnectionavailable = 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");
return con;
       }  
return null;
   }  
3. When we close the resulting connection, ConnectionProvider is not actually closing the connection. Instead, just change the status to "AVAILABLE".
Public
void CloseConnection (Java.sql.Connection Connection) 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.

How "Java" Java connection pooling works

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.