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

com.firefly.db.TransactionalJDBCHelper Maven / Gradle / Ivy

package com.firefly.db;

import com.firefly.utils.Assert;
import com.firefly.utils.function.Func1;
import com.firefly.utils.function.Func2;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import static com.firefly.db.JDBCConnectionUtils.setAutoCommit;

public class TransactionalJDBCHelper {

    private final static Logger log = LoggerFactory.getLogger("firefly-system");

    protected final JDBCHelper jdbcHelper;
    protected final TransactionalManager transactionalManager;

    public TransactionalJDBCHelper(DataSource dataSource) {
        this(dataSource, true, null, new ThreadLocalTransactionalManager(dataSource));
    }

    public TransactionalJDBCHelper(DataSource dataSource, boolean monitorEnable, MetricReporterFactory metricReporterFactory, TransactionalManager transactionalManager) {
        this(new JDBCHelper(dataSource, monitorEnable, metricReporterFactory), transactionalManager);
    }

    public TransactionalJDBCHelper(JDBCHelper jdbcHelper, TransactionalManager transactionalManager) {
        this.jdbcHelper = jdbcHelper;
        this.transactionalManager = transactionalManager;
    }

    public JDBCHelper getJdbcHelper() {
        return jdbcHelper;
    }

    public TransactionalManager getTransactionalManager() {
        return transactionalManager;
    }

    public  T queryForSingleColumn(String sql, Object... params) {
        return executeSQL((connection, helper) -> helper.queryForSingleColumn(connection, sql, params));
    }

    public  T queryForObject(String sql, Class t, Object... params) {
        return executeSQL((connection, helper) -> helper.queryForObject(connection, sql, t, params));
    }

    public  T queryForObject(String sql, Class t, BeanProcessor beanProcessor, Object... params) {
        return executeSQL((connection, helper) -> helper.queryForObject(connection, sql, t, beanProcessor, params));
    }

    public  T queryById(Class t, Object id) {
        return executeSQL((connection, helper) -> helper.queryById(connection, t, id));
    }

    public  Map queryForBeanMap(String sql, Class t, Object... params) {
        return executeSQL((connection, helper) -> helper.queryForBeanMap(connection, sql, t, params));
    }

    public  Map queryForBeanMap(String sql, Class t, BeanProcessor beanProcessor, Object... params) {
        return executeSQL((connection, helper) -> {
            String columnName = helper.getDefaultBeanProcessor().getIdColumnName(t);
            Assert.notNull(columnName);

            return helper.queryForBeanMap(connection, sql, t, columnName, beanProcessor, params);
        });
    }

    public  List queryForList(String sql, Class t, Object... params) {
        return executeSQL((connection, helper) -> helper.queryForList(connection, sql, t, params));
    }

    public  List queryForList(String sql, Class t, BeanProcessor beanProcessor, Object... params) {
        return executeSQL((connection, helper) -> helper.queryForList(connection, sql, t, beanProcessor, params));
    }

    public int update(String sql, Object... params) {
        Integer ret = executeSQL((connection, helper) -> helper.update(connection, sql, params));
        return ret != null ? ret : -1;
    }

    public int updateObject(Object object) {
        Integer ret = executeSQL((connection, helper) -> helper.updateObject(connection, object));
        return ret != null ? ret : -1;
    }

    public  T insert(String sql, Object... params) {
        return executeSQL((connection, helper) -> helper.insert(connection, sql, params));
    }

    public  T insertObject(Object object) {
        return executeSQL((connection, helper) -> helper.insertObject(connection, object));
    }

    public int deleteById(Class t, Object id) {
        Integer ret = executeSQL((connection, helper) -> helper.deleteById(connection, t, id));
        return ret != null ? ret : -1;
    }

    public int[] batch(String sql, Object[][] params) {
        return executeSQL((connection, helper) -> {
            int[] ret;
            try {
                ret = helper.getRunner().batch(connection, sql, params);
            } catch (Exception e) {
                log.error("batch exception", e);
                throw new DBException(e);
            }
            return ret;
        });
    }

    public  T insertBatch(String sql, ResultSetHandler rsh, Object[][] params) {
        return executeSQL((connection, helper) -> {
            T ret;
            try {
                ret = helper.getRunner().insertBatch(connection, sql, rsh, params);
            } catch (Exception e) {
                log.error("insert batch exception", e);
                throw new DBException(e);
            }
            return ret;
        });
    }

    public  T executeTransaction(Func1 func) {
        transactionalManager.beginTransaction();
        try {
            T ret = func.call(this);
            transactionalManager.commit();
            return ret;
        } catch (Throwable t) {
            transactionalManager.rollback();
            log.error("the transaction exception", t);
            return null;
        } finally {
            transactionalManager.endTransaction();
        }
    }

    protected  T executeSQL(Func2 func) {
        if (transactionalManager.isTransactionBegin()) {
            transactionalManager.beginTransaction();
            try {
                T ret = func.call(transactionalManager.getConnection(), jdbcHelper);
                transactionalManager.commit();
                return ret;
            } catch (Throwable t) {
                transactionalManager.rollback();
                log.error("the transaction exception", t);
                return null;
            } finally {
                transactionalManager.endTransaction();
            }
        } else {
            try (Connection connection = transactionalManager.getConnection()) {
                setAutoCommit(connection, true);
                return func.call(connection, jdbcHelper);
            } catch (SQLException e) {
                log.error("execute SQL exception", e);
                throw new DBException(e);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy