Understanding of database connection pool data Source pool
1. The database connection pool allows applications to reuse an existing database connection instead of re-establishing a connection, avoiding the resource and time spent by new connection in each method.
2. The database connection pool is initialized at the start of the project to make it easy to invoke the connection that have been created at any time when the program is ready to run. Just need getconnection () just fine.
The establishment and disconnection of connection are managed by the connection pool itself.
3. Creating connection is a time-consuming operation, so it is recommended to create a connection when the project is started. It is time-consuming to avoid new connection when it is necessary to connection in a method.
4. Database connection pool, how to return connection?
Connection Connection = Pool.getconnection ();
Pool.release (connection);//Return resources
5. When the program starts, how does it initialize a data connection pool?
A: Spring start automatically injects the beans into the same pool class as the new already configured. The connection pool class declares the static list, which is used to put connection. The conection add is then put into the list by a static block of code. This way, when the entire list is in the new bean, it is created when the connection pool class is loaded. When the late program runs, those connection already exist.
Connection's understanding
1.JDBC connection is a TCP instance that connects to a database.
2.connection is a long-connected TCP. Unlike HTTP, which is a short connection, it takes 3 handshakes each time to build.
3.connection long connections are performance-enhancing. However, there are some details that need to be addressed, that is, when MySQL discovers a link for a long time without executing a query request, it automatically disconnects the connection.
4. How long it takes to break down, there is a timeout setting time. Via sql: "Show global variables like '%timeout ';" View.
When the link has expired, still go to perform query operation, an obvious manifestation is prompt: MySQL server has gone away
5. The database connection pool passes the heartbeat mechanism, sending empty packets for a period of time to maintain connection survival.
6. Long connection is suitable for large amount of data transmission, such as: Database, redis,memcached and other requirements fast, large amount of data in the case.
7. We usually use the database connection is long-connected, because we are every time from the database connection pool to get connection!!! The DB source is long connected!!
8.mysql The default connection timeout time is 8 hours. This can be modified through the My.ini configuration file.
9.connection is not thread-safe! Connection is not thread-safe, and when used in a multithreaded environment, it can cause confusion in data operations, especially in the case of transactions. The Connection.commit () method is to commit the transaction, and you
As you can imagine, in a multithreaded environment, thread A opens a transaction, and then thread B unexpectedly commits, which is a very tangled case.
10. Multiple wire Chengtong with a connection will it improve efficiency and reduce the consumption of multiple connections? A: No, because connection, each method is synchronized, all performed synchronization. So it doesn't improve efficiency.
Such as:
public int executeupdate () {
Synchronized (connection) {
Do
}
}
11. Beginners are generally used in two ways connection:1. Or just one connection, and multiple threads using a connection. 2. Either a connection is created in each method, each call is created
A connection.
Both of these are inefficient.
Because the creation of TCP links is expensive, there is also a limit to the number of TCP concurrent connections that DB server can host. Therefore, it is unrealistic to create a connection for each invocation, which is why the database connection pool appears.
Create a database connection pool:
Public classSimplepooldemo {//Create a connection pool Private StaticLinkedlist<connection> pool =NewLinkedlist<connection>(); //Initialize 10 connections Static{ Try { for(inti =0; I <Ten; i++) {Connection conn= Dbutils.getconnection ();//Get a connectionPool.add (conn); } } Catch(Exception e) {Throw NewExceptionininitializererror ("database connection failed, please check the configuration"); } } //get a connection from the pool Public StaticConnection Getconnectionfrompool () {returnPool.removefirst ();//Removing a Connection object } //Freeing Resources Public Static voidrelease (Connection conn) {pool.addlast (conn); }}
Understanding of database connection pooling and connection