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.ecfront.easybi.dbutils.exchange.ConcurrentDB Maven / Gradle / Ivy
package com.ecfront.easybi.dbutils.exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 并发版本的数据操作类
*/
public class ConcurrentDB {
public ConcurrentDB(DB db){
this.db=db;
}
/**
* DDL操作
* @param ddls DDL语句
*/
public void ddls(final List ddls) throws SQLException{
final CountDownLatch signal = new CountDownLatch(ddls.size());
for (int i = 0; i < ddls.size(); i++) {
final int finalI = i;
executorService.execute(new Runnable() {
@Override
public void run() {
try {
db.ddl(ddls.get(finalI));
} catch (SQLException e) {
logger.warn("ddl execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
}
/**
* 获取多个对象
*
* @param sqls SQLs
* @param clazz 对象类
* @return java对象集合
*/
public Map getObjects(final Map sqls, final Class clazz) throws SQLException, ExecutionException, InterruptedException {
return getObjects(sqls,null,clazz);
}
/**
* 获取多个对象
*
* @param sqls SQLs
* @param params 参数
* @param clazz 对象类
* @return java对象集合
*/
public Map getObjects(final Map sqls, final Object[] params,final Class clazz) throws SQLException {
final Map result = new HashMap();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for(final Map.Entry entry:sqls.entrySet()){
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.getObject(entry.getValue(), params, clazz));
} catch (SQLException e) {
logger.warn("getObjects execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 获取多个对象集合
*
* @param sqls SQLs
* @param clazz 对象类
* @return java对象集合
*/
public Map> findObjects(final Map sqls,final Class clazz) throws SQLException {
return findObjects(sqls, null, clazz);
}
/**
* 获取多个对象集合
*
* @param sqls SQLs
* @param params 参数
* @param clazz 对象类
* @return java对象集合
*/
public Map> findObjects(final Map sqls, final Object[] params, final Class clazz) throws SQLException {
final Map> result = new HashMap>();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.findObjects(entry.getValue(), params, clazz));
} catch (SQLException e) {
logger.warn("findObjects execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 获取多个对象集合(带分页)
*
* @param sqls SQLs
* @param pageNumber 页码(从1开始)
* @param pageSize 每页条数
* @param clazz 对象类
* @return java对象集合(带分页)
*/
public Map> findObjects(final Map sqls,long pageNumber, long pageSize, Class clazz) throws SQLException {
return findObjects(sqls, null, pageNumber, pageSize, clazz);
}
/**
* 获取多个对象集合(带分页)
*
* @param sqls SQLs
* @param params 参数
* @param pageNumber 页码(从1开始)
* @param pageSize 每页条数
* @param clazz 对象类
* @return java对象集合(带分页)
*/
public Map> findObjects(final Map sqls,final Object[] params,final long pageNumber, final long pageSize,final Class clazz) throws SQLException {
final Map> result = new HashMap>();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.findObjects(entry.getValue(), params, pageNumber, pageSize, clazz));
} catch (SQLException e) {
logger.warn("findObjects execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 获取单条记录集合
*
* @param sqls SQLs
* @return 单条记录集合
*/
public Map> get(final Map sqls) throws SQLException {
return get(sqls, null);
}
/**
* 获取单条记录集合
*
* @param sqls SQLs
* @param params 参数
* @return 单条记录集合
*/
public Map> get(final Map sqls,final Object[] params) throws SQLException {
final Map> result = new HashMap>();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.get(entry.getValue(), params));
} catch (Exception e) {
logger.warn("get execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 获取多条记录集合(带分页)
*
* @param sqls SQLs
* @return 多条记录集合(带分页)
*/
public Map>> find(final Map sqls) throws SQLException {
return find(sqls, null);
}
/**
* 获取多条记录集合(带分页)
*
* @param sqls SQLs
* @param params 参数
* @return 多条记录集合(带分页)
*/
public Map>> find(final Map sqls,final Object[] params) throws SQLException {
final Map>> result = new HashMap>>();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.find(entry.getValue(), params));
} catch (SQLException e) {
logger.warn("find execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 获取多条记录集合(带分页)
*
* @param sqls SQLs
* @param pageNumber 页码(从1开始)
* @param pageSize 每页条数
* @return 多条记录集合(带分页)
*/
public Map>> find(final Map sqls, int pageNumber, int pageSize) throws SQLException {
return find(sqls, null, pageNumber, pageSize);
}
/**
* 获取多条记录集合(带分页)
*
* @param sqls SQLs
* @param params 参数
* @param pageNumber 页码(从1开始)
* @param pageSize 每页条数
* @return 多条记录集合(带分页)
*/
public Map>> find(final Map sqls,final Object[] params, final int pageNumber, final int pageSize) throws SQLException {
final Map>> result = new HashMap>>();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.find(entry.getValue(), params,pageNumber,pageSize));
} catch (SQLException e) {
logger.warn("find execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 获取记录数集合
*
* @param sqls SQLs
* @return 记录数集合
*/
public Map count(final Map sqls) throws SQLException {
return count(sqls, null);
}
/**
* 获取记录数集合
*
* @param sqls SQLs
* @param params 参数
* @return 记录数集合
*/
public Map count(final Map sqls,final Object[] params) throws SQLException {
final Map result = new HashMap();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.count(entry.getValue(), params));
} catch (SQLException e) {
logger.warn("count execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 更新记录集合
*
* @param sqls SQLs
*/
public Map update(final Map sqls) throws SQLException {
return update(sqls, null);
}
/**
* 更新记录集合
*
* @param sqls SQLs
* @param params 参数
*/
public Map update(final Map sqls,final Object[] params) throws SQLException {
final Map result = new HashMap();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.update(entry.getValue(), params));
} catch (SQLException e) {
logger.warn("update execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
/**
* 批量更新记录集合
*
* @param sqls SQLs
* @param params 参数
*/
public Map batch(final Map sqls,final Object[][] params) throws SQLException {
final Map result = new HashMap();
final CountDownLatch signal = new CountDownLatch(sqls.size());
for (final Map.Entry entry : sqls.entrySet()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
result.put(entry.getKey(), db.batch(entry.getValue(), params));
} catch (SQLException e) {
logger.warn("batch execute error..", e);
} finally {
signal.countDown();
}
}
});
}
try {
signal.await();
} catch (InterruptedException e) {
logger.warn("MultiThread Interrupted.", e);
}
return result;
}
private DB db;
private static ExecutorService executorService= Executors.newCachedThreadPool();
private static final Logger logger = LoggerFactory.getLogger(ConcurrentDB.class);
}