Class TransactionalDatabaseProvider

java.lang.Object
org.litebridge.orm.persistence.TransactionalDatabaseProvider
All Implemented Interfaces:
DatabaseProvider

public final class TransactionalDatabaseProvider extends Object implements DatabaseProvider
A decorator class that wraps a DatabaseProvider to provide transactional support using a TransactionManager.

This class ensures that any database operation is executed with proper transaction management and cleanup logic when necessary.

Responsibilities:

  • Delegates all database operations to the underlying DatabaseProvider.
  • Ensures that transactional contexts are handled properly using the provided TransactionManager.
  • Cleans up the transaction context if it's no longer active and cleanup is required.

Thread Safety:

This class is thread-safe if the provided TransactionManager and DatabaseProvider are thread-safe.

Implementation Details:

  • The class uses a SqlOperationSupplier functional interface to wrap database operations and ensure proper invocation of cleanup logic via the executeAndCleanupIfNeeded(SqlOperationSupplier) method.
  • Transaction cleanup logic is triggered if no active transaction exists in the TransactionManager, and cleanup is deemed necessary.
See Also:
  • Constructor Details

    • TransactionalDatabaseProvider

      public TransactionalDatabaseProvider(TransactionManager transactionManager, DatabaseProvider databaseProvider)
      Constructs a new TransactionalDatabaseProvider with the provided transaction manager and database provider.

      This class is responsible for managing transactional interactions with the underlying database provider.

      Parameters:
      transactionManager - The transaction manager responsible for managing transaction lifecycles.
      databaseProvider - The database provider that executes database operations within transactions.
  • Method Details

    • transactionManager

      public TransactionManager transactionManager()
      Returns the transaction manager responsible for managing transaction lifecycles.
      Returns:
      The transaction manager.
    • tableMetaData

      public TableMetaData tableMetaData(Table table, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Retrieve metadata for the specified table.
      Specified by:
      tableMetaData in interface DatabaseProvider
      Parameters:
      table - the Table object representing the table for which metadata is to be retrieved.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      a TableMetaData object containing metadata information about the specified table, including its columns, primary key, and other details.
      Throws:
      SQLException - if any SQL error occurs while retrieving the metadata.
    • insert

      public InsertResult insert(Insert insert, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Execute an INSERT operation in the database using the provided Insert statement.
      Specified by:
      insert in interface DatabaseProvider
      Parameters:
      insert - the Insert statement containing the table, columns, and rows to insert.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an InsertResult containing the number of rows affected and any generated keys.
      Throws:
      SQLException - if any SQL error occurs during the execution of the insert operation.
    • update

      public UpdateResult update(Update update, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Execute an UPDATE operation in the database using the provided Update statement.
      Specified by:
      update in interface DatabaseProvider
      Parameters:
      update - the Update statement containing the table, columns, and rows to update.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an UpdateResult containing the number of rows affected.
      Throws:
      SQLException - if any SQL error occurs during the execution of the update operation.
    • delete

      public UpdateResult delete(Delete delete, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Execute a DELETE operation in the database using the provided Delete statement.
      Specified by:
      delete in interface DatabaseProvider
      Parameters:
      delete - the Delete statement containing the table and rows to delete.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an UpdateResult containing the number of rows affected.
      Throws:
      SQLException - if any SQL error occurs during the execution of the delete operation.
    • select

      public List<Row> select(Select select, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Execute a SELECT operation in the database using the provided Select statement.
      Specified by:
      select in interface DatabaseProvider
      Parameters:
      select - the Select statement containing information about the table, expressions, joins, conditions, ordering, and optional limits for the query.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      a List of Row objects representing the results of the SELECT operation.
      Throws:
      SQLException - if any SQL error occurs during the execution of the SELECT operation.
    • toSql

      public String toSql(Operation operation, ConnectionProvider connectionProvider)
      Description copied from interface: DatabaseProvider
      Converts the given Operation into its corresponding SQL representation.

      This does not execute the operation, it only generates the SQL string.

      Specified by:
      toSql in interface DatabaseProvider
      Parameters:
      operation - the Operation object representing the database operation (e.g., SELECT, INSERT, UPDATE, DELETE) to be converted to a SQL string.
      connectionProvider - the ConnectionProvider used to obtain database-specific context or information required for SQL generation, such as metadata or dialect.
      Returns:
      a String containing the SQL representation of the given Operation.
    • nativeSqlQuery

      public List<Row> nativeSqlQuery(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Executes a SQL query with the given SQL string and a list of positional bind parameters.
      Specified by:
      nativeSqlQuery in interface DatabaseProvider
      Parameters:
      sql - the SQL query string to be executed; must not be null
      bindParameters - the list of parameters to bind to the query
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      a list of Row objects representing the result set of the query
      Throws:
      SQLException - if an error occurs during query execution
    • nativeSqlUpdate

      public UpdateResult nativeSqlUpdate(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Executes a SQL update statement with the given SQL string and a list of positional bind parameters.

      This method delegates to the overloaded execute method that accepts a list of bind parameters.

      Specified by:
      nativeSqlUpdate in interface DatabaseProvider
      Parameters:
      sql - the SQL update statement to execute; must not be null
      bindParameters - the list of named parameters to bind to the statement
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an UpdateResult object that encapsulates the outcome of the update operation
      Throws:
      SQLException - if an error occurs while executing the update statement
    • getSequenceColumnValueGenerator

      public SequenceColumnValueGenerator getSequenceColumnValueGenerator(String sequenceName)
      Description copied from interface: DatabaseProvider
      Retrieves a SequenceColumnValueGenerator instance for generating SQL fragments that fetch the next value from a specified database sequence. This is typically used in SQL statements such as INSERT or UPDATE to generate unique values from a sequence.
      Specified by:
      getSequenceColumnValueGenerator in interface DatabaseProvider
      Parameters:
      sequenceName - the name of the database sequence from which the values will be generated. It is used to create the SQL fragment to retrieve the next value in the sequence.
      Returns:
      a SequenceColumnValueGenerator instance that generates SQL fragments for retrieving sequence values.
    • getSqlFunctionRegistry

      public SqlFunctionRegistry getSqlFunctionRegistry()
      Description copied from interface: DatabaseProvider
      Retrieve the SqlFunctionRegistry instance associated with the database provider.

      The SqlFunctionRegistry is used for registering and managing SQL functions that can be used in SQL queries executed by the database provider.

      Specified by:
      getSqlFunctionRegistry in interface DatabaseProvider
      Returns:
      the SqlFunctionRegistry instance for managing SQL functions
    • getTypeConverter

      public TypeConverter getTypeConverter()
      Description copied from interface: DatabaseProvider
      Retrieve the TypeConverter instance associated with the database provider.

      The TypeConverter is used for converting objects between different types, typically for database data type conversions and mapping domain-specific representations.

      Specified by:
      getTypeConverter in interface DatabaseProvider
      Returns:
      the TypeConverter instance for handling data type conversions
    • getAliasTransformer

      public AliasTransformer getAliasTransformer()
      Description copied from interface: DatabaseProvider
      Retrieve the AliasTransformer instance associated with the database provider.
      Specified by:
      getAliasTransformer in interface DatabaseProvider
      Returns:
      the AliasTransformer instance for transforming aliases