Hi Sebastian, With SQL Server I use something like follows:
<jdbc-connection-descriptor ... > <connection-pool maxActive="10" testOnBorrow="true" testOnReturn="false" testWhileIdle="true" timeBetweenEvictionRunsMillis="10000" minEvictableIdleTimeMillis="20000" numTestsPerEvictionRun="10" validationQuery="SELECT 1" /> <sequence-manager> ... </jdbc-connection-descriptor> The key is to specify a fast, simple query so as to not suffer a great performance penalty. Wally -----Original Message----- From: news [mailto:[EMAIL PROTECTED] On Behalf Of Sebastian Sent: Monday, May 03, 2004 11:05 AM To: [EMAIL PROTECTED] Subject: Re: How to test db connection on application startup How would a generic ValidationQuery look then, that simply reviews, whether with a certain DB instance can be established communication? Gelhar, Wallace Joseph wrote: > 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.newCon > ne > ctionFromDriverManager(Unknown > >>Source) >> at >> > > org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl$ConPoolF > ac > tory.makeObject(Unknown > >>Source) >> at >> > > org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObj > ec > tPool.java:816) > >> at >> > > org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl.getConne > ct > ionFromPool(Unknown > >>Source) >> at >> > > org.apache.ojb.broker.accesslayer.ConnectionFactoryAbstractImpl.lookup > Co > nnection(Unknown > >>Source) >> at >> > > org.apache.ojb.broker.accesslayer.ConnectionManagerImpl.getConnection( > Un > known > >>Source) >> at >> > > org.apache.ojb.broker.accesslayer.ConnectionManagerImpl.localBegin(Unk > no > wn > >>Source) >> at >> > > org.apache.ojb.broker.core.PersistenceBrokerImpl.beginTransaction(Unkn > ow > n > >>Source) >> at >> > > org.apache.ojb.broker.core.DelegatingPersistenceBroker.beginTransactio > n( > Unknown > >>Source) >> at >> > > org.apache.ojb.broker.core.DelegatingPersistenceBroker.beginTransactio > n( > 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]