-->
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: Bad behavior if DB server is not running
PostPosted: Fri Aug 29, 2008 9:34 pm 
Newbie

Joined: Fri Aug 29, 2008 9:17 pm
Posts: 1
I'm new to Hibernate, using it as an implementation of JPA.

I'm puzzled by something basic. I establish the connection to the DB (MySQL) using the two lines:

EntityManagerFactory emf= Persistence.createEntityManagerFactory("MyPU");
EntityManager em = emf.createEntityManager();

Now, this works fine if the DB server is running. However, if it isn't, Hibernate doesn't seem to handle the situation gracefully. The application exits with an exception trace, written to stderr (not logged):

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:41)
at com.magicballbingo.yboard.Main.initializePersistence(Main.java:227)
at com.magicballbingo.yboard.Main.main(Main.java:63)
Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:426)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)
... 2 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

...



Shouldn't Hibernate be throwing an exception here? I find this very strange. Why is Hibernate writing to stderr, and not logging this? Why does it exit?

Here's what I'd like to do. At application startup, I'd like to test for a connection to the DB server. I'd like to use that connection conn, if I can get it, to query the DB for its configuration, using something like
metaData = conn.getMetaData();

If things aren't right, I'll shut down in an orderly fashion.

Can someone tell me how to do this with Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2008 4:19 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
the exception trace you're seeing on stderr IS the exception thrown by hibernate, you should handle that.

Quote:
Why is Hibernate writing to stderr, and not logging this? Why does it exit?

It's your application who is shutting down because you didn't manage the exception and also you didn't log it...

_________________
Sanne
http://in.relation.to/


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.