public static Connection createTempConnection ( StorageService storageService , final DataSource dataSource ) throws RepositoryException { if ( DataSource . JNDI_VENDOR . equals ( dataSource . getVendor ( ) ) ) { return getJNDIConnection ( storageService , dataSource ) ; } final String driver = dataSource . getDriver ( ) ; try { Class . forName ( driver ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; LOG . error ( e . getMessage ( ) , e ) ; throw new RepositoryException ( "Driver '" + driver + "' not found." , e ) ; } final String url = dataSource . getUrl ( ) ; final String username = dataSource . getUsername ( ) ; final String password = dataSource . getPassword ( ) ; Settings settings = storageService . getSettings ( ) ; int connectionTimeout = settings . getConnectionTimeout ( ) ; if ( connectionTimeout <= 0 ) { // wait as long as driver manager (not deterministic) try { if ( driver . equals ( CSVDialect . DRIVER_CLASS ) ) { return DriverManager . getConnection ( url , convertListToProperties ( dataSource . getProperties ( ) ) ) ; } else { return DriverManager . getConnection ( url , username , password ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; LOG . error ( e . getMessage ( ) , e ) ; Locale locale = LanguageManager . getInstance ( ) . getLocale ( storageService . getSettings ( ) . getLanguage ( ) ) ; ResourceBundle bundle = ResourceBundle . getBundle ( "ro.nextreports.server.web.NextServerApplication" , locale ) ; throw new RepositoryException ( bundle . getString ( "Connection.failed" ) + " '" + dataSource . getPath ( ) + "'" , e ) ; } } else { // wait just the number of seconds configured (deterministic) Connection connection ; FutureTask < Connection > createConnectionTask = null ; try { createConnectionTask = new FutureTask < Connection > ( new Callable < Connection > ( ) { public Connection call ( ) throws Exception { if ( driver . equals ( CSVDialect . DRIVER_CLASS ) ) { return DriverManager . getConnection ( url , convertListToProperties ( dataSource . getProperties ( ) ) ) ; } else { return DriverManager . getConnection ( url , username , password ) ; } } } ) ; new Thread ( createConnectionTask ) . start ( ) ; connection = createConnectionTask . get ( connectionTimeout , TimeUnit . SECONDS ) ; } catch ( Exception e ) { Locale locale = LanguageManager . getInstance ( ) . getLocale ( storageService . getSettings ( ) . getLanguage ( ) ) ; ResourceBundle bundle = ResourceBundle . getBundle ( "ro.nextreports.server.web.NextServerApplication" , locale ) ; throw new RepositoryException ( bundle . getString ( "Connection.failed" ) + " '" + dataSource . getPath ( ) + "'" , e ) ; } return connection ; } }
this is useful if we want to test the connection