You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Jens Mayer <je...@gmx.de> on 2007/04/10 22:23:28 UTC
Testing the connection
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
Re: Testing the connection
Posted by Mike Kienenberger <mk...@gmail.com>.
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 <je...@gmx.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
>