[ 
https://issues.apache.org/jira/browse/DBCP-287?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12705471#action_12705471
 ] 

Kenny Huang edited comment on DBCP-287 at 5/3/09 7:57 PM:
----------------------------------------------------------

I use a list store the last visited time that conn is used. Attached code.
---------------------------------------------------------------------
public static DataSource qaDB = null;
private static HashMap<Integer, Calendar> connectTime = null;   // use hashcode 
as key
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss"); 
private static int MINUTES_TIME_OUT = 5;  // 5 minutes

static {
    Properties properties_ = new Properties();
    properties_.put("driverClassName", "oracle.jdbc.driver.OracleDriver");
    properties_.put("url", "xxxxx");
    properties_.put("username", "xxxx");
    properties_.put("password", "xxxx");
    properties_.put("accessToUnderlyingConnectionAllowed", "true");  // this 
properties must be set.
    try {
        qaDB = BasicDataSourceFactory.createDataSource(properties_);
    } catch(Exception e) {
        qaDB = null;
    }
    connectTime = new HashMap<Integer, Calendar>();
}

public static Connection getQAConnection() {
    if (qaDB != null) {
        try {
            while(true) {
                Connection conn = qaDB.getConnection();
                
                Calendar curTime = Calendar.getInstance();
                int hashCode = conn.hashCode();
                
                if (connectTime.containsKey(hashCode)) {
                    // compare the last visited time
                    Calendar lastTime = connectTime.get(hashCode);
                    lastTime.add(Calendar.MINUTE, MINUTES_TIME_OUT);
                    
                    if (lastTime.before(curTime)) {
                        // timeout
                        if (conn instanceof DelegatingConnection) {
                            Connection delegate = 
((DelegatingConnection)conn).getDelegate();
                            if (delegate instanceof PoolableConnection)
                                ((PoolableConnection)delegate).reallyClose();
                            try {
                                conn.close();
                            } catch(Exception e_) { }
                            connectTime.remove(hashCode);
                            
                        } else {
                            conn.close();
                            connectTime.remove(hashCode);
                        }
                        System.out.println("Del-> " + hashCode);
                        
                    } else {
                        // update the last visited time
                        connectTime.put(hashCode, curTime);
                        System.out.println("Upd-> " + hashCode);
                        return conn;
                    }
                    
                } else {
                    connectTime.put(hashCode, curTime);
                    System.out.println("New-> " + hashCode);
                    return conn;
                }
            }
        } catch(SQLException e) { }
    }
    return null;
}


      was (Author: yourenzhuce):
    I use a list store the last visited time that conn is used. Attached code.
---------------------------------------------------------------------
public static DataSource qaDB = null;
private static HashMap<Integer, Calendar> connectTime = null;   // use hashcode 
as key
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss"); 
private static int MINUTES_TIME_OUT = 5;  // 5 minutes

static {
        Properties properties_ = new Properties();
        properties_.put("driverClassName", "oracle.jdbc.driver.OracleDriver");
        properties_.put("url", "xxxxx");
        properties_.put("username", "xxxx");
        properties_.put("password", "xxxx");
        properties_.put("accessToUnderlyingConnectionAllowed", "true");  // 
this properties must be set.
        try {
                qaDB = BasicDataSourceFactory.createDataSource(properties_);
        } catch(Exception e) {
                qaDB = null;
        }
        connectTime = new HashMap<Integer, Calendar>();
}

public static Connection getQAConnection() {
        if (qaDB != null) {
                try {
                        while(true) {
                                Connection conn = qaDB.getConnection();
                                
                                Calendar curTime = Calendar.getInstance();
                                int hashCode = conn.hashCode();
                                
                                if (connectTime.containsKey(hashCode)) {
                                        // compare the last visited time
                                        Calendar lastTime = 
connectTime.get(hashCode);
                                        lastTime.add(Calendar.MINUTE, 
MINUTES_TIME_OUT);
                                        
                                        if (lastTime.before(curTime)) {
                                                // timeout
                                                if (conn instanceof 
DelegatingConnection) {
                                                        Connection delegate = 
((DelegatingConnection)conn).getDelegate();
                                                        if (delegate instanceof 
PoolableConnection)
                                                                
((PoolableConnection)delegate).reallyClose();
                                                        try {
                                                                conn.close();
                                                        } catch(Exception e_) { 
}
                                                        
connectTime.remove(hashCode);
                                                        
                                                } else {
                                                        conn.close();
                                                        
connectTime.remove(hashCode);
                                                }
                                                System.out.println("Del-> " + 
hashCode);
                                                
                                        } else {
                                                // update the last visited time
                                                connectTime.put(hashCode, 
curTime);
                                                System.out.println("Upd-> " + 
hashCode);
                                                return conn;
                                        }
                                        
                                } else {
                                        connectTime.put(hashCode, curTime);
                                        System.out.println("New-> " + hashCode);
                                        return conn;
                                }
                        }
                } catch(SQLException e) { }
        }
        return null;
}

  
> Socket Connection Timeout Fix for Connection Pool management
> ------------------------------------------------------------
>
>                 Key: DBCP-287
>                 URL: https://issues.apache.org/jira/browse/DBCP-287
>             Project: Commons Dbcp
>          Issue Type: Task
>         Environment: JDK 1.5 on Linux Tomcat 5.5.23 with DBCP
>            Reporter: Ajay Warrier
>
> I have an apache tomcat server configured in the Production with DBCP 
> Connection Pool parameters configured to talk to the Database.
> The Application war files looks at the datasource that is setup in the 
> context.xml to connect to the DB
> Now the issue is....There is a firewall between the Tomcat Server and the DB. 
> and the firewall is programmed to timeout and disconnect any existing
> sockets after an hour of inactivity. When this happens , and a new connection 
> is tried from the Tomcat Server to the DB, it fails as TOmcat still things 
> that 
> the connection is active and tries to make an attempt on this Socket timed 
> out connection and then it eventually times out.
> Here is a snippet from my context.xml
> <Resource name="jdbc/TESTDB" auth="Container" type="javax.sql.DataSource" 
> driverClassName="oracle.jdbc.driver.OracleDriver" 
> url="jdbc:oracle:thin:@SERVER_NAME:1521:TEST_DB_SID" username="TEST1" 
> password="TEST1" maxActive="200" maxIdle="10" maxWait="10000" 
> removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
> Can anyone help me resolve this issue? ..

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to