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
?
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.
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.
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 .
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 () .
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With