protected Connection newConnectionFromDataSource ( JdbcConnectionDescriptor jcd ) throws LookupException { Connection retval = null ; // use JNDI lookup DataSource ds = jcd . getDataSource ( ) ; if ( ds == null ) { // [tomdz] Would it suffice to store the datasources only at the JCDs ? // Only possible problem would be serialization of the JCD because // the data source object in the JCD does not 'survive' this ds = ( DataSource ) dataSourceCache . get ( jcd . getDatasourceName ( ) ) ; } try { if ( ds == null ) { /** * this synchronization block won't be a big deal as we only look up * new datasources not found in the map. */ synchronized ( dataSourceCache ) { InitialContext ic = new InitialContext ( ) ; ds = ( DataSource ) ic . lookup ( jcd . getDatasourceName ( ) ) ; /** * cache the datasource lookup. */ dataSourceCache . put ( jcd . getDatasourceName ( ) , ds ) ; } } if ( jcd . getUserName ( ) == null ) { retval = ds . getConnection ( ) ; } else { retval = ds . getConnection ( jcd . getUserName ( ) , jcd . getPassWord ( ) ) ; } } catch ( SQLException sqlEx ) { log . error ( "SQLException thrown while trying to get Connection from Datasource (" + jcd . getDatasourceName ( ) + ")" , sqlEx ) ; throw new LookupException ( "SQLException thrown while trying to get Connection from Datasource (" + jcd . getDatasourceName ( ) + ")" , sqlEx ) ; } catch ( NamingException namingEx ) { log . error ( "Naming Exception while looking up DataSource (" + jcd . getDatasourceName ( ) + ")" , namingEx ) ; throw new LookupException ( "Naming Exception while looking up DataSource (" + jcd . getDatasourceName ( ) + ")" , namingEx ) ; } // initialize connection initializeJdbcConnection ( retval , jcd ) ; if ( log . isDebugEnabled ( ) ) log . debug ( "Create new connection using DataSource: " + retval ) ; return retval ; }
Creates a new connection from the data source that the connection descriptor represents . If the connection descriptor does not directly contain the data source then a JNDI lookup is performed to retrieve the data source .