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
AnAbstractDataSource
and aSynchronization
that wraps anotherDataSource
that 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-awareDataSource
behave 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
DataSource
wrapped 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 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.void
beforeCompletion()
Implements theSynchronization.beforeCompletion()
method to do nothing.Connection
getConnection()
Returns a special kind ofConnection
that is sourced from an underlyingDataSource
.Connection
getConnection(String username, String password)
Returns a special kind ofConnection
that is sourced from an underlyingDataSource
.boolean
registerWith(Consumer<? super javax.transaction.Synchronization> registrar)
If there is an active transaction, registers thisJtaDataSource
with 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
- theDataSource
instance to which operations will be delegated; must not benull
transactionIsActiveSupplier
- aBooleanSupplier
that returnstrue
only 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
- aSupplier
ofDataSource
instances to which operations will be delegated; must not benull
transactionIsActiveSupplier
- anBooleanSupplier
that returnstrue
only 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 thisJtaDataSource
with 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
- aConsumer
that may accept thisJtaDataSource
if there is a currently active transaction; must not benull
- Returns:
true
if registration occurred;false
otherwise- Throws:
NullPointerException
- ifregistrar
isnull
RuntimeException
- if the suppliedregistrar
'saccept
method throws aRuntimeException
-
beforeCompletion
public void beforeCompletion()
Implements theSynchronization.beforeCompletion()
method to do nothing.- Specified by:
beforeCompletion
in 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:
afterCompletion
in interfacejavax.transaction.Synchronization
- Parameters:
status
- the status of the transaction after completion; must be eitherStatus.STATUS_COMMITTED
orStatus.STATUS_ROLLEDBACK
- Throws:
IllegalArgumentException
- ifstatus
is neitherStatus.STATUS_COMMITTED
norStatus.STATUS_ROLLEDBACK
-
getConnection
public Connection getConnection() throws SQLException
Returns a special kind ofConnection
that is sourced from an underlyingDataSource
.The
Connection
returned by this method:- is never
null
(unless the underlyingDataSource
is not JDBC-compliant) - is exactly the
Connection
returned by the underlyingDataSource
when there is no JTA transaction in effect at the time that this method is invoked
Otherwise, when a JTA transaction is in effect, the
Connection
returned by this method:- is the same
Connection
returned by prior invocations of this method on the same thread during the lifespan of a JTA transaction. That is, theConnection
is "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 thisConnection
will 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:
getConnection
in interfaceDataSource
- Returns:
- a non-
null
Connection
- Throws:
SQLException
- if an error occursRuntimeException
- if theBooleanSupplier
supplied at construction time that reports a transaction's status throws aRuntimeException
, or if theSupplier
supplied at construction time that retrieves a delegateDataSource
throws 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 ofConnection
that is sourced from an underlyingDataSource
.The
Connection
returned by this method:- is never
null
(unless the underlyingDataSource
is not JDBC-compliant) - is exactly the
Connection
returned by the underlyingDataSource
when there is no JTA transaction in effect at the time that this method is invoked
Otherwise, when a JTA transaction is in effect, the
Connection
returned by this method:- is the same
Connection
returned 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, theConnection
is "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 thisConnection
will 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:
getConnection
in interfaceDataSource
- Parameters:
username
- the username to use to acquire an underlyingConnection
; may benull
password
- the password to use to acquire an underlyingConnection
; may benull
- Returns:
- a non-
null
Connection
- Throws:
SQLException
- if an error occursRuntimeException
- if theBooleanSupplier
supplied at construction time that reports a transaction's status throws aRuntimeException
, or if theSupplier
supplied at construction time that retrieves a delegateDataSource
throws aRuntimeException
- See Also:
DataSource.getConnection()
,DataSource.getConnection(String, String)
- is never
-
-