public static DbTypeRegister getRegistry ( final Connection connection ) throws SQLException { // if connection URL is null we can't proceed. fail fast Preconditions . checkNotNull ( connection ) ; final String connectionURL = connection . getMetaData ( ) . getURL ( ) ; Preconditions . checkNotNull ( connection . getMetaData ( ) . getURL ( ) , "connection URL is null" ) ; // check if we have the value in memory DbTypeRegister cachedRegisters = registers . get ( connectionURL ) ; // First check (no locking) if ( cachedRegisters == null ) { synchronized ( typeRegisterLock ) { cachedRegisters = registers . get ( connectionURL ) ; // Second check (with locking) if ( cachedRegisters == null ) { cachedRegisters = new DbTypeRegister ( connection ) ; // read optimization. We are not expecting so many different DBMS URLs, so read is really fast and // the write is a little bit slower which is ok because most of the // times we will use what we have in memory. Copy all previous entries to the new immutable map. // This soudnd't happen very often. registers = ImmutableMap . < String , DbTypeRegister > builder ( ) . putAll ( registers ) . put ( connectionURL , cachedRegisters ) . build ( ) ; } } } return cachedRegisters ; }
situation because we are not expecting so many different jdbc urls .