1. When the mybatis built-in connection pool is injected into spring, it only sets the necessary data for some database connections, such as driver, URL, username, and password, and does not connect to the database.
2. The status of the mybatis connection pool is maintained by the poolstate class. The most important is two lists: idleconnections and activeconnections, which are used to save idle connections and active connections respectively, this poolstate object needs to be synchronized during use
3. pooledconnection is the connection object maintained in the connection pool. This class proxy java. SQL. Connection, a common connection, mainly proxies two methods:
① The close method is not to call the close method of realconnection, but to put the connection back into the connection pool.
② The validity test is also added when other methods are called.
if (!Object.class.equals(method.getDeclaringClass())) { // issue #579 toString() should never fail // throw an SQLException instead of a Runtime checkConnection(); }
Note that pooleddatasource provides a static method to obtain realconnection and unwrapconnection (connection conn). You need to input a pooledconnection
4. When a user needs a database connection, the connection pool will call the popconnection method. The connection pool will first check whether there is a connection in the free queue. If so, the first connection will be taken and returned to the user. If no, determine whether the maximum number of active connections is exceeded. If no, create a database connection and call the getconnection method of the non-connection pool.
Conn = new pooledconnection (datasource. getconnection (), this );
The second parameter is the reference of the database connection pool. This parameter will be used when the pooledconnection is closed and put back in the connection pool. The pushconnection (pooledconnection conn) method in pooleddatasource will be called.
I drew a flowchart for the popcollection method, which is not good. Sorry ~
5. It is called when sqlsessionfactory is initialized. A sqlsessionfactory is bound to a datasource.
Vendordatabaseidprovider. getdatabaseid (datasource)
// The databaseidprovider here does not seem to be null, because there is a new if (this. databaseidprovider during initialization! = NULL) {try {configuration. setdatabaseid (this. databaseidprovider. getdatabaseid (this. datasource);} catch (sqlexception e) {Throw new nestedioexception ("failed getting a databaseid", e );}}
6. To obtain the databaseid, connect to the database to obtain the metadata of the database (metadata)
7. Protected Boolean pingconnection (pooledconnection conn)
This method is used to test whether the connection is still available and used in the pooledconnection. isvalid method.
Use poolpingquery in pooleddatasource to set the SQL statement used for testing