Tomcat JDBC pool source code analysis (2)

Source: Internet
Author: User

In the previous article, we analyzed how to obtain the connection. In this article, we analyzed how to return the connection and clean up the connection. Returning a connection is basically an entry:

 
  1. protected void returnConnection(PooledConnection con) { 
  2.        if (isClosed()) { 
  3.            //if the connection pool is closed 
  4.            //close the connection instead of returning it 
  5.            release(con); 
  6.            return; 
  7.        } //end if 
  8.  
  9.        if (con != null) { 
  10.            try { 
  11.                con.lock(); 
  12.  
  13.                if (busy.remove(con)) { 
  14.  
  15.                    if (!shouldClose(con,PooledConnection.VALIDATE_RETURN)) { 
  16.                        con.setStackTrace(null); 
  17.                        con.setTimestamp(System.currentTimeMillis()); 
  18.                        if (((idle.size()>=poolProperties.getMaxIdle()) && !poolProperties.isPoolSweeperEnabled()) || (!idle.offer(con))) { 
  19.                            if (log.isDebugEnabled()) { 
  20.                                log.debug("Connection ["+con+"] will be closed and not returned to the pool, idle["+idle.size()+"]>=maxIdle["+poolProperties.getMaxIdle()+"] idle.offer failed."); 
  21.                            } 
  22.                            release(con); 
  23.                        } 
  24.                    } else { 
  25.                        if (log.isDebugEnabled()) { 
  26.                            log.debug("Connection ["+con+"] will be closed and not returned to the pool."); 
  27.                        } 
  28.                        release(con); 
  29.                    } //end if 
  30.                } else { 
  31.                    if (log.isDebugEnabled()) { 
  32.                        log.debug("Connection ["+con+"] will be closed and not returned to the pool, busy.remove failed."); 
  33.                    } 
  34.                    release(con); 
  35.                } 
  36.            } finally { 
  37.                con.unlock(); 
  38.            } 
  39.        } //end if 
  40.    } // 

Check whether the connection pool is closed. If the connection pool is closed, call release (con) to close the connection.

Obtain the lock of the current connection and remove the connection from the busy queue. Put it in an idle queue for use by other requests. In the following cases, the connection will be closed directly, rather than put into the idle queue.

1. failed to remove the connection from the busy queue. This phenomenon is rare)

2. the maximum number of idle connections allowed by the number of idle connections is not enabled.

3. failed to put in the idle queue. Similar to 1, which is rare)

It can be seen that returning the connection is relatively simple.

For connection cleaning, the connection pool provides a Timer to complete:

Class PoolCleaner extends TimerTask

The Timer mainly cleans up connections in three aspects.

1. The connection to be discarded is cleared from the busy queue.

This type of connection is mainly a connection that has not been returned for a long time. The Connection Pool provides two time values: AbandonTimeout and SuspectTimeout. If the number of connections that exceed the value of AbandonTimeout is exceeded, close the Pool if it meets the conditions for cleaning up the Abandon connection.

For connections larger than SuspectTimeout, log notifications are output.

2. idle connections are cleared from the idle queue.

Close connections that are idle for too long.

3. Verify the idle connection if.

The VALIDATE_IDLE authentication method is a user-specified authentication policy for idle connections.

 

This article from the "World without thieves" blog, please be sure to keep this source http://guojuanjun.blog.51cto.com/277646/1185901

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.