Package io.helidon.integrations.jta.jdbc
Class JtaDataSource
- java.lang.Object
-
- io.helidon.integrations.jdbc.AbstractCommonDataSource
-
- io.helidon.integrations.jdbc.AbstractDataSource
-
- io.helidon.integrations.jta.jdbc.JtaDataSource
-
- All Implemented Interfaces:
Wrapper,CommonDataSource,DataSource,javax.transaction.Synchronization
public final class JtaDataSource extends AbstractDataSource implements javax.transaction.Synchronization
AnAbstractDataSourceand aSynchronizationthat wraps anotherDataSourcethat is known to not behave correctly in the presence of JTA transaction management, such as one supplied by any of several freely and commercially available connection pools, and that makes such a non-JTA-awareDataSourcebehave as sensibly as possible in the presence of a JTA-managed transaction.Thread Safety
Instances of this class are safe for concurrent use by multiple threads. No such guarantee obviously can be made about the
DataSourcewrapped by any given instance of this class.Note that the JDBC specification places no requirement on any implementor to make any implementations of any JDBC constructs thread-safe.
-
-
Constructor Summary
Constructors Constructor Description JtaDataSource(Supplier<? extends DataSource> delegateSupplier, BooleanSupplier transactionIsActiveSupplier)Creates a newJtaDataSource.JtaDataSource(DataSource dataSource, BooleanSupplier transactionIsActiveSupplier)Creates a newJtaDataSource.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterCompletion(int status)Ensures that any thread-associated connections are properly committed, restored to their initial states, closed where appropriate and removed from the system when a definitionally thread-scoped JTA transaction commits or rolls back.voidbeforeCompletion()Implements theSynchronization.beforeCompletion()method to do nothing.ConnectiongetConnection()Returns a special kind ofConnectionthat is sourced from an underlyingDataSource.ConnectiongetConnection(String username, String password)Returns a special kind ofConnectionthat is sourced from an underlyingDataSource.booleanregisterWith(Consumer<? super javax.transaction.Synchronization> registrar)If there is an active transaction, registers thisJtaDataSourcewith the supplied registrar, which is most commonly—but is not required to be—a reference to theTransactionSynchronizationRegistry.registerInterposedSynchronization(Synchronization)method.-
Methods inherited from class io.helidon.integrations.jdbc.AbstractDataSource
isWrapperFor, unwrap
-
Methods inherited from class io.helidon.integrations.jdbc.AbstractCommonDataSource
getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriter
-
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, getParentLogger
-
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder, getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
-
-
-
-
Constructor Detail
-
JtaDataSource
public JtaDataSource(DataSource dataSource, BooleanSupplier transactionIsActiveSupplier)
Creates a newJtaDataSource.- Parameters:
dataSource- theDataSourceinstance to which operations will be delegated; must not benulltransactionIsActiveSupplier- aBooleanSupplierthat returnstrueonly if the current transaction, if any, is active; must not benull- Throws:
NullPointerException- if either parameter isnull- See Also:
JtaDataSource(Supplier, BooleanSupplier)
-
JtaDataSource
public JtaDataSource(Supplier<? extends DataSource> delegateSupplier, BooleanSupplier transactionIsActiveSupplier)
Creates a newJtaDataSource.- Parameters:
delegateSupplier- aSupplierofDataSourceinstances to which operations will be delegated; must not benulltransactionIsActiveSupplier- anBooleanSupplierthat returnstrueonly if the current transaction, if any, is active; must not benull- Throws:
NullPointerException- if either parameter isnull
-
-
Method Detail
-
registerWith
public boolean registerWith(Consumer<? super javax.transaction.Synchronization> registrar)
If there is an active transaction, registers thisJtaDataSourcewith the supplied registrar, which is most commonly—but is not required to be—a reference to theTransactionSynchronizationRegistry.registerInterposedSynchronization(Synchronization)method.If there is no currently active transaction, no action is taken.
- Parameters:
registrar- aConsumerthat may accept thisJtaDataSourceif there is a currently active transaction; must not benull- Returns:
trueif registration occurred;falseotherwise- Throws:
NullPointerException- ifregistrarisnullRuntimeException- if the suppliedregistrar'sacceptmethod throws aRuntimeException
-
beforeCompletion
public void beforeCompletion()
Implements theSynchronization.beforeCompletion()method to do nothing.- Specified by:
beforeCompletionin interfacejavax.transaction.Synchronization
-
afterCompletion
public void afterCompletion(int status)
Ensures that any thread-associated connections are properly committed, restored to their initial states, closed where appropriate and removed from the system when a definitionally thread-scoped JTA transaction commits or rolls back.- Specified by:
afterCompletionin interfacejavax.transaction.Synchronization- Parameters:
status- the status of the transaction after completion; must be eitherStatus.STATUS_COMMITTEDorStatus.STATUS_ROLLEDBACK- Throws:
IllegalArgumentException- ifstatusis neitherStatus.STATUS_COMMITTEDnorStatus.STATUS_ROLLEDBACK
-
getConnection
public Connection getConnection() throws SQLException
Returns a special kind ofConnectionthat is sourced from an underlyingDataSource.The
Connectionreturned by this method:- is never
null(unless the underlyingDataSourceis not JDBC-compliant) - is exactly the
Connectionreturned by the underlyingDataSourcewhen there is no JTA transaction in effect at the time that this method is invoked
Otherwise, when a JTA transaction is in effect, the
Connectionreturned by this method:- is the same
Connectionreturned by prior invocations of this method on the same thread during the lifespan of a JTA transaction. That is, theConnectionis "pinned" to the current thread for the lifespan of the transaction. - is not actually closeable when a JTA transaction is in
effect. The
Connection.close()method will behave from the standpoint of the caller as if it functions normally, but its invocation will not actually be propagated to the underlyingDataSource's connection. The fact that it was in fact invoked will be stored, and at such time that the JTA transaction completes thisConnectionwill be closed at that point. - has its autocommit status set to
false - will have
Connection.commit()called on it when the JTA transaction commits - will have
Connection.rollback()called on it when the JTA transaction rolls back - will have its autocommit status restored to its original value after the transaction completes
- Specified by:
getConnectionin interfaceDataSource- Returns:
- a non-
nullConnection - Throws:
SQLException- if an error occursRuntimeException- if theBooleanSuppliersupplied at construction time that reports a transaction's status throws aRuntimeException, or if theSuppliersupplied at construction time that retrieves a delegateDataSourcethrows aRuntimeException- See Also:
DataSource.getConnection(),DataSource.getConnection(String, String)
- is never
-
getConnection
public Connection getConnection(String username, String password) throws SQLException
Returns a special kind ofConnectionthat is sourced from an underlyingDataSource.The
Connectionreturned by this method:- is never
null(unless the underlyingDataSourceis not JDBC-compliant) - is exactly the
Connectionreturned by the underlyingDataSourcewhen there is no JTA transaction in effect at the time that this method is invoked
Otherwise, when a JTA transaction is in effect, the
Connectionreturned by this method:- is the same
Connectionreturned by prior invocations of this method with the same credentials (or no credentials) on the same thread during the lifespan of a JTA transaction. That is, theConnectionis "pinned" to the current thread for the lifespan of the transaction - is not actually closeable when a JTA transaction is in
effect. The
Connection.close()method will behave from the standpoint of the caller as if it functions normally, but its invocation will not actually be propagated to the underlyingDataSource's connection. The fact that it was in fact invoked will be stored, and at such time that the JTA transaction completes thisConnectionwill be closed at that point. - has its autocommit status set to
false - will have
Connection.commit()called on it when the JTA transaction commits - will have
Connection.rollback()called on it when the JTA transaction rolls back - will have its autocommit status restored to its original value after the transaction completes
- Specified by:
getConnectionin interfaceDataSource- Parameters:
username- the username to use to acquire an underlyingConnection; may benullpassword- the password to use to acquire an underlyingConnection; may benull- Returns:
- a non-
nullConnection - Throws:
SQLException- if an error occursRuntimeException- if theBooleanSuppliersupplied at construction time that reports a transaction's status throws aRuntimeException, or if theSuppliersupplied at construction time that retrieves a delegateDataSourcethrows aRuntimeException- See Also:
DataSource.getConnection(),DataSource.getConnection(String, String)
- is never
-
-