Check if transaction in hibernate successful

hibernate, java, transactions, try-catch

Solution

session.getTransaction().wasCommitted(); does return false even if the transaction was committed. Please take a look at

wasCommitted returns false

what worked for me is

// close database connection  
public boolean closeDBConnection() {  
    boolean successful = false;  
    try {  
        session.getTransaction().commit();  
        successful = true;  
    } catch (HibernateException r) {  
    //log exception here  
    } finally {  
        session.close();  
        session = null;  
    }  
    return successful;  
}  

Problem

I am developing an application using hibernate and I save `Entities` as usual inside `hibernate` transactions. I want to 'get Feedback' from a transaction if it has completed sucessfully or not and according to that to excecute next code. Here is the trivial method I use to update the entity: ``` public boolean updateDepartment(Department s) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = HibernateUtil.getTransaction(session); boolean success = false; try { tx.begin(); session.update(s); tx.commit(); success = true; } catch (Exception e) { tx.rollback(); e.printStackTrace(); success = false; } return success; } ``` Invocation of the method from other code: ``` boolean b = dao.updateDepartment(d); if(b) { doStuff(); } else { showMessage("Save not usccessful. Try again"); } ``` My question is whether this approach with the `boolean` variable is the optimal way, or could it be carried out in a better way. If my approach is OK, would it be better if the `return` statement would be surrounded with `finally`?

Original source