How is JDBC used to ensure that the database client and database server are connected?
By code:
Conn=drivermanager.getconnection (URL, username, password);
JDBC builds a physical connection of the client application to the back-end database through the invocation of this code method. A large number of TCP-based client-to-server interactions occur during this time.
Because of the large network overhead of network transmissions across machines, time costs are high.
In the traditional multithreaded JDBC service, we need to allocate one thread per service, each thread to establish a database connection, and when the service is finished, the thread and the database connection will be destroyed.
When we need the next database service, a new thread and database connection is established again.
This wastes a lot of time in establishing a database connection, and the user's corresponding time becomes slow.
The connection pool appears to solve this problem, the connection pool implements the database connection (Connection object) reuse, when a thread finishes processing the corresponding program service, and then does not immediately destroy the database connection, but to the next thread to use. This avoids the problem of wasting time creating database connections multiple times. A transition was implemented that creates a database connection from each thread to a leased database connection per thread.
How does the MySQL database distribute various connections internally?
In the MySQL database, there are also memory limitations, when the allocation of a large number of database connections, will increase the use of database memory, increasing the conflicts of various locks, the use of database resources, and even caused the database crash. Therefore, the maximum number of connections is generally set in the MySQL database.
However, in order to ensure that there is no such problem, we can limit the flow before the connection request reaches the database, ensure the optimal utilization of the database resources, and efficiency is paramount. The connection pool plays this role: restricting connections.
What exactly is a connection pool?
A connection pool is essentially a set of Java jar packages, between the Java application and the JDBC database physical connection, which helps the application to manage the database connection, the interface exposed by the connection pool, the application can obtain the JDBC connection, after use
return JDBC to the connection pool for use by the next thread.
In connection pooling, connections are automatically created when there are insufficient database connections, and connections are automatically destroyed when too many idle connections are made.
The connection pool also provides queued-waiting functionality when multiple threads are accessed concurrently. Ensures that the application obtains a database connection in an orderly manner.
How is the connection pool used?
Take the current most popular JDCP connection pool as an example.
Related JAR Packages:
http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
http://commons.apache.org/proper/commons-pool/download_pool.cgi
http://commons.apache.org/proper/commons-logging/download_logging.cgi
Where there are two versions of the jar, Windows recommends downloading. zip version
For the differences between the two editions, refer to:
http://blog.csdn.net/suyu_yuan/article/details/52733117
https://www.zhihu.com/question/26026741
1) Create a connection pool object:
Because the bottom of the JDCP is also implemented through JDBC, we need to tell it the appropriate information.
Database connection pool (based on MySQL database)