All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.transaction.TransactionOperations Maven / Gradle / Ivy

There is a newer version: 4.9.3
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.transaction;

import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Blocking;

import java.util.Optional;

/***
 * Generic transaction operations interface.
 *
 * @param  The resource type, such as the connection.
 * @author graemerocher
 * @since 1.0.0
 */
@Blocking
public interface TransactionOperations {

    /**
     * Obtains the connection for the current transaction.
     * @return The connection
     * @throws io.micronaut.transaction.exceptions.NoTransactionException if no
     * connection exists for the current transaction
     */
    @NonNull
    T getConnection();

    /**
     * Check if the connection exists.
     *
     * @return True if transaction exists
     * @since 3.3
     */
    boolean hasConnection();

    /**
     * Find optional propagated transaction status.
     * @return The transaction status.
     */
    @Experimental
    Optional> findTransactionStatus();

    /**
     * Execute a transaction within the context of the function.
     *
     * @param definition The transaction definition
     * @param callback The call back
     * @param  The result
     * @return The result
     */
     R execute(@NonNull TransactionDefinition definition, @NonNull TransactionCallback callback);

    /**
     * Execute a read-only transaction within the context of the function.
     *
     * @param callback The call back
     * @param  The result
     * @return The result
     */
    default  R executeRead(@NonNull TransactionCallback callback) {
        return execute(TransactionDefinition.READ_ONLY, callback);
    }

    /**
     * Execute a default transaction within the context of the function.
     *
     * @param callback The call back
     * @param  The result
     * @return The result
     */
    default  R executeWrite(@NonNull TransactionCallback callback) {
        return execute(TransactionDefinition.DEFAULT, callback);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy