Hibernate Transaction Management Example
A transaction basically represents to a unit of work. In such case, on the off chance that one stage comes up short, the entire transaction fizzles (which is named as atomicity). An transaction can be portrayed by ACID properties (Atomicity, Consistency, Isolation and Durability).
Transaction Interface in Hibernate
In hibernate framework, we have Transaction interface that characterizes the unit of work. It keeps up deliberation from the transaction usage (JTA,JDBC).
A transaction is related with Session and instantiated by calling session.beginTransaction().
The techniques for Transaction interface are as follows:
- void begin() begins another transaction.
- void commit() closes the unit of work except if we are in FlushMode.NEVER.
- void rollback() powers this transaction to rollback.
- void setTimeout(int seconds) it sets an transaction break for any transaction begun by an ensuing call to start on this instance.
- boolean isAlive() checks if the transaction is still alive.
- void registerSynchronization(Synchronization s) registers a client synchronization callback for this transaction.
- boolean wasCommited() checks if the transaction is commited successfully.
- boolean wasRolledBack() checks if the transaction is rolledback successfully.
Example of Transaction Management in Hibernate
In hibernate, it is smarter to rollback the transaction if any special case happens, with the goal that assets can be free. How about we see the case of transaction the board in hibernate.