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

org.ict4h.atomfeed.jdbc.AtomFeedJdbcTransactionManager Maven / Gradle / Ivy

There is a newer version: 1.10.1
Show newest version
package org.ict4h.atomfeed.jdbc;

import org.ict4h.atomfeed.transaction.AFTransactionManager;
import org.ict4h.atomfeed.transaction.AFTransactionWork;

import java.sql.Connection;
import java.sql.SQLException;

public class AtomFeedJdbcTransactionManager implements AFTransactionManager {
    private JdbcConnectionProvider connectionProvider;

    public AtomFeedJdbcTransactionManager(JdbcConnectionProvider connectionProvider) {
        this.connectionProvider = connectionProvider;
    }

    @Override
    public  T executeWithTransaction(AFTransactionWork action) throws RuntimeException {
        Connection connection = null;
        try {
            connection = connectionProvider.getConnection();
            connection.setAutoCommit(false);
            T result = action.execute();
            connection.commit();
            return result;
        } catch (SQLException e ) {
            if (connection != null) {
                try {
                    System.err.print("Transaction is being rolled back");
                    connection.rollback();
                } catch(SQLException excep) {
                    throw new RuntimeException("Error occurred while trying to rollback transaction", excep);
                }
            }
            throw new RuntimeException("Error occurred while trying to execute in transaction", e);
        } finally {
            //connection.setAutoCommit(true);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy