Class AbstractConnectionOperations<C>
- Type Parameters:
C- The connection type
- All Implemented Interfaces:
ConnectionOperations<C>, SynchronousConnectionManager<C>
- Direct Known Subclasses:
DefaultDataSourceConnectionOperations, HibernateConnectionOperations
- Since:
- 4.0.0
- Author:
- Denis Stepanov
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConnectionCustomizer(@NonNull ConnectionCustomizer<C> connectionCustomizer) Adds a connection customizer to the list of customizers that will be notified before or after a call to the underlying data repository is issues.protected abstract voidcloseConnection(ConnectionStatus<C> connectionStatus) Closed the connection.voidcomplete(@NonNull ConnectionStatus<C> status) Completes the connection.final <R> Rexecute(@NonNull ConnectionDefinition definition, @NonNull Function<ConnectionStatus<C>, R> callback) Execute a connection within the context of the function.final <R> RexecuteWithConnection(@NonNull C connection, @NonNull ConnectionDefinition definition, boolean ownerControlsTransaction, @NonNull Function<ConnectionStatus<C>, R> callback) Executes the callback with a connection supplied by the caller as the current connection.final Optional<ConnectionStatus<C>> Obtains the current connection.@NonNull DefaultConnectionStatus<C> getConnection(@NonNull ConnectionDefinition definition) Opens or reuses the existing connection based on the definition.booleanmanagesConnection(ConnectionStatus<C> connectionStatus) Determine whether the given connection status refers to a connection managed by thisConnectionOperationsinstance.protected abstract CopenConnection(ConnectionDefinition definition) Opens a new connection.protected abstract voidsetupConnection(ConnectionStatus<C> connectionStatus) Setups the connection after it have been open.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ConnectionOperations
executeRead, executeWithConnection, executeWrite, getConnectionStatus
-
Field Details
-
logger
protected final org.slf4j.Logger logger
-
-
Constructor Details
-
AbstractConnectionOperations
public AbstractConnectionOperations()
-
-
Method Details
-
managesConnection
Description copied from interface:ConnectionOperationsDetermine whether the given connection status refers to a connection managed by thisConnectionOperationsinstance.- Specified by:
managesConnectionin interfaceConnectionOperations<C>- Parameters:
connectionStatus- The connection status to verify- Returns:
- true if the connection is managed (i.e. created/supplied) by this operations instance
-
addConnectionCustomizer
Adds a connection customizer to the list of customizers that will be notified before or after a call to the underlying data repository is issues.The added customizer will be sorted according to its order using the
OrderUtil.sort(List)method.- Parameters:
connectionCustomizer- the connection customizer to add- Since:
- 4.11
-
openConnection
Opens a new connection.- Parameters:
definition- The connection definition- Returns:
- The connection
-
setupConnection
Setups the connection after it have been open.- Parameters:
connectionStatus- The connection status
-
closeConnection
Closed the connection.- Parameters:
connectionStatus- The connection status
-
findConnectionStatus
Description copied from interface:ConnectionOperationsObtains the current connection.- Specified by:
findConnectionStatusin interfaceConnectionOperations<C>- Returns:
- The optional connection
-
execute
public final <R> R execute(@NonNull ConnectionDefinition definition, @NonNull Function<ConnectionStatus<C>, R> callback) Description copied from interface:ConnectionOperationsExecute a connection within the context of the function.- Specified by:
executein interfaceConnectionOperations<C>- Type Parameters:
R- The result- Parameters:
definition- The connection definitioncallback- The call back- Returns:
- The result
-
executeWithConnection
public final <R> R executeWithConnection(@NonNull C connection, @NonNull ConnectionDefinition definition, boolean ownerControlsTransaction, @NonNull Function<ConnectionStatus<C>, R> callback) Description copied from interface:ConnectionOperationsExecutes the callback with a connection supplied by the caller as the current connection.Repositories, connection-scoped operations and transactions started inside the callback use the supplied connection, as they would use a connection opened by these operations, even when the caller is inside a transaction on another connection: a transaction started inside the callback never joins that transaction. Use it when another library owns a connection that Micronaut Data should take part in, such as the database connection of an Oracle AQ JMS session or of an XA resource.
When
ownerControlsTransactionistrue, the connection must not be in auto-commit mode: with auto-commit on there is no owner transaction to join, so every statement commits as it runs and the owner cannot roll it back. The owner of the connection controls its transaction, as with an AQ session, an OKafka producer or an XA resource, and transactions started inside the callback join it. They don't change the auto-commit mode or the isolation level, and they don't commit or roll back the connection. ANESTEDtransaction uses a savepoint, andREQUIRES_NEWopens a separate connection. When joined work rolls back, the exception propagates, or this method throws aConnectionExceptionif the exception was handled inside the callback, so that the owner rolls back its transaction; transactions joined after such a rollback fail with anUnexpectedRollbackException. Micronaut Data does not see the owner's commit, so transaction synchronizations, such as@TransactionalEventListenermethods andafterCommitcallbacks, cannot be registered on a joined transaction and fail with aTransactionUsageException. Hibernate transactions cannot join the owner's transaction either, because Hibernate writes need a Hibernate transaction of their own, so they also fail with aTransactionUsageException: use JDBC repositories on such a connection.When
ownerControlsTransactionisfalse, transactions started inside the callback begin, commit and roll back on the supplied connection, and restore its auto-commit mode afterwards.Connection operations that cannot adopt a connection, such as the Spring ones, throw
UnsupportedOperationException. The JDBC and Hibernate connection operations support it; for Hibernate, the work runs on a session opened on the supplied connection.The connection is not set up (for example, read-only mode is left as it is), no request boundaries are marked on it and it is never closed: the caller remains responsible for it, including Oracle request boundaries and DRCP release.
- Specified by:
executeWithConnectionin interfaceConnectionOperations<C>- Type Parameters:
R- The result- Parameters:
connection- The connectiondefinition- The connection definition describing itownerControlsTransaction- Whether the owner of the connection controls its transaction, so that transactions inside the callback join itcallback- The call back- Returns:
- The result
-
getConnection
Description copied from interface:SynchronousConnectionManagerOpens or reuses the existing connection based on the definition. It's required to callSynchronousConnectionManager.complete(ConnectionStatus)after the connection is not needed anymore.- Specified by:
getConnectionin interfaceSynchronousConnectionManager<C>- Parameters:
definition- The connection definition.- Returns:
- The connection status
-
complete
Description copied from interface:SynchronousConnectionManagerCompletes the connection. Closes it if the connection was open before.- Specified by:
completein interfaceSynchronousConnectionManager<C>- Parameters:
status- The connection status
-