开发者

Problem with hibernate and exceptions

On server's side I use hibernate to communicate with database. However, if the database is offline I can't catch any exception related with the connection. What's more, on client's site where gwt is used, onFailure(Throwable caught) function is executed, but caught.printStackTrace(); prints nothing. I want gwt to inform a user that the database is currently unavailable, but as you can read I can't catch any exception which can help me to do that. Any ideas?

update:

e.g.

public class HibernateUtil
{
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactor开发者_StackOverflowy creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

...

Session session = null;
try
{
   session = HibernateUtil.getSessionFactory().openSession();
} catch (Throwable ex) {
   System.err.println("Initial SessionFactory creation failed." + ex);
}

In both cases no exception is catched if the database is offline, so I'm not able to send a message about the problem with connection to the client

Thanks in advance


You can catch the Exception server-side, and then pass it down to the callback in your DTO as a successful request. Add an "isError" flag to the DTO, and in the onSuccess() method in the client check the flag and process accordingly. In fact, you can call onFailure() from on Success() with the downloaded Exception. You may have to make the Exception serializable.


You have to add "throws Exception" for your ServiceAsync -interfaces and be sure that the exceptions can be serializable. I recommend catching the db-exceptions on the serverside and then throw your own exceptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜