--- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java Thu Dec 26 01:05:24 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java Thu Dec 26 01:24:21 2013 +0100
@@ -120,6 +120,17 @@
}
+ /**
+ * Tests if this connection is live.
+ *
+ * @return true if test was successful
+ * @throws SQLException if test fails
+ */
+ public boolean test() throws SQLException {
+ connection.getAutoCommit();
+ return true;
+ }
+
@Override
public void close() throws SQLException {
connection.close();
--- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Thu Dec 26 01:05:24 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Thu Dec 26 01:24:21 2013 +0100
@@ -17,10 +17,13 @@
*/
package info.globalcode.sql.dk;
+import info.globalcode.sql.dk.configuration.ConfigurationException;
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
+import info.globalcode.sql.dk.configuration.DatabaseDefinition;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
+import java.sql.SQLException;
import java.util.EnumSet;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -67,7 +70,22 @@
println("TODO: list databases");
break;
case CONNECTION:
- println("TODO: test database connection: " + options.getDatabaseNameToTest());
+ boolean connectionTestResult = false;
+ String dbName = options.getDatabaseNameToTest();
+ log.log(Level.FINE, "Testing connection to database: {0}", dbName);
+ try {
+ DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
+ if (dd == null) {
+ log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
+ } else {
+ log.log(Level.FINE, "Database definition was loaded from configuration");
+ DatabaseConnection dc = dd.connect();
+ connectionTestResult = dc.test();
+ }
+ } catch (ConfigurationException | SQLException e) {
+ log.log(Level.SEVERE, "Error during testing connection", e);
+ }
+ log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
break;
default:
throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);