private Connection getTargetConnection ( Method operation ) throws SQLException { if ( this . target == null ) { boolean readOnly = MasterSlaveInterceptor . isReadOnly ( ) ; // No target Connection held -> fetch one. if ( log . isDebugEnabled ( ) ) { log . debug ( "Connecting to database for operation '" + operation . getName ( ) + "'" ) ; } // Fetch physical Connection from DataSource. DruidDataSource ds = router . determineTargetDataSource ( readOnly ) ; TraceContext . get ( ) . setServerName ( router . getConfigName ( ) ) . setUrl ( extractHost ( ds . getUrl ( ) ) ) ; this . target = ( this . username != null ) ? ds . getConnection ( this . username , this . password ) : ds . getConnection ( ) ; // If we still lack default connection properties, check them now. //checkDefaultConnectionProperties(this.target); // Apply kept transaction settings, if any. if ( this . readOnly ) { this . target . setReadOnly ( true ) ; } if ( this . transactionIsolation != null ) { this . target . setTransactionIsolation ( this . transactionIsolation ) ; } if ( this . autoCommit != null && this . autoCommit != this . target . getAutoCommit ( ) ) { this . target . setAutoCommit ( this . autoCommit ) ; } } else { // Target Connection already held -> return it. if ( log . isDebugEnabled ( ) ) { log . debug ( "Using existing database connection for operation '" + operation . getName ( ) + "'" ) ; } } return this . target ; }
Return the target Connection fetching it and initializing it if necessary .