Class Transaction
java.lang.Object
io.github.blyznytsiaorg.bibernate.transaction.Transaction
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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addUpdatedEntity
(Object entity) Adds an entity to the set of updated entities.void
commit()
Commits the transaction, and end the unit of work, applies changes to the database, and closes the associated connection.void
rollback()
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
start()
Starts the transaction by setting auto-commit to false on the associated database connection.
-
Field Details
-
connection
-
updatedEntities
-
-
Constructor Details
-
Transaction
public Transaction()
-
-
Method Details
-
start
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
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
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
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.
-