-->
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: JPA / Hibernate handle error in createEntityManagerFactory a
PostPosted: Tue Feb 17, 2009 11:53 am 
Newbie

Joined: Tue Feb 17, 2009 11:50 am
Posts: 2
Hi,
I wonder how I can capture a possible exception using JPA, when creating a new instance with EntityManagerFactory? I'm working with Hibernate 3.3.1 and Entitymanager 3.4.0.

For example, if the database is not reachable or so, I would take exception, however I have managed to do so. Reaching the Throwable also not working.

Here is my code.

Code:

- com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@f1f051 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect
   at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
   at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
   at com.mysql.jdbc.Connection.createNewIO(Connection.java:2592)
   at com.mysql.jdbc.Connection.<init>(Connection.java:1509)
   at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
   at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)
   at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
   at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
   at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledCo



Top
 Profile  
 
 Post subject: Re: JPA / Hibernate handle error in createEntityManagerFactory a
PostPosted: Thu Nov 24, 2011 2:08 pm 
Newbie

Joined: Thu Nov 24, 2011 1:58 pm
Posts: 1
I know the topic is old, but I searched the Internet and found no solution to this.
My workaround is this:
Code:
    private static EntityManagerFactory emf;

    public static void createEntityManagerFactory() throws Exception {
        Thread criarBanco = new Thread() {
            @Override           
            public void run() {
                emf = Persistence.createEntityManagerFactory("PU");
            }
        };
        criarBanco.start();
        criarBanco.join();
        if (emf == null) throw new Exception("Error while connecting to database.");
    }

    public static EntityManagerFactory getEmf() {
        return emf;
    }

Obs: I'm not a experienced programmer in Java/JPA/Hibernate, and this is probably not the best way to do so.


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.