Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
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);
}
}
}
}