Class TransactionalDatasource

java.lang.Object
io.github.blyznytsiaorg.bibernate.connectionpool.BibernateDataSource
io.github.blyznytsiaorg.bibernate.transaction.TransactionalDatasource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

public class TransactionalDatasource extends BibernateDataSource
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:
  • Constructor Details

    • TransactionalDatasource

      public TransactionalDatasource(BibernateDatasSourceConfig config)
      Constructs a new TransactionalDatasource with the specified configuration.
      Parameters:
      config - The configuration for the data source
  • Method Details

    • getConnection

      public Connection getConnection() throws SQLException
      Overrides the getConnection 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 of BibernateDataSource to retrieve a regular connection.
      Specified by:
      getConnection in interface DataSource
      Overrides:
      getConnection in class BibernateDataSource
      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 the close method to remove the transaction association from TransactionHolder before closing the data source, ensuring proper cleanup and consistency.
      Overrides:
      close in class BibernateDataSource