Re: Testing the connection

From: Mike Kienenberger (mkienen..mail.com)
Date: Tue Apr 10 2007 - 16:34:19 EDT

  • Next message: Peter Karich: "Re: Temporary Objects"

    What you described is how the Cayenne modeler does it, so it's
    probably the best way we know.

    org.objectstyle.cayenne.modeler.dialog.pref.DataSourcePreferences

        /**
         * Tries to establish a DB connection, reporting the status of
    this operation.
         */
        public void testDataSourceAction() {
            DBConnectionInfo currentDataSource = getConnectionInfo();
            if (currentDataSource == null) {
                return;
            }

            if (currentDataSource.getJdbcDriver() == null) {
                JOptionPane.showMessageDialog(
                        null,
                        "No JDBC Driver specified",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (currentDataSource.getUrl() == null) {
                JOptionPane.showMessageDialog(
                        null,
                        "No Database URL specified",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                Class driverClass =
    getApplication().getClassLoadingService().loadClass(
                        currentDataSource.getJdbcDriver());
                Driver driver = (Driver) driverClass.newInstance();

                // connect via Cayenne DriverDataSource - it addresses
    some driver issues...
                Connection c = new DriverDataSource(
                        driver,
                        currentDataSource.getUrl(),
                        currentDataSource.getUserName(),
                        currentDataSource.getPassword()).getConnection();
                try {
                    c.close();
                }
                catch (SQLException e) {
                    // i guess we can ignore this...
                }

                JOptionPane.showMessageDialog(
                        null,
                        "Connected Successfully",
                        "Success",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            catch (Throwable th) {
                th = Util.unwindException(th);
                JOptionPane.showMessageDialog(null, "Error connecting to DB: "
                        + th.getLocalizedMessage(), "Warning",
    JOptionPane.WARNING_MESSAGE);
                return;
            }
        }

    On 4/10/07, Jens Mayer <jens.mayer..mx.de> wrote:
    > In my application, the user has the ability to switch between databases
    > (for testing purposes). He gets a little dialog, types in
    > user/password/db-name and under the hood, an appropriate
    > DataContext is built.
    >
    > Works fine, but I want to test the correctness of what he typed in
    > BEFORE the next query is performed.
    >
    > So I could do that by getting the new DataSource and invoking
    > getConnection() with an appropriate Exceptionhandling but that seems to
    > be not very smart design.
    >
    > Does anyone have a better idea?
    >
    > Jens
    >



    This archive was generated by hypermail 2.0.0 : Tue Apr 10 2007 - 16:34:58 EDT