java.lang.Object
org.litebridge.orm.persistence.TransactionalDatabaseProvider
- All Implemented Interfaces:
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 providedTransactionManager and DatabaseProvider
are thread-safe.
Implementation Details:
- The class uses a
SqlOperationSupplierfunctional interface to wrap database operations and ensure proper invocation of cleanup logic via theexecuteAndCleanupIfNeeded(SqlOperationSupplier)method. - Transaction cleanup logic is triggered if no active transaction exists in the
TransactionManager, and cleanup is deemed necessary.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTransactionalDatabaseProvider(TransactionManager transactionManager, DatabaseProvider databaseProvider) Constructs a new TransactionalDatabaseProvider with the provided transaction manager and database provider. -
Method Summary
Modifier and TypeMethodDescriptiondelete(Delete delete, ConnectionProvider connectionProvider) Execute a DELETE operation in the database using the providedDeletestatement.Retrieve theAliasTransformerinstance associated with the database provider.getSequenceColumnValueGenerator(String sequenceName) Retrieves aSequenceColumnValueGeneratorinstance for generating SQL fragments that fetch the next value from a specified database sequence.Retrieve theSqlFunctionRegistryinstance associated with the database provider.Retrieve theTypeConverterinstance associated with the database provider.insert(Insert insert, ConnectionProvider connectionProvider) Execute an INSERT operation in the database using the providedInsertstatement.nativeSqlQuery(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) Executes a SQL query with the given SQL string and a list of positional bind parameters.nativeSqlUpdate(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) Executes a SQL update statement with the given SQL string and a list of positional bind parameters.select(Select select, ConnectionProvider connectionProvider) Execute a SELECT operation in the database using the providedSelectstatement.tableMetaData(Table table, ConnectionProvider connectionProvider) Retrieve metadata for the specified table.toSql(Operation operation, ConnectionProvider connectionProvider) Converts the givenOperationinto its corresponding SQL representation.Returns the transaction manager responsible for managing transaction lifecycles.update(Update update, ConnectionProvider connectionProvider) Execute an UPDATE operation in the database using the providedUpdatestatement.
-
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
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:DatabaseProviderRetrieve metadata for the specified table.- Specified by:
tableMetaDatain interfaceDatabaseProvider- Parameters:
table- theTableobject representing the table for which metadata is to be retrieved.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- a
TableMetaDataobject 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:DatabaseProviderExecute an INSERT operation in the database using the providedInsertstatement.- Specified by:
insertin interfaceDatabaseProvider- Parameters:
insert- theInsertstatement containing the table, columns, and rows to insert.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
InsertResultcontaining 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:DatabaseProviderExecute an UPDATE operation in the database using the providedUpdatestatement.- Specified by:
updatein interfaceDatabaseProvider- Parameters:
update- theUpdatestatement containing the table, columns, and rows to update.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
UpdateResultcontaining 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:DatabaseProviderExecute a DELETE operation in the database using the providedDeletestatement.- Specified by:
deletein interfaceDatabaseProvider- Parameters:
delete- theDeletestatement containing the table and rows to delete.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
UpdateResultcontaining the number of rows affected. - Throws:
SQLException- if any SQL error occurs during the execution of the delete operation.
-
select
Description copied from interface:DatabaseProviderExecute a SELECT operation in the database using the providedSelectstatement.- Specified by:
selectin interfaceDatabaseProvider- Parameters:
select- theSelectstatement containing information about the table, expressions, joins, conditions, ordering, and optional limits for the query.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- a
ListofRowobjects representing the results of the SELECT operation. - Throws:
SQLException- if any SQL error occurs during the execution of the SELECT operation.
-
toSql
Description copied from interface:DatabaseProviderConverts the givenOperationinto its corresponding SQL representation.This does not execute the operation, it only generates the SQL string.
- Specified by:
toSqlin interfaceDatabaseProvider- Parameters:
operation- theOperationobject representing the database operation (e.g., SELECT, INSERT, UPDATE, DELETE) to be converted to a SQL string.connectionProvider- theConnectionProviderused to obtain database-specific context or information required for SQL generation, such as metadata or dialect.- Returns:
- a
Stringcontaining the SQL representation of the givenOperation.
-
nativeSqlQuery
public List<Row> nativeSqlQuery(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderExecutes a SQL query with the given SQL string and a list of positional bind parameters.- Specified by:
nativeSqlQueryin interfaceDatabaseProvider- Parameters:
sql- the SQL query string to be executed; must not benullbindParameters- the list of parameters to bind to the queryconnectionProvider- theConnectionProviderused to get a database connection.- Returns:
- a list of
Rowobjects 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:DatabaseProviderExecutes 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:
nativeSqlUpdatein interfaceDatabaseProvider- Parameters:
sql- the SQL update statement to execute; must not benullbindParameters- the list of named parameters to bind to the statementconnectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
UpdateResultobject that encapsulates the outcome of the update operation - Throws:
SQLException- if an error occurs while executing the update statement
-
getSequenceColumnValueGenerator
Description copied from interface:DatabaseProviderRetrieves aSequenceColumnValueGeneratorinstance 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:
getSequenceColumnValueGeneratorin interfaceDatabaseProvider- 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
SequenceColumnValueGeneratorinstance that generates SQL fragments for retrieving sequence values.
-
getSqlFunctionRegistry
Description copied from interface:DatabaseProviderRetrieve theSqlFunctionRegistryinstance associated with the database provider.The
SqlFunctionRegistryis used for registering and managing SQL functions that can be used in SQL queries executed by the database provider.- Specified by:
getSqlFunctionRegistryin interfaceDatabaseProvider- Returns:
- the
SqlFunctionRegistryinstance for managing SQL functions
-
getTypeConverter
Description copied from interface:DatabaseProviderRetrieve theTypeConverterinstance associated with the database provider.The
TypeConverteris used for converting objects between different types, typically for database data type conversions and mapping domain-specific representations.- Specified by:
getTypeConverterin interfaceDatabaseProvider- Returns:
- the
TypeConverterinstance for handling data type conversions
-
getAliasTransformer
Description copied from interface:DatabaseProviderRetrieve theAliasTransformerinstance associated with the database provider.- Specified by:
getAliasTransformerin interfaceDatabaseProvider- Returns:
- the
AliasTransformerinstance for transforming aliases
-