-->
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.  [ 3 posts ] 
Author Message
 Post subject: Catch an Exception when calling createEntityManagerFactory?
PostPosted: Sat Oct 25, 2008 2:18 pm 
Newbie

Joined: Sat Oct 25, 2008 2:09 pm
Posts: 6
Hi,
I am wondering how I can catch a possible exception using Hibernate JPA, when creating a new instance with EntityManagerFactory? I am working with Hibernate 3.3.1 and Entitymanager 3.4.0.

For example if the database is not reachable or so, I would like to catch the exception, however I have not succeeded to do so. Catching the Throwable does not work either.

Here is my code.

Code:
try {
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("my_database", null);
} catch (Throwable t) {
  // I want to go here if something goes wrong...
  System.out.println("** Error: " + t.getMessage());
}



Any hints on how to do this?
Thanks!
Paul


Last edited by paulkuhn on Sun Oct 26, 2008 4:04 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Unable to catch Exception...
PostPosted: Sun Oct 26, 2008 4:03 pm 
Newbie

Joined: Sat Oct 25, 2008 2:09 pm
Posts: 6
Basically I am not able to catch an Exception in case the database is not available and I am calling

Persistence.createEntityManagerFactory(...);

Any ideas how to catch the exception?


This is a StackTrace that is thrown, but the my catch block is not reached:

Code:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
     at java.lang.reflect.Constructor.newInstance(Unknown Source)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
     at com.mysql.jdbc.Util.getInstance(Util.java:381)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2253)
     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
     at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
     at java.lang.reflect.Constructor.newInstance(Unknown Source)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
     at java.sql.DriverManager.getConnection(Unknown Source)
     at java.sql.DriverManager.getConnection(Unknown Source)
     at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
     at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:111)
     at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
     at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
     at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
     at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
     at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
     …


Top
 Profile  
 
 Post subject: Re: Catch an Exception when calling createEntityManagerFactory?
PostPosted: Sun Aug 21, 2011 8:41 am 
Beginner
Beginner

Joined: Sat Mar 27, 2010 11:03 am
Posts: 27
Location: Berlin
Hi,

i know its an old post !

I have the same Problem here ... how can i catch this Exception while connecting to the DB?

thx
moe


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.