Transaction - bobocode-breskul/bibernate GitHub Wiki
Transaction
Transactions are essential for managing data consistency and integrity in enterprise applications.
Transaction has next statuses
- NOT_ACTIVE - The transaction has not yet been started.
- ACTIVE - The transaction has been started, but not yet completed.
- COMMITTED - The transaction has been completed successfully.
- ROLLED_BACK - The transaction has been rolled back.
- FAILED_COMMIT - The transaction attempted to commit, but failed.
- FAILED_ROLLBACK - The transaction attempted to rollback, but failed.
Transaction has next methods
- Start a resource transaction
public void begin()
- Commit the current transaction
public void commit()
- Roll back the current resource transaction
public void rollback()
- Return current transaction status
public TransactionStatus getStatus()
Using transaction in Bibernate is super easy
public void saveEntity(Person person) {
Transaction transaction = session.getTransaction();
try {
transaction.begin();
session.persist(expectedPerson);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}