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

io.descoped.rawdata.provider.postgres.tx.TransactionFactory Maven / Gradle / Ivy

The newest version!
package io.descoped.rawdata.provider.postgres.tx;

import io.descoped.rawdata.provider.postgres.PersistenceException;

import javax.sql.DataSource;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public interface TransactionFactory {

    /**
     * Execute a unit-of-work within a new transaction asynchronously.
     *
     * @param retryable a function that will be run in another thread asynchronously given a new transaction as input.
     * @param        The result type of the retryable function.
     * @param readOnly  whether the unit-of-work represent a read-only transaction or not.
     * @return a completable future that will be signalled when the asynchronous work is complete.
     */
    default  CompletableFuture runAsyncInIsolatedTransaction(Function retryable, boolean readOnly) {
        RuntimeException callerStackException = new RuntimeException();
        return CompletableFuture.supplyAsync(() -> {
            try (Transaction tx = createTransaction(readOnly)) {
                return retryable.apply(tx);
            } catch (Throwable t) {
                callerStackException.initCause(t);
                throw callerStackException;
            }
        });
    }

    /**
     * Create a new transaction.
     *
     * @param readOnly true if the transaction will only perform read operations, false if at least one write operation
     *                 will be performed, and false if the caller is unsure. Note that the underlying persistence
     *                 provider may be able to optimize performance and contention related issues when read-only
     *                 transactions are involved.
     * @return the newly created transaction
     * @throws PersistenceException
     */
    Transaction createTransaction(boolean readOnly) throws PersistenceException;

    boolean checkIfTableTopicExists(String topic, String table);

    DataSource dataSource();

    /**
     * Close all resources associated with this transaction-factory. Such resources will typically be open-transactions,
     * thread-pools, data-source connections, or other persistence-provider specific resources.
     */
    void close();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy