java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 196 76da38d49e81
parent 183 1bb5abfb0655
child 197 7a2f535017e4
equal deleted inserted replaced
195:aed11d9107bf 196:76da38d49e81
    33 import java.io.ByteArrayOutputStream;
    33 import java.io.ByteArrayOutputStream;
    34 import java.io.InputStreamReader;
    34 import java.io.InputStreamReader;
    35 import java.io.PrintStream;
    35 import java.io.PrintStream;
    36 import java.sql.Array;
    36 import java.sql.Array;
    37 import java.sql.Driver;
    37 import java.sql.Driver;
       
    38 import java.sql.DriverManager;
    38 import java.sql.DriverPropertyInfo;
    39 import java.sql.DriverPropertyInfo;
    39 import java.sql.SQLException;
    40 import java.sql.SQLException;
    40 import java.util.ArrayList;
    41 import java.util.ArrayList;
    41 import java.util.EnumSet;
    42 import java.util.EnumSet;
    42 import java.util.HashSet;
    43 import java.util.HashSet;
    45 import java.util.Set;
    46 import java.util.Set;
    46 import java.util.concurrent.ExecutorService;
    47 import java.util.concurrent.ExecutorService;
    47 import java.util.concurrent.Executors;
    48 import java.util.concurrent.Executors;
    48 import java.util.concurrent.TimeUnit;
    49 import java.util.concurrent.TimeUnit;
    49 import java.util.logging.Level;
    50 import java.util.logging.Level;
       
    51 import java.util.logging.LogRecord;
    50 import java.util.logging.Logger;
    52 import java.util.logging.Logger;
    51 import javax.sql.rowset.RowSetMetaDataImpl;
    53 import javax.sql.rowset.RowSetMetaDataImpl;
    52 
    54 
    53 /**
    55 /**
    54  * Displays info like help, version etc.
    56  * Displays info like help, version etc.
   304 		final Formatter currentFormatter = formatter;
   306 		final Formatter currentFormatter = formatter;
   305 
   307 
   306 		printHeader(currentFormatter, header, "-- database configuration and connectivity test", null);
   308 		printHeader(currentFormatter, header, "-- database configuration and connectivity test", null);
   307 
   309 
   308 		for (final String dbName : options.getDatabaseNamesToTest()) {
   310 		for (final String dbName : options.getDatabaseNamesToTest()) {
       
   311 			preloadDriver(dbName);
       
   312 		}
       
   313 
       
   314 		for (final String dbName : options.getDatabaseNamesToTest()) {
   309 			es.submit(new Runnable() {
   315 			es.submit(new Runnable() {
   310 				// TODO: Java 8 – lambda
   316 				// TODO: Java 8 – lambda
   311 				@Override
   317 				@Override
   312 				public void run() {
   318 				public void run() {
   313 					final Object[] row = testConnection(dbName);
   319 					final Object[] row = testConnection(dbName);
   333 		}
   339 		}
   334 
   340 
   335 		printFooter(currentFormatter);
   341 		printFooter(currentFormatter);
   336 	}
   342 	}
   337 
   343 
       
   344 	/**
       
   345 	 * JDBC driver classes should be preloaded in single thread to avoid deadlocks while doing
       
   346 	 * {@linkplain DriverManager#registerDriver(java.sql.Driver)} during parallel connections.
       
   347 	 *
       
   348 	 * @param dbName
       
   349 	 */
       
   350 	private void preloadDriver(String dbName) {
       
   351 		try {
       
   352 			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
       
   353 			Driver driver = findDriver(dd);
       
   354 			if (driver == null) {
       
   355 				log.log(Level.WARNING, "No Driver found for DB: {0}", dbName);
       
   356 			} else {
       
   357 				log.log(Level.FINEST, "Driver preloading for DB: {0} was successfull", dbName);
       
   358 			}
       
   359 		} catch (Exception e) {
       
   360 			LogRecord r = new LogRecord(Level.WARNING, "Failed to preload the Driver for DB: {0}");
       
   361 			r.setParameters(new Object[]{dbName});
       
   362 			r.setThrown(e);
       
   363 			log.log(r);
       
   364 		}
       
   365 	}
       
   366 
   338 	public Object[] testConnection(String dbName) {
   367 	public Object[] testConnection(String dbName) {
   339 		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
   368 		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
   340 
   369 
   341 		boolean succesfullyConnected = false;
   370 		boolean succesfullyConnected = false;
   342 		boolean succesfullyConfigured = false;
   371 		boolean succesfullyConfigured = false;
   347 			succesfullyConfigured = true;
   376 			succesfullyConfigured = true;
   348 			try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
   377 			try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
   349 				succesfullyConnected = dc.test();
   378 				succesfullyConnected = dc.test();
   350 			}
   379 			}
   351 			log.log(Level.FINE, "Database connection test was successful");
   380 			log.log(Level.FINE, "Database connection test was successful");
   352 		} catch (ConfigurationException | SQLException e) {
   381 		} catch (ConfigurationException | SQLException | RuntimeException e) {
   353 			log.log(Level.SEVERE, "Error during testing connection " + dbName, e);
   382 			log.log(Level.SEVERE, "Error during testing connection " + dbName, e);
   354 		}
   383 		}
   355 
   384 
   356 		return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
   385 		return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
   357 	}
   386 	}