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:
- "DriverManager" Check and register the driver,
- "Com.mysql.jdbc.Driver" is our registered driver, which calls "Connect (URL ...)" in the driver class. Method.
- 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.
- 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