Verify Sleep Transaction Success

I am developing an application that uses hibernation and save Entitiesas usual inside transactions hibernate. I want to “get feedback” from a transaction if it completed successfully or not, and accordingly to exclude the following code. Here is the trivial method that I use to update an object:

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;
    }

Calling a method from another code:

boolean b = dao.updateDepartment(d);
if(b)
{
 doStuff();
}
else
{
 showMessage("Save not usccessful. Try again");
}

My question is whether this approach with a variable is the booleanbest way or whether it can be done better. If my approach is ok, would it be better if the operator returnwere surrounded finally?

+4
source share
2
.

session.getTransaction() wasCommitted(); false, . ,

wasCommitted false

,

// 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;  
}  
+4

, session.getTransaction().wasCommitted();. tx , TRUE.

-1

Source: https://habr.com/ru/post/1537659/


All Articles