Hi Sebastian,

I think it would be wiser to specify a validation query in the database
repository descriptor to check if the connection is valid *each* time it
is used.  This will deal with not only the database being unavailable at
startup, but throughout the operation of the application.

BTW, I suspect that the service method did nothing because your code did
nothing that required opening a connection (i.e. opening a broker does
not open a connection until it is needed).  Also note that Brokers are
pooled separately from Connections.

Wally

-----Original Message-----
From: news [mailto:[EMAIL PROTECTED] On Behalf Of Sebastian
Sent: Monday, May 03, 2004 10:31 AM
To: [EMAIL PROTECTED]
Subject: Re: How to test db connection on application startup


I changed the code to obtain the connection from the 
serviceConnectionManager, but the problem remains, I can't catch the 
thrown java.sql.SQLException. Any ideas?

try
{
     pb = PersistenceBrokerFactory.defaultPersistenceBroker();
     pb.serviceConnectionManager().getConnection();
}
catch (Exception e)
{
     throw new DatabaseOpenException(e);
}

Sebastian wrote:

> Hi,
> 
> when launching my ojb app I'd like to test if the db connection is
> available. I thought I can do something like this:
> 
> try
> {
>     pb = PersistenceBrokerFactory.defaultPersistenceBroker();
>     pb.beginTransaction();
>     pb.commitTransaction();
> }
> catch (Exception e)
> {
>     throw new DatabaseOpenException(e);
> }
> 
> When launching the app with turned off db I get exception stacktrace
> appended to the end of this post, but the SQLException is actually not

> catched by my catch block. I guess that is because opening the db 
> connection is done in a separate thread?
> 
> My question is, what is the best generic way to test the db connection
> when launching an ojb enabled app?
> 
> java.sql.SQLException: Unable to connect to any hosts due to 
> exception:
> java.net.ConnectException: Connection refused: connect
>     at com.mysql.jdbc.Connection.createNewIO(Connection.java:1797)
>     at com.mysql.jdbc.Connection.<init>(Connection.java:562)
>     at 
>
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:36
1)
>     at java.sql.DriverManager.getConnection(Unknown Source)
>     at java.sql.DriverManager.getConnection(Unknown Source)
>     at 
>
org.apache.ojb.broker.accesslayer.ConnectionFactoryAbstractImpl.newConne
ctionFromDriverManager(Unknown 
> Source)
>     at 
>
org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl$ConPoolFac
tory.makeObject(Unknown 
> Source)
>     at 
>
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjec
tPool.java:816) 
> 
>     at
>
org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl.getConnect
ionFromPool(Unknown 
> Source)
>     at 
>
org.apache.ojb.broker.accesslayer.ConnectionFactoryAbstractImpl.lookupCo
nnection(Unknown 
> Source)
>     at 
>
org.apache.ojb.broker.accesslayer.ConnectionManagerImpl.getConnection(Un
known 
> Source)
>     at 
>
org.apache.ojb.broker.accesslayer.ConnectionManagerImpl.localBegin(Unkno
wn 
> Source)
>     at 
>
org.apache.ojb.broker.core.PersistenceBrokerImpl.beginTransaction(Unknow
n 
> Source)
>     at 
>
org.apache.ojb.broker.core.DelegatingPersistenceBroker.beginTransaction(
Unknown 
> Source)
>     at 
>
org.apache.ojb.broker.core.DelegatingPersistenceBroker.beginTransaction(
Unknown 
> Source)
>     at test.DBTest.<init>(DBTest.java:26)


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to