0

I have the following SQL transaction in java

public List<MyObj> find() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        List<MyObj> obj = null;
        try {
            tx = session.beginTransaction();
            String sql = " some sql query";
            SQLQuery query = session.createSQLQuery(sql);
            query.addEntity(MyObj.class);
            objs = query.list();
            tx.commit();
        }
        catch(Exception e) {
            if (tx != null) tx.rollback();

        }
        finally {
            session.close();
        }
        return objs;

    }

Should I be catching an SQLException instead of Exception? When I change the class to be SQL Exception I get an error that I should be adding a throws clause and then removing the catch clause.

Can I add a throws declaration and still catch and handle the SQLException? in this case rollback?

user_mda
  • 18,148
  • 27
  • 82
  • 145
  • What is the exact error from compiler? – Vasu Mar 27 '17 at 17:24
  • Unreachable catch block for SQLException. This exception is never thrown from the try statement body – user_mda Mar 27 '17 at 17:50
  • Hibernate wraps exceptions in a HibernateException, so you can just catch that. RuntimeException will work too but then you are catching more than just a HibernateException. Better to be specific. – Joe.b Mar 27 '17 at 18:13
  • `catch(Exception ...)` is an antipattern, as is `throws Exception`. Always deal in more specific exceptions. – Lew Bloch Mar 27 '17 at 19:32

1 Answers1

0

Hibernate API does not throw the SQLException (which is a checked Exception), so you can't catch it.

The good practice is to catch RuntimeException (to handle exceptions occurred because of code issues like null references, etc... or exceptions thrown by Hibernate API) and rollback the transaction, you can look Hibernate docs here for examples on transaction rollback.

try {
        tx = session.beginTransaction();
        String sql = " some sql query";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(MyObj.class);
        objs = query.list();
        tx.commit();
    }
    catch(RuntimeException e) {
        if (tx != null) tx.rollback();
         throw e;
    }
    finally {
        session.close();
    }

I think what the error means is that I should first declare a throws clause indicating that the block of code will throw an SQLEXception correct?

No, because Hibernate already wraps the checked SQLException into one of the unchecked Exception objects (list given below), you don't need to either catch that Exception or need throws clause on method signature.

I strongly suggest you read this link on why Hibernate or Spring frameworks have moved out of checked exceptions (like SQLException, etc..).

Is there a doc that says that SQLexception is not thrown by the hibernate API?

You can look here for the list of Hibernate exceptions.

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • I think what the error means is that I should first declare a throws clause indicating that the block of code will throw an SQLEXception correct? Is there a doc that says that SQLexception is not thrown by the hibernate API ? – user_mda Mar 27 '17 at 18:02
  • Updated the answer, have a look – Vasu Mar 27 '17 at 18:07
  • @user_mda "Is there a doc that says that SQLexception is not thrown by the hibernate [sic] API ?" Yes, the Hibernate Javadocs. Have you looked into them? – Lew Bloch Mar 27 '17 at 19:34