Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if transaction in hibernate successful

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?

like image 239
arjacsoh Avatar asked Apr 23 '14 08:04

arjacsoh


People also ask

What is the use of @transactional in hibernate?

The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.

What is session beginTransaction ()?

A transaction is associated with a Session and is usually instantiated by a call to Session. beginTransaction(). A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the notion of a transaction.

How hibernate handle multiple transactions?

Therefore, you can run multiple transactions on the same Hibernate Session, but there's a catch. Once an exception is thrown you can no longer reuse that Session. My advice is to divide-and-conquer. Just split all items, construct a Command object for each of those and send them to an ExecutorService#invokeAll .

What is transaction in hibernate?

In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session.beginTransaction () .

Is it better to rollback the transaction in hibernate?

In hibernate, it is better to rollback the transaction if any exception occurs, so that resources can be free. Let's see the example of transaction management in hibernate.

What is the difference between JTA and JDBC in hibernate?

Note: D atabase connection handling is different between JDBC and JTA transactions. Hibernate gets a connection for every Session and tries to be as lazy as possible. Without JTA, Hibernate would hold on to a particular database connection from the beginning until the end of the transaction.

What is the use of wasrolledback in hibernate?

boolean wasRolledBack () checks if the transaction is rolledback successfully. In hibernate, it is better to rollback the transaction if any exception occurs, so that resources can be free. Let's see the example of transaction management in hibernate.


1 Answers

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;  
}  
like image 58
dukekosy Avatar answered Oct 07 '22 09:10

dukekosy