Transaction - bobocode-breskul/bibernate GitHub Wiki

Transaction

Transactions are essential for managing data consistency and integrity in enterprise applications.

Transaction has next statuses

  1. NOT_ACTIVE - The transaction has not yet been started.
  2. ACTIVE - The transaction has been started, but not yet completed.
  3. COMMITTED - The transaction has been completed successfully.
  4. ROLLED_BACK - The transaction has been rolled back.
  5. FAILED_COMMIT - The transaction attempted to commit, but failed.
  6. FAILED_ROLLBACK - The transaction attempted to rollback, but failed.

Transaction has next methods

  1. Start a resource transaction
public void begin()
  1. Commit the current transaction
public void commit()
  1. Roll back the current resource transaction
public void rollback()
  1. 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();
  }
}