java.lang.Object
io.github.blyznytsiaorg.bibernate.transaction.Transaction

public class Transaction extends Object
Enables the application to delineate units of work while abstracting away the specifics of the underlying transaction implementation. A representation of a database transaction that manages a connection, tracks updated entities, and supports transactional operations such as starting, committing, and rolling back.

Instances of this class are typically used to group a set of database operations into a single atomic unit.

A transaction is linked with a BibernateSession and is typically initiated through a call to bibernateSession.startTransaction(). The design anticipates having, at most, one uncommitted Transaction associated with a specific BibernateSession concurrently.
Since:
1.0
Author:
Blyzhnytsia Team
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Connection
     
    private final Set<Object>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds an entity to the set of updated entities.
    void
    Commits the transaction, and end the unit of work, applies changes to the database, and closes the associated connection.
    void
    Force the underlying transaction to roll back, undoing changes made during the transaction, and closes the associated connection.
    private void
    Rolls back the ID fields of all entities in the updatedEntities set by setting them to null.
    void
    Starts the transaction by setting auto-commit to false on the associated database connection.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • connection

      private final Connection connection
    • updatedEntities

      private final Set<Object> updatedEntities
  • Constructor Details

    • Transaction

      public Transaction()
  • Method Details

    • start

      public void start() throws SQLException
      Starts the transaction by setting auto-commit to false on the associated database connection.
      Throws:
      SQLException - If an SQL exception occurs while starting the transaction.
    • commit

      public void commit() throws SQLException
      Commits the transaction, and end the unit of work, applies changes to the database, and closes the associated connection. Clears the set of updated entities after a successful commit.
      Throws:
      SQLException - If an SQL exception occurs while committing the transaction or closing the connection.
    • rollback

      public void rollback() throws SQLException
      Force the underlying transaction to roll back, undoing changes made during the transaction, and closes the associated connection. Resets the ID fields of all updated entities to null during rollback.
      Throws:
      SQLException - If an SQL exception occurs while rolling back the transaction or closing the connection.
    • addUpdatedEntity

      public void addUpdatedEntity(Object entity)
      Adds an entity to the set of updated entities. Entities in this set are considered modified during the transaction. This set is contained here in order to be able to revert changes to the entity in the rollback case
      Parameters:
      entity - The entity to be added to the set of updated entities.
    • rollbackAllIds

      private void rollbackAllIds()
      Rolls back the ID fields of all entities in the updatedEntities set by setting them to null. This operation is typically performed during a rollback to undo changes made during the transaction.