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

no.ssb.rawdata.provider.state.h2.H2Transaction Maven / Gradle / Ivy

The newest version!
package no.ssb.rawdata.provider.state.h2;

import no.ssb.rawdata.api.persistence.PersistenceException;
import no.ssb.rawdata.api.persistence.Transaction;
import no.ssb.rawdata.api.persistence.TransactionStatistics;

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

class H2Transaction implements Transaction {

    final Connection connection;
    final TransactionStatistics statistics = new TransactionStatistics();

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

    @Override
    public CompletableFuture commit() throws PersistenceException {
        try {
            return CompletableFuture.completedFuture(statistics);
        } 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(statistics);
        } 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 - 2025 Weber Informatics LLC | Privacy Policy