diff --git a/db/apichanges.xml b/db/apichanges.xml --- a/db/apichanges.xml +++ b/db/apichanges.xml @@ -105,6 +105,20 @@ + + + Add ability to test a connection for validity + + + + + + This change allows you to make sure a DatabaseConnection is valid + before you use it to issue commands to the database. + + + + Add ability to get the underlying JDBC Driver instance for a JDBCDriver diff --git a/db/arch.xml b/db/arch.xml --- a/db/arch.xml +++ b/db/arch.xml @@ -339,6 +339,14 @@ method.

+ +

+ You may want to test to make sure a connection is open and valid before you + use it to issue a command to the database. This can be achieved using the + DatabaseConnection.test() + method +

+

A component which provides database functionality (such as the SQL Editor diff --git a/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java b/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java --- a/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java +++ b/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java @@ -226,6 +226,15 @@ } /** + * Test whether the connection is still valid + * + * @return true if the connection is valid, false otherwise + */ + public boolean test() { + return delegate.test(); + } + + /** * Returns a string representation of the database connection. */ public String toString() { diff --git a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java --- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java +++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java @@ -212,6 +212,22 @@ } } return useDriver; + } + + public boolean test() { + try { + Connection conn = getJDBCConnection(); + if (conn == null || conn.isClosed()) { + return false; + } + + // Send a command to the server, if it fails we know the connection is invalid. + conn.getMetaData().getTables(null, null, " ", new String[] { "TABLE" }).close(); + } catch (SQLException e) { + return false; + } + return true; + } private Collection getOpenConnections() { diff --git a/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java b/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java --- a/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java +++ b/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java @@ -117,5 +117,15 @@ ConnectionManager.getDefault().removeConnection(dbconn); assertEquals(0, ConnectionManager.getDefault().getConnections().length); - } + } + + public void testTestConnection() throws Exception { + DatabaseConnection dbconn = getDatabaseConnection(); + ConnectionManager.getDefault().disconnect(dbconn); + assertFalse(dbconn.test()); + + ConnectionManager.getDefault().connect(dbconn); + + assertTrue(dbconn.test()); + } } diff --git a/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java b/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java --- a/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java +++ b/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java @@ -89,8 +89,8 @@ private static int unquotedCaseRule = RULE_UNDEFINED; private static int quotedCaseRule = RULE_UNDEFINED; - private static JDBCDriver jdbcDriver; - private static DatabaseConnection dbConnection; + private JDBCDriver jdbcDriver; + private DatabaseConnection dbConnection; protected Connection conn; @@ -98,7 +98,7 @@ super(name); } - protected static JDBCDriver getJDBCDriver() throws Exception{ + protected JDBCDriver getJDBCDriver() throws Exception{ if (jdbcDriver == null) { jdbcDriver = JDBCDriver.create("derbydriver", "derbydriver", driverClass, new URL[] {driverJarUrl}); assertNotNull(jdbcDriver); @@ -113,7 +113,7 @@ * Get the DatabaseConnection for the configured Java DB database. This * method will create and register the connection the first time it is called */ - protected static DatabaseConnection getDatabaseConnection() throws Exception { + protected DatabaseConnection getDatabaseConnection() throws Exception { if (dbConnection == null) { JDBCDriver driver = getJDBCDriver();