Skip to content

Transactions

Session is an object that allows you to create and manage transactions. In order to create a transaction, we call the getTransaction method, then, after performing the operation on the database, we can call the commit or rollback method, e.g .:

final SessionFactory sessionFactory = new Configuration()
    .configure("hibernate.cfg.xml")
    .buildSessionFactory();

try (Session session = sessionFactory.openSession()) {
  Transaction transaction = session.beginTransaction();

  // performing database operations

  if (success) {
    transaction.commit();
  } else {
    transaction.rollback();
  }
}