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

io.descoped.rawdata.provider.postgres.PostgresTransaction Maven / Gradle / Ivy

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

import io.descoped.rawdata.provider.postgres.tx.Transaction;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.CompletableFuture;

class PostgresTransaction implements Transaction {

    final Connection connection;

    public PostgresTransaction(Connection connection) throws SQLException {
        this.connection = connection;
        connection.beginRequest();
    }

    @Override
    public Connection connection() {
        return connection;
    }

    @Override
    public CompletableFuture commit() throws PersistenceException {
        try {
            return CompletableFuture.completedFuture(null);
        } finally {
            try {
                connection.commit();
                connection.endRequest();
            } catch (SQLException e) {
                throw new PersistenceException(e);
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new PersistenceException(e);
                }
            }
        }
    }

    @Override
    public CompletableFuture cancel() {
        try {
            return CompletableFuture.completedFuture(null);
        } finally {
            try {
                connection.rollback();
                connection.endRequest();
            } catch (SQLException e) {
                throw new PersistenceException(e);
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new PersistenceException(e);
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy