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
ConstructorDescriptionConstructs a newTransactionalDatasource
with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Overrides theclose
method to remove the transaction association fromTransactionHolder
before closing the data source, ensuring proper cleanup and consistency.Overrides thegetConnection
method to provide transactional support for obtaining connections.Methods inherited from class io.github.blyznytsiaorg.bibernate.connectionpool.BibernateDataSource
getConnection, getLoginTimeout, getLogWriter, getParentLogger, isWrapperFor, setLoginTimeout, setLogWriter, unwrap
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
TransactionalDatasource
Constructs a newTransactionalDatasource
with the specified configuration.- Parameters:
config
- The configuration for the data source
-
-
Method Details
-
getConnection
Overrides thegetConnection
method 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 ofBibernateDataSource
to retrieve a regular connection.- Specified by:
getConnection
in interfaceDataSource
- Overrides:
getConnection
in classBibernateDataSource
- Returns:
- A
Connection
object 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 theclose
method to remove the transaction association fromTransactionHolder
before closing the data source, ensuring proper cleanup and consistency.- Overrides:
close
in classBibernateDataSource
-