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?