Class TransactionalDatasource
java.lang.Object
io.github.blyznytsiaorg.bibernate.connectionpool.BibernateDataSource
io.github.blyznytsiaorg.bibernate.transaction.TransactionalDatasource
- All Implemented Interfaces:
Wrapper,CommonDataSource,DataSource
TransactionalDatasource extends BibernateDataSource and provides support for managing
transactions within the data source.
This class ensures that connections retrieved during an active transaction are associated with that transaction.
When a transaction is not active, it falls back to the default behavior of its parent class, BibernateDataSource.
The class overrides the getConnection method to check for an active transaction using TransactionHolder.
If a transaction is active, it returns the connection associated with that transaction; otherwise, it falls back
to the default behavior of the parent class to retrieve a regular connection.
The close method is overridden to remove the transaction association from TransactionHolder
before closing the data source, ensuring proper cleanup and consistency.
Instances of this class are created with a BibernateDatasSourceConfig, which is passed to its superclass,
BibernateDataSource, during construction.
- Since:
- 1.0
- Author:
- Blyzhnytsia Team
- See Also:
-
Field Summary
Fields inherited from class io.github.blyznytsiaorg.bibernate.connectionpool.BibernateDataSource
METHOD_IS_NOT_SUPPORTED -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newTransactionalDatasourcewith the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Overrides theclosemethod to remove the transaction association fromTransactionHolderbefore closing the data source, ensuring proper cleanup and consistency.Overrides thegetConnectionmethod to provide transactional support for obtaining connections.Methods inherited from class io.github.blyznytsiaorg.bibernate.connectionpool.BibernateDataSource
getConnection, getLoginTimeout, getLogWriter, getParentLogger, isWrapperFor, setLoginTimeout, setLogWriter, unwrapMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
TransactionalDatasource
Constructs a newTransactionalDatasourcewith the specified configuration.- Parameters:
config- The configuration for the data source
-
-
Method Details
-
getConnection
Overrides thegetConnectionmethod to provide transactional support for obtaining connections. If an active transaction is present, it returns the connection associated with that transaction; otherwise, it falls back to the default behavior ofBibernateDataSourceto retrieve a regular connection.- Specified by:
getConnectionin interfaceDataSource- Overrides:
getConnectionin classBibernateDataSource- Returns:
- A
Connectionobject associated with an active transaction or a regular connection if no transaction is active - Throws:
SQLException- If an SQL exception occurs while obtaining a connection
-
close
public void close()Overrides theclosemethod to remove the transaction association fromTransactionHolderbefore closing the data source, ensuring proper cleanup and consistency.- Overrides:
closein classBibernateDataSource
-