public void cleanDataSources ( ) { Hashtable < String , List < String > > removedDsKeyTable = null ; //gather all the empty ds's key, can't remove item while iterate Enumeration < String > allPoolKeys = dbmsPoolTable . keys ( ) ; while ( allPoolKeys . hasMoreElements ( ) ) { String dbPoolKey = allPoolKeys . nextElement ( ) ; Hashtable < String , DataSource > dsTable = dbmsPoolTable . get ( dbPoolKey ) ; Enumeration < String > allDsKeys = dsTable . keys ( ) ; while ( allDsKeys . hasMoreElements ( ) ) { String dsKey = allDsKeys . nextElement ( ) ; DataSource ds = dsTable . get ( dsKey ) ; //c3p0 impl if ( ds != null && ds instanceof PooledDataSource ) { PooledDataSource pDs = ( PooledDataSource ) ds ; int conCount ; try { conCount = pDs . getNumConnectionsAllUsers ( ) ; } catch ( SQLException e ) { // todo logger.error // ("Failed to get total number of connections for datasource. dbmsPoolKey = " // + dbPoolKey, e); continue ; } //no connections if ( conCount == 0 ) { List < String > removedList = null ; if ( removedDsKeyTable == null ) { removedDsKeyTable = new Hashtable <> ( ) ; } else { removedList = removedDsKeyTable . get ( dbPoolKey ) ; } if ( removedList == null ) { removedList = new ArrayList <> ( ) ; removedList . add ( dsKey ) ; removedDsKeyTable . put ( dbPoolKey , removedList ) ; } else { removedList . add ( dsKey ) ; } } } } } //have empty ds if ( removedDsKeyTable != null && ! removedDsKeyTable . isEmpty ( ) ) { Enumeration < String > removedPoolKeys = removedDsKeyTable . keys ( ) ; while ( removedPoolKeys . hasMoreElements ( ) ) { String removedPoolKey = removedPoolKeys . nextElement ( ) ; PooledDataSourceProvider provider = this . getProvider ( removedPoolKey ) ; List < String > removedDsList = removedDsKeyTable . get ( removedPoolKey ) ; Hashtable < String , DataSource > dsTable = dbmsPoolTable . get ( removedPoolKey ) ; for ( String dsKey : removedDsList ) { DataSource removedDs = dsTable . remove ( dsKey ) ; try { provider . closePooledDataSource ( removedDs ) ; } catch ( SQLException e ) { //can't show the dsKey since it has encrypted password there // todo logger.error("Failed to close datadsource in dmbs poolKey = " // + removedPoolKey, e); continue ; } //tracing // todo if (logger.isDebugEnabled()) { // logger.debug("Removed one datasource in dbms poolKey = " // + removedPoolKey); // } } //don't have any ds for the pool key if ( dsTable . isEmpty ( ) ) { dbmsPoolTable . remove ( removedPoolKey ) ; //tracing // todo if (logger.isDebugEnabled()) { // logger.debug("Removed dbms poolKey = " + removedPoolKey); // } } } } }
clean any empty datasource and pool in the dbmsPool table .