option --test-connection – tests connection to given database v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 01:24:21 +0100
branchv_0
changeset 65f05be87239ad
parent 64 fcc499518dc7
child 66 6e28893eaada
option --test-connection – tests connection to given database
java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Thu Dec 26 01:05:24 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Thu Dec 26 01:24:21 2013 +0100
     1.3 @@ -120,6 +120,17 @@
     1.4  
     1.5  	}
     1.6  
     1.7 +	/**
     1.8 +	 * Tests if this connection is live.
     1.9 +	 *
    1.10 +	 * @return true if test was successful
    1.11 +	 * @throws SQLException if test fails
    1.12 +	 */
    1.13 +	public boolean test() throws SQLException {
    1.14 +		connection.getAutoCommit();
    1.15 +		return true;
    1.16 +	}
    1.17 +
    1.18  	@Override
    1.19  	public void close() throws SQLException {
    1.20  		connection.close();
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 01:05:24 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 01:24:21 2013 +0100
     2.3 @@ -17,10 +17,13 @@
     2.4   */
     2.5  package info.globalcode.sql.dk;
     2.6  
     2.7 +import info.globalcode.sql.dk.configuration.ConfigurationException;
     2.8  import info.globalcode.sql.dk.configuration.ConfigurationProvider;
     2.9 +import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    2.10  import java.io.BufferedReader;
    2.11  import java.io.InputStreamReader;
    2.12  import java.io.PrintStream;
    2.13 +import java.sql.SQLException;
    2.14  import java.util.EnumSet;
    2.15  import java.util.logging.Level;
    2.16  import java.util.logging.Logger;
    2.17 @@ -67,7 +70,22 @@
    2.18  					println("TODO: list databases");
    2.19  					break;
    2.20  				case CONNECTION:
    2.21 -					println("TODO: test database connection: " + options.getDatabaseNameToTest());
    2.22 +					boolean connectionTestResult = false;
    2.23 +					String dbName = options.getDatabaseNameToTest();
    2.24 +					log.log(Level.FINE, "Testing connection to database: {0}", dbName);
    2.25 +					try {
    2.26 +						DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
    2.27 +						if (dd == null) {
    2.28 +							log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
    2.29 +						} else {
    2.30 +							log.log(Level.FINE, "Database definition was loaded from configuration");
    2.31 +							DatabaseConnection dc = dd.connect();
    2.32 +							connectionTestResult = dc.test();
    2.33 +						}
    2.34 +					} catch (ConfigurationException | SQLException e) {
    2.35 +						log.log(Level.SEVERE, "Error during testing connection", e);
    2.36 +					}
    2.37 +					log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
    2.38  					break;
    2.39  				default:
    2.40  					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);