1、修改Mysql线程池逻辑
2、新增数据表信息查询接口 3、优化前端代码逻辑 4、新增业面业务功能提示信息窗口
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
package net.tccn.dbq;
|
||||
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.base.PageBean;
|
||||
import net.tccn.dbq.fbean.FBean;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.jdbc.api.DbKit;
|
||||
import net.tccn.dbq.parser.ParseMysql;
|
||||
import net.tccn.dbq.parser.Parser;
|
||||
import net.tccn.meta.MetaKit;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class DbExecutors {
|
||||
private final static Parser PARSER = new ParseMysql();
|
||||
|
||||
public static PageBean findPage(FBean fBean) {
|
||||
|
||||
public static PageBean findPage(FBean fBean) throws ExecutionException, InterruptedException {
|
||||
//sql解析
|
||||
String[] sqls = PARSER.parse(fBean);
|
||||
|
||||
//当前的业务 => 获取主表 信息 => 数据源信息 => 数据源对象 => 创建数据工具对象 => 查询数据
|
||||
@@ -25,15 +26,13 @@ public class DbExecutors {
|
||||
|
||||
MetaTable mainTable = MetaKit.getMetaTableByAlias(metaService.getTable());
|
||||
|
||||
DbAccount dbAccount = DbAccount.dao.findByKey(mainTable.getDbPlatId());
|
||||
|
||||
DbKit dbKit = new DbKit(dbAccount);
|
||||
DbKit dbKit = MetaKit.getDbKit(mainTable.getDbPlatId());
|
||||
System.out.println("countSql: " + sqls[0]);
|
||||
System.out.println("findSql: " + sqls[1]);
|
||||
|
||||
Integer count = dbKit.queryColumn(sqls[0], int.class);
|
||||
List<Map> list = dbKit.findList(sqls[1], Map.class);
|
||||
CompletableFuture<Integer> countFuture = CompletableFuture.supplyAsync(() -> dbKit.queryColumn(sqls[0], int.class));
|
||||
CompletableFuture<List<Map>> listFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(sqls[1], Map.class));
|
||||
|
||||
return PageBean.by(list, count);
|
||||
return PageBean.by(listFuture.get(), countFuture.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,14 @@ public class DbKit implements DbSource{
|
||||
public <T> T queryColumn(String sql, Class<T> type) {
|
||||
return dbSource.queryColumn(sql, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable(String sql) {
|
||||
dbSource.createTable(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropTable(String tableName) {
|
||||
dbSource.dropTable(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,7 @@ public interface DbSource {
|
||||
default Date queryDate(String sql) {
|
||||
return queryColumn(sql, Date.class);
|
||||
}
|
||||
|
||||
void createTable(String sql);
|
||||
void dropTable(String tableName);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/12 14:20.
|
||||
@@ -17,7 +17,7 @@ import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class DbSourceMysql implements DbSource {
|
||||
|
||||
private static ConcurrentHashMap<String, AtomicReferenceArray<Connection>> conns = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<String, LinkedBlockingQueue<Connection>> conns = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<String, AtomicInteger> counter = new ConcurrentHashMap<>();
|
||||
|
||||
private String accountKey;
|
||||
@@ -111,57 +111,66 @@ public class DbSourceMysql implements DbSource {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable(String sql) {
|
||||
new RuntimeException("DbSourceMysql.createTable NOT SUPPORT right now" ); // todo:
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropTable(String tableName) {
|
||||
new RuntimeException("[DbSourceMysql.dropTable] NOT SUPPORT right now" ); // todo:
|
||||
}
|
||||
|
||||
private Connection connection() {
|
||||
return connection(0);
|
||||
}
|
||||
private Connection connection(int n) {
|
||||
AtomicReferenceArray<Connection> arr = conns.getOrDefault(accountKey, new AtomicReferenceArray<>(15));
|
||||
Connection connection = null;
|
||||
AtomicInteger num = counter.getOrDefault(accountKey, new AtomicInteger(0));
|
||||
for (int i = 0; num.get() > 0 && i < arr.length() && connection == null; i++) {
|
||||
try {
|
||||
connection = arr.getAndUpdate(i, null);
|
||||
} catch (Exception e) {
|
||||
System.out.println("getAndUpdate exception");
|
||||
}
|
||||
}
|
||||
if (connection == null) {
|
||||
try {
|
||||
if (num.get() < 15) {
|
||||
connection = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
|
||||
num.getAndIncrement();
|
||||
} else {
|
||||
//连接被全部使用中,等待1s后再次获取连接,直到得到连接 !!!
|
||||
Thread.sleep(1000);
|
||||
if (++n > 3)
|
||||
connection(n);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalArgumentException("获取数据库连接失败");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
LinkedBlockingQueue<Connection> queue = conns.getOrDefault(accountKey, new LinkedBlockingQueue<>(15));
|
||||
|
||||
return connection;
|
||||
Connection conn = null;
|
||||
AtomicInteger num = counter.getOrDefault(accountKey, new AtomicInteger(0));
|
||||
try {
|
||||
if (queue.size() == 0 && num.get() < 15) {
|
||||
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
|
||||
int x = num.incrementAndGet();
|
||||
counter.put(accountKey, num);
|
||||
System.out.println("创建新的连接:" + x);
|
||||
} else {
|
||||
conn = queue.take();
|
||||
if (conn != null) {
|
||||
System.out.println("获取已有连接" + conn);
|
||||
}
|
||||
}
|
||||
} catch (SQLException | InterruptedException e) {
|
||||
if (e instanceof InterruptedException) {
|
||||
try {
|
||||
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
|
||||
num.getAndIncrement();
|
||||
if (conn != null) {
|
||||
System.out.println("获取连接异常,并重新创建成功");
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
new IllegalArgumentException("创建连接失败", e);
|
||||
}
|
||||
num.getAndIncrement();
|
||||
counter.put(accountKey, num);
|
||||
} else {
|
||||
new IllegalArgumentException("获取连接失败", e);
|
||||
}
|
||||
}
|
||||
conns.put(accountKey, queue);
|
||||
return conn;
|
||||
}
|
||||
private void release(Connection connection) {
|
||||
AtomicReferenceArray<Connection> arr = conns.getOrDefault(accountKey, new AtomicReferenceArray<>(15));
|
||||
|
||||
int i = 0;
|
||||
boolean bool = false;
|
||||
while (i < arr.length() && !bool){
|
||||
bool = arr.compareAndSet(i++, null, connection);
|
||||
}
|
||||
|
||||
//如果没成功释放,关系连接
|
||||
if (!bool && connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
LinkedBlockingQueue<Connection> queue = conns.getOrDefault(accountKey, new LinkedBlockingQueue<>(15));
|
||||
try {
|
||||
if (connection != null) {
|
||||
queue.put(connection);
|
||||
conns.put(accountKey, queue);
|
||||
System.out.println("还回连接:" + connection);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package net.tccn.dbq.parser;
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.fbean.*;
|
||||
import net.tccn.meta.MetaKit;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.meta.MetaLink;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
|
||||
@@ -5,7 +5,7 @@ package net.tccn.dbq.table;
|
||||
* @author: liangxianyou at 2018/10/8 10:59.
|
||||
*/
|
||||
public class Column {
|
||||
private String name; //列名称
|
||||
private String field; //列名称
|
||||
private String type; //列类型
|
||||
private boolean notNull; //不为null
|
||||
private String comment; //列说明
|
||||
@@ -14,18 +14,18 @@ public class Column {
|
||||
}
|
||||
|
||||
public Column(String name, String type, boolean notNull, String comment) {
|
||||
this.name = name;
|
||||
this.field = name;
|
||||
this.type = type;
|
||||
this.notNull = notNull;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
@@ -52,4 +52,9 @@ public class Column {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
//-----------------------
|
||||
public void setNull(String notNull) {
|
||||
this.notNull = "NO".equalsIgnoreCase(notNull) ? true : false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class Table {
|
||||
|
||||
buf.append("CREATE TABLE " + name + "(");
|
||||
columns.forEach(x -> {
|
||||
buf.append("\n " + x.getName() + " " + x.getType() + ",");
|
||||
buf.append("\n " + x.getField() + " " + x.getType() + ",");
|
||||
});
|
||||
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
|
||||
Reference in New Issue
Block a user