Introduction and definition of Java database connection Pool A simple connection pool
What is a connection pool
Java uses JDBC to manipulate the database, we usually encapsulate the code of the JDBC duplicate into a Dbutil tool class, but in this case, because each operation of the database needs to establish a connection and release the connection, will cause a lot of resource consumption, when the Web application has a large number of users to access, This is going to be a very serious problem.
Therefore, in order to reduce the performance overhead, introduced the concept of connection pooling, that is, the connection with the database into a container, when the program needs to use the connection to the container request instead of establishing a connection to the database, release is also put the connection back to the container, rather than close the connection, in fact, there are some places in Java also "pool The concept, the normal volume pool, the thread pool, are to improve the efficiency of the program running, interested can be understood under.
How to customize a connection pool
Java provides an interface Java.sql.DataSource, for the user to define a connection pool, if we want to customize a connection pool, then we can implement this interface, nonsense, directly paste code
This is because I'm just doing a simple connection pool for practiced hand, so there's no implementation of the DataSource interface, just a way to get the connection and put it back. There are a bit more comments, so maybe the code looks a bit long
With connection pooling, you can modify the access and release resources directly in the original dbutil.
Originally wanted to use the current commonly used connection pool Code also came out, but it seems a bit long, so we open a separate "room"
For any doubts or errors in this article, please comment below the article,
I buckle 3592867153, Network name and blog account name consistent
New registration of a number, for learning and communication, welcome you to guide the next
Introduction and definition of Java database connection Pool A simple connection pool