A method for implementing the database connection pool (3)
Source: Internet
Author: User
DataSourceImpl is a class that implements the interface javax. SQL. DataSource, which maintains an object in the connection pool. Because this class is a protected class, only the methods defined in the DataSource interface are exposed to the user. All other methods are invisible to the user. First, let's take a look at the method getConnection that users can access.
/**
* @ See javax. SQL. DataSource # getConnection (String, String)
*/
Public Connection getConnection (String user, String password) throws SQLException
{
// First, find the idle object from the connection pool.
Connection conn = getFreeConnection (0 );
If (conn = null ){
// Determine whether the maximum number of connections is exceeded. If the maximum number of connections is exceeded
// Wait for a certain period of time to check whether there are idle connections; otherwise, an exception is thrown to inform the user that no available connections exist.
If (getConnectionCount ()> = connParam. getMaxConnection ())
Conn = getFreeConnection (connParam. getWaitTime ());
Else {// The number of connections does not exceed. Obtain a new database connection.
ConnParam. setUser (user );
ConnParam. setPassword (password );
Connection conn2 = DriverManager. getConnection (connParam. getUrl (),
User, password );
// Connection object to be returned by the proxy
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.