diff -r 50a79c553f80 db/apichanges.xml --- a/db/apichanges.xml Wed May 06 16:31:28 2009 +0200 +++ b/db/apichanges.xml Wed May 06 16:35:42 2009 +0200 @@ -107,6 +107,21 @@ + Add a DatabaseConnection.getJDBCDriver() method + + + + + + Currently there is no easy way to get the JDBCDriver instance that a + DatabaseConnection will use / used to connect to a database. A + DatabaseConnection.getJDBCDriver() method should be added. + + + + + + Add ability to ensure a JDBC connection is valid diff -r 50a79c553f80 db/nbproject/project.properties --- a/db/nbproject/project.properties Wed May 06 16:31:28 2009 +0200 +++ b/db/nbproject/project.properties Wed May 06 16:35:42 2009 +0200 @@ -42,7 +42,7 @@ javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=1.31.0 +spec.version.base=1.32.0 extra.module.files=modules/ext/ddl.jar diff -r 50a79c553f80 db/src/org/netbeans/api/db/explorer/DatabaseConnection.java --- a/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java Wed May 06 16:31:28 2009 +0200 +++ b/db/src/org/netbeans/api/db/explorer/DatabaseConnection.java Wed May 06 16:35:42 2009 +0200 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common @@ -24,7 +24,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -146,6 +146,16 @@ } /** + * Returns the JDBC driver instance that this connection uses. + * + * @since 1.32 + * @return the JDBC driver or null if no driver registred + */ + public JDBCDriver getJDBCDriver() { + return delegate.findJDBCDriver (); + } + + /** * Returns this connection's database URL. * * @return the connection's database URL @@ -275,6 +285,7 @@ /** * Returns a string representation of the database connection. */ + @Override public String toString() { return "DatabaseConnection[name='" + getName() + "']"; // NOI18N } diff -r 50a79c553f80 db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java --- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java Wed May 06 16:31:28 2009 +0200 +++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java Wed May 06 16:35:42 2009 +0200 @@ -69,6 +69,7 @@ import org.netbeans.lib.ddl.DDLException; import org.netbeans.api.db.explorer.DatabaseException; import org.netbeans.api.db.explorer.JDBCDriver; +import org.netbeans.api.db.explorer.JDBCDriverListener; import org.netbeans.api.db.explorer.JDBCDriverManager; import org.netbeans.modules.db.ExceptionListener; @@ -169,6 +170,13 @@ public static final String DRIVER_CLASS_NET = "org.apache.derby.jdbc.ClientDriver"; // NOI18N public static final int DERBY_UNICODE_ERROR_CODE = 20000; private OpenConnectionInterface openConnection = null; + private JDBCDriver jdbcdrv = null; + private JDBCDriverListener jdbcL = new JDBCDriverListener () { + public void driversChanged () { + jdbcdrv = null; + } + }; + static private final Lookup.Result openConnectionLookupResult; static private Collection openConnectionServices = null; @@ -187,6 +195,7 @@ public DatabaseConnection() { dbconn = DatabaseConnectionAccessor.DEFAULT.createDatabaseConnection(this); propertySupport = new PropertyChangeSupport(this); + JDBCDriverManager.getDefault().addDriverListener (jdbcL); } /** Advanced constructor @@ -220,19 +229,22 @@ } public JDBCDriver findJDBCDriver() { - JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers(drv); - if (drvs.length <= 0) { - return null; + if (jdbcdrv == null) { + JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers(drv); + if (drvs.length <= 0) { + return null; + } + + JDBCDriver useDriver = drvs[0]; + for (int i = 0; i < drvs.length; i++) { + if (drvs[i].getName().equals(getDriverName())) { + useDriver = drvs[i]; + break; + } + } + return useDriver; } - - JDBCDriver useDriver = drvs[0]; - for (int i = 0; i < drvs.length; i++) { - if (drvs[i].getName().equals(getDriverName())) { - useDriver = drvs[i]; - break; - } - } - return useDriver; + return jdbcdrv; } public Connection getJDBCConnection(boolean test) { diff -r 50a79c553f80 db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java --- a/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java Wed May 06 16:31:28 2009 +0200 +++ b/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java Wed May 06 16:35:42 2009 +0200 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common @@ -24,7 +24,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -43,10 +43,8 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.logging.Level; import org.netbeans.modules.db.test.Util; import org.netbeans.modules.db.test.DBTestBase; -import org.openide.util.NbBundle; /** * @@ -72,14 +70,41 @@ DatabaseConnection dbconn = DatabaseConnection.create(driver, "database", "user", "schema", "password", true); ConnectionManager.getDefault().addConnection(dbconn); - + assertTrue(ConnectionManager.getDefault().getConnections().length > 0); - + Util.clearConnections(); - + assertTrue(ConnectionManager.getDefault().getConnections().length == 0); } + public void testGetJDDCDriver() throws Exception{ + Util.clearConnections(); + Util.deleteDriverFiles(); + + JDBCDriver driver = Util.createDummyDriver(); + assertEquals(1, JDBCDriverManager.getDefault().getDrivers().length); + + DatabaseConnection dbconn = DatabaseConnection.create(driver, "database", "user", "schema", "password", true); + assertEquals ("Returns the correct driver", driver, dbconn.getJDBCDriver ()); + } + + public void testGetJDDCDriverWhenAddOtherDriver() throws Exception{ + Util.clearConnections(); + Util.deleteDriverFiles(); + + JDBCDriver driver1 = Util.createDummyDriver(); + assertEquals(1, JDBCDriverManager.getDefault().getDrivers().length); + DatabaseConnection dbconn1 = DatabaseConnection.create(driver1, "database", "user", "schema", "password", true); + assertEquals ("Returns the correct driver", driver1, dbconn1.getJDBCDriver ()); + + JDBCDriver driver2 = Util.createDummyDriverWithOtherJar (); + assertEquals(2, JDBCDriverManager.getDefault().getDrivers().length); + DatabaseConnection dbconn2 = DatabaseConnection.create(driver2, "database", "user", "schema", "password", true); + assertEquals ("Returns the correct driver", driver1, dbconn1.getJDBCDriver ()); + assertEquals ("Returns the correct driver", driver2, dbconn2.getJDBCDriver ()); + } + public void testSameDatabaseConnectionReturned() throws Exception { Util.clearConnections(); Util.deleteDriverFiles(); diff -r 50a79c553f80 db/test/unit/src/org/netbeans/modules/db/test/Util.java --- a/db/test/unit/src/org/netbeans/modules/db/test/Util.java Wed May 06 16:31:28 2009 +0200 +++ b/db/test/unit/src/org/netbeans/modules/db/test/Util.java Wed May 06 16:35:42 2009 +0200 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common @@ -24,7 +24,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -82,16 +82,24 @@ } public static JDBCDriver createDummyDriver() throws Exception { - JDBCDriver[] drivers = + JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers("org.bar.barDriver"); if ( drivers.length > 0 ) { return drivers[0]; } - - JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", + + JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", "org.bar.BarDriver", new URL[]{ new URL("file://foo/path/foo.jar")}); JDBCDriverManager.getDefault().addDriver(driver); - + + return driver; + } + + public static JDBCDriver createDummyDriverWithOtherJar() throws Exception { + JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", + "org.bar.BarDriver2", new URL[]{ new URL("file://foo2/path/foo2.jar")}); + JDBCDriverManager.getDefault().addDriver(driver); + return driver; } }