This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 143837
Collapse All | Expand All

(-)a/db/apichanges.xml (+14 lines)
Lines 105-110 Link Here
105
    <!-- ACTUAL CHANGES BEGIN HERE: -->
105
    <!-- ACTUAL CHANGES BEGIN HERE: -->
106
106
107
    <changes>
107
    <changes>
108
        <change>
109
            <api name="database_explorer_api"/>
110
            <summary>Add ability to test a connection for validity</summary>
111
            <version major="1" minor="29"/>
112
            <date day="15" month="8" year="2008"/>
113
            <author login="davidvc"/>
114
            <compatibility addition="yes"/>
115
            <description>
116
                This change allows you to make sure a DatabaseConnection is valid
117
                before you use it to issue commands to the database.
118
            </description>
119
            <class package="org.netbeans.api.db.explorer" name="DatabaseConnection"/>
120
            <issue number="143837"/>
121
        </change>
108
        <change>
122
        <change>
109
            <api name="database_explorer_api"/>
123
            <api name="database_explorer_api"/>
110
            <summary>Add ability to get the underlying JDBC Driver instance for a JDBCDriver</summary>
124
            <summary>Add ability to get the underlying JDBC Driver instance for a JDBCDriver</summary>
(-)a/db/arch.xml (+8 lines)
Lines 339-344 Link Here
339
     method.
339
     method.
340
   </p>
340
   </p>
341
  </usecase>
341
  </usecase>
342
 <usecase id="test-connection" name="Test a database connection for validity">
343
   <p>
344
    You may want to test to make sure a connection is open and valid before you
345
    use it to issue a command to the database.  This can be achieved using the
346
    <a href="@TOP@org/netbeans/api/db/explorer/DatabaseConnection.html#test()">DatabaseConnection.test()</a>
347
    method 
348
   </p>
349
  </usecase>
342
  <usecase id="connections-combo-box" name="Displaying the database connections in the UI">
350
  <usecase id="connections-combo-box" name="Displaying the database connections in the UI">
343
   <p>
351
   <p>
344
    A component which provides database functionality (such as the SQL Editor
352
    A component which provides database functionality (such as the SQL Editor
(-)a/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java (+9 lines)
Lines 226-231 Link Here
226
    }
226
    }
227
227
228
    /**
228
    /**
229
     * Test whether the connection is still valid
230
     *
231
     * @return true if the connection is valid, false otherwise
232
     */
233
    public boolean test() {
234
        return delegate.test();
235
    }
236
237
    /**
229
     * Returns a string representation of the database connection.
238
     * Returns a string representation of the database connection.
230
     */
239
     */
231
    public String toString() {
240
    public String toString() {
(-)a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java (+16 lines)
Lines 212-217 Link Here
212
            }
212
            }
213
        }
213
        }
214
        return useDriver;
214
        return useDriver;
215
    }
216
217
    public boolean test() {
218
        try {
219
            Connection conn = getJDBCConnection();
220
            if (conn == null || conn.isClosed()) {
221
                return false;
222
            }
223
224
            // Send a command to the server, if it fails we know the connection is invalid.
225
            conn.getMetaData().getTables(null, null, " ", new String[] { "TABLE" }).close();
226
        } catch (SQLException e) {
227
            return false;
228
        }
229
        return true;
230
215
    }
231
    }
216
232
217
     private Collection getOpenConnections() {
233
     private Collection getOpenConnections() {
(-)a/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java (-1 / +11 lines)
Lines 117-121 Link Here
117
        
117
        
118
        ConnectionManager.getDefault().removeConnection(dbconn);
118
        ConnectionManager.getDefault().removeConnection(dbconn);
119
        assertEquals(0, ConnectionManager.getDefault().getConnections().length);
119
        assertEquals(0, ConnectionManager.getDefault().getConnections().length);
120
    }    
120
    }
121
122
    public void testTestConnection() throws Exception {
123
        DatabaseConnection dbconn = getDatabaseConnection();
124
        ConnectionManager.getDefault().disconnect(dbconn);
125
        assertFalse(dbconn.test());
126
127
        ConnectionManager.getDefault().connect(dbconn);
128
129
        assertTrue(dbconn.test());
130
    }
121
}
131
}
(-)a/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java (-4 / +4 lines)
Lines 89-96 Link Here
89
    private static int    unquotedCaseRule = RULE_UNDEFINED;
89
    private static int    unquotedCaseRule = RULE_UNDEFINED;
90
    private static int    quotedCaseRule = RULE_UNDEFINED;
90
    private static int    quotedCaseRule = RULE_UNDEFINED;
91
91
92
    private static JDBCDriver jdbcDriver;
92
    private JDBCDriver jdbcDriver;
93
    private static DatabaseConnection dbConnection;
93
    private DatabaseConnection dbConnection;
94
    
94
    
95
    protected Connection conn;
95
    protected Connection conn;
96
96
Lines 98-104 Link Here
98
        super(name);
98
        super(name);
99
    }
99
    }
100
100
101
    protected static JDBCDriver getJDBCDriver() throws Exception{
101
    protected JDBCDriver getJDBCDriver() throws Exception{
102
        if (jdbcDriver == null) {
102
        if (jdbcDriver == null) {
103
            jdbcDriver = JDBCDriver.create("derbydriver", "derbydriver", driverClass, new URL[] {driverJarUrl});
103
            jdbcDriver = JDBCDriver.create("derbydriver", "derbydriver", driverClass, new URL[] {driverJarUrl});
104
            assertNotNull(jdbcDriver);
104
            assertNotNull(jdbcDriver);
Lines 113-119 Link Here
113
     * Get the DatabaseConnection for the configured Java DB database.  This
113
     * Get the DatabaseConnection for the configured Java DB database.  This
114
     * method will create and register the connection the first time it is called
114
     * method will create and register the connection the first time it is called
115
     */
115
     */
116
    protected static DatabaseConnection getDatabaseConnection() throws Exception {
116
    protected DatabaseConnection getDatabaseConnection() throws Exception {
117
        if (dbConnection == null) {
117
        if (dbConnection == null) {
118
            JDBCDriver driver = getJDBCDriver();
118
            JDBCDriver driver = getJDBCDriver();
119
119

Return to bug 143837