-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Determine if db-connection fails
PostPosted: Tue Mar 30, 2010 4:07 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 4:06 am
Posts: 21
Hi,

i'd like do know if the db connection could be established when my program starts up. I thought I can just handle the exception in the class "HibernateUtil" while creating the session factory but unfortunately it doesn't work. Hibernate throws a nested exception and i can't catch it.
There must be a better way to check the connection-state than performing a sql-statement right? May anybody give me a piece of advise? Thanks a lot.

Jonny


Top
 Profile  
 
 Post subject: Re: Determine if db-connection fails
PostPosted: Wed Mar 31, 2010 9:16 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Once you have builded the sessionfactory you can test the connection to the database in following way:

Code:
try {
    java.sql.Connection conn =
   ((org.hibernate.impl.SessionFactoryImpl) sessionfactory).getConnectionProvider().getConnection();
    conn.close();  // connection was sucessfull, closing it again (frees resources !)
}
catch Exception (x) {
    sys.err ("Connection to database could not be established: " + x.toString());
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.