Skip to content

Commit 2ef0657

Browse files
committed
解决 Demo 注册接口调用报错 parser.newSuccessResult 及 connectionMap.get 报错 NPE
1 parent 32c2777 commit 2ef0657

File tree

6 files changed

+117
-104
lines changed

6 files changed

+117
-104
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -892,16 +892,17 @@ public void onTableArrayParse(String key, L valueArray) throws Exception {
892892
public M parseResponse(RequestMethod method, String table, String alias
893893
, M request, List<Join<T, M, L>> joinList, boolean isProcedure) throws Exception {
894894
SQLConfig<T, M, L> config = newSQLConfig(method, table, alias, request, joinList, isProcedure)
895-
.setParser(parser)
895+
.setParser(getParser())
896896
.setObjectParser(this);
897897
return parseResponse(config, isProcedure);
898898
}
899899
@Override
900900
public M parseResponse(SQLConfig<T, M, L> config, boolean isProcedure) throws Exception {
901+
parser = getParser();
901902
if (parser.getSQLExecutor() == null) {
902903
parser.createSQLExecutor();
903904
}
904-
if (parser != null && config.gainParser() == null) {
905+
if (config.gainParser() == null) {
905906
config.setParser(parser);
906907
}
907908
return parser.getSQLExecutor().execute(config, isProcedure);

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @author Lemon
3838
*/
3939
public abstract class AbstractParser<T, M extends Map<String, Object>, L extends List<Object>>
40-
implements Parser<T, M, L>, ParserCreator<T, M, L>, VerifierCreator<T, M, L>, SQLCreator<T, M, L> {
40+
implements Parser<T, M, L> {
4141
protected static final String TAG = "AbstractParser";
4242

4343
/**
@@ -411,7 +411,6 @@ public AbstractParser<T, M, L> setNeedVerifyContent(boolean needVerifyContent) {
411411
return this;
412412
}
413413

414-
415414
protected SQLExecutor<T, M, L> sqlExecutor;
416415
protected Verifier<T, M, L> verifier;
417416
protected Map<String, Object> queryResultMap;//path-result
@@ -420,15 +419,16 @@ public AbstractParser<T, M, L> setNeedVerifyContent(boolean needVerifyContent) {
420419
public SQLExecutor<T, M, L> getSQLExecutor() {
421420
if (sqlExecutor == null) {
422421
sqlExecutor = createSQLExecutor();
423-
sqlExecutor.setParser(this);
424422
}
423+
sqlExecutor.setParser(this);
425424
return sqlExecutor;
426425
}
427426
@Override
428427
public Verifier<T, M, L> getVerifier() {
429428
if (verifier == null) {
430429
verifier = createVerifier().setVisitor(getVisitor());
431430
}
431+
verifier.setParser(this);
432432
return verifier;
433433
}
434434

@@ -437,13 +437,14 @@ public Verifier<T, M, L> getVerifier() {
437437
* @return
438438
* @throws Exception
439439
*/
440-
@NotNull
441440
public static <M extends Map<String, Object>> M parseRequest(String request) throws Exception {
442-
M obj = JSON.parseObject(request);
443-
if (obj == null) {
444-
throw new UnsupportedEncodingException("JSON格式不合法!");
441+
try {
442+
M req = JSON.parseObject(request);
443+
Objects.requireNonNull(req);
444+
return req;
445+
} catch (Throwable e) {
446+
throw new UnsupportedEncodingException("JSON格式不合法!" + e.getMessage() + "! " + request);
445447
}
446-
return obj;
447448
}
448449

449450
/**解析请求json并获取对应结果
@@ -569,7 +570,6 @@ public M parseResponse(M request) {
569570

570571
final String requestString = JSON.toJSONString(request);//request传进去解析后已经变了
571572

572-
573573
queryResultMap = new HashMap<String, Object>();
574574

575575
Exception error = null;
@@ -608,7 +608,8 @@ public M parseResponse(M request) {
608608

609609
res.putIfAbsent("time", endTime);
610610
if (Log.DEBUG) {
611-
res.put("sql:generate|cache|execute|maxExecute", getSQLExecutor().getGeneratedSQLCount() + "|" + getSQLExecutor().getCachedSQLCount() + "|" + getSQLExecutor().getExecutedSQLCount() + "|" + getMaxSQLCount());
611+
sqlExecutor = getSQLExecutor();
612+
res.put("sql:generate|cache|execute|maxExecute", sqlExecutor.getGeneratedSQLCount() + "|" + sqlExecutor.getCachedSQLCount() + "|" + sqlExecutor.getExecutedSQLCount() + "|" + getMaxSQLCount());
612613
res.put("depth:count|max", queryDepth + "|" + getMaxQueryDepth());
613614

614615
executedSQLDuration += sqlExecutor.getExecutedSQLDuration() + sqlExecutor.getSqlResultDuration();
@@ -639,6 +640,7 @@ public M parseResponse(M request) {
639640
Log.fd(TAG, requestMethod + "/parseResponse endTime = " + endTime + "; duration = " + duration);
640641
Log.sl("", '>', "\n\n\n");
641642
}
643+
642644
return res;
643645
}
644646

@@ -1085,6 +1087,7 @@ public M getStructure(@NotNull String table, String method, String tag, int vers
10851087

10861088
// 获取指定的JSON结构 <<<<<<<<<<<<<<
10871089
SQLConfig<T, M, L> config = createSQLConfig().setMethod(GET).setTable(table);
1090+
config.setParser(this);
10881091
config.setPrepared(false);
10891092
config.setColumn(Arrays.asList("structure"));
10901093

@@ -2105,8 +2108,7 @@ public M executeSQL(SQLConfig<T, M, L> config, boolean isSubquery) throws Except
21052108
}
21062109
}
21072110
else {
2108-
sqlExecutor = getSQLExecutor();
2109-
result = sqlExecutor.execute(config, false);
2111+
result = getSQLExecutor().execute(config, false);
21102112
// FIXME 改为直接在 sqlExecutor 内加好,最后 Parser<T, M, L> 取结果,可以解决并发执行导致内部计算出错
21112113
// executedSQLDuration += sqlExecutor.getExecutedSQLDuration() + sqlExecutor.getSqlResultDuration();
21122114
}
@@ -2531,7 +2533,7 @@ protected M objectVerify(RequestMethod method, String tag, int version, String n
25312533
// 获取指定的JSON结构 >>>>>>>>>>>>>>
25322534
M target = wrapRequest(method, tag, object, true);
25332535
// Map<String, Object> clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
2534-
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobalDatabase(), getGlobalSchema(), creator);
2536+
return getVerifier().setParser(this).verifyRequest(method, name, target, request, maxUpdateCount, getGlobalDatabase(), getGlobalSchema());
25352537
}
25362538

25372539
/***

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ public M getCacheItem(List<M> list, int position, SQLConfig<T, M, L> config) {
127127

128128

129129

130-
131-
132130
/**移除缓存
133131
* @param sql key
134132
* @param config
@@ -175,6 +173,9 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
175173
return null;
176174
}
177175

176+
Parser<T, M, L> parser2 = config.gainParser();
177+
parser = parser2 != null ? parser2 : getParser();;
178+
178179
boolean isExplain = config.isExplain();
179180
boolean isHead = RequestMethod.isHeadMethod(config.getMethod(), true);
180181

@@ -237,7 +238,7 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
237238
}
238239

239240
// updateCount>0时收集结果。例如更新操作成功时,返回count(affected rows)、id字段
240-
result = getParser().newSuccessResult(); // TODO 对 APIAuto 及其它现有的前端/客户端影响比较大,暂时还是返回 code 和 msg,5.0 再移除 JSON.createJSONObject();
241+
result = parser.newSuccessResult(); // TODO 对 APIAuto 及其它现有的前端/客户端影响比较大,暂时还是返回 code 和 msg,5.0 再移除 JSON.createJSONObject();
241242

242243
//id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
243244
result.put(JSONResponse.KEY_COUNT, updateCount);//返回修改的记录数
@@ -293,13 +294,12 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
293294
}
294295
}
295296

296-
297297
if (isExplain == false && isHead) {
298298
if (rs.next() == false) {
299-
return getParser().newErrorResult(new SQLException("数据库错误, rs.next() 失败!"));
299+
return parser.newErrorResult(new SQLException("数据库错误, rs.next() 失败!"));
300300
}
301301

302-
result = getParser().newSuccessResult();
302+
result = parser.newSuccessResult();
303303
// 兼容nosql,比如 elasticSearch-sql
304304
if(config.isElasticsearch()) {
305305
result.put(JSONResponse.KEY_COUNT, rs.getObject(1));
@@ -1276,17 +1276,24 @@ public PreparedStatement setArgument(@NotNull SQLConfig<T, M, L> config, @NotNul
12761276
}
12771277

12781278
protected Map<String, Connection> connectionMap = new HashMap<>();
1279+
public Map<String, Connection> getConnectionMap() {
1280+
if (connectionMap == null) {
1281+
connectionMap = new HashMap<>();
1282+
}
1283+
return connectionMap;
1284+
}
1285+
12791286
protected Connection connection;
12801287
@NotNull
12811288
@Override
12821289
public Connection getConnection(@NotNull SQLConfig<T, M, L> config) throws Exception {
12831290
String connectionKey = getConnectionKey(config);
1284-
connection = connectionMap.get(connectionKey);
1291+
connection = getConnectionMap().get(connectionKey);
12851292
if (connection == null || connection.isClosed()) {
12861293
Log.i(TAG, "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
12871294
// PostgreSQL 不允许 cross-database
12881295
connection = DriverManager.getConnection(config.gainDBUri(), config.gainDBAccount(), config.gainDBPassword());
1289-
connectionMap.put(connectionKey, connection);
1296+
getConnectionMap().put(connectionKey, connection);
12901297
}
12911298

12921299
// TDengine 驱动内部事务处理方法都是空实现,手动 commit 无效
@@ -1326,7 +1333,7 @@ public void begin(int transactionIsolation) throws SQLException {
13261333
// }
13271334

13281335
// 将所有连接设置隔离级别,且禁止自动提交,需要以下代码来 commit/rollback
1329-
Collection<Connection> connections = connectionMap.values();
1336+
Collection<Connection> connections = connectionMap == null ? null : connectionMap.values();
13301337
if (connections != null) {
13311338
for (Connection connection : connections) {
13321339
try {
@@ -1356,7 +1363,7 @@ public void rollback() throws SQLException {
13561363
// }
13571364

13581365
// 将所有连接进行回滚
1359-
Collection<Connection> connections = connectionMap.values();
1366+
Collection<Connection> connections = connectionMap == null ? null : connectionMap.values();
13601367
if (connections != null) {
13611368
for (Connection connection : connections) {
13621369
try {
@@ -1388,7 +1395,7 @@ public void rollback(Savepoint savepoint) throws SQLException {
13881395
// }
13891396

13901397
// 将所有连接进行回滚
1391-
Collection<Connection> connections = connectionMap.values();
1398+
Collection<Connection> connections = connectionMap == null ? null : connectionMap.values();
13921399
if (connections != null) {
13931400
for (Connection connection : connections) {
13941401
try {
@@ -1415,7 +1422,7 @@ public void commit() throws SQLException {
14151422
// }
14161423

14171424
// 将所有连接进行提交
1418-
Collection<Connection> connections = connectionMap.values();
1425+
Collection<Connection> connections = connectionMap == null ? null : connectionMap.values();
14191426
if (connections != null) {
14201427
for (Connection connection : connections) {
14211428
try {
@@ -1444,7 +1451,7 @@ public void close() {
14441451
cachedSQLCount = 0;
14451452
executedSQLCount = 0;
14461453

1447-
if (connectionMap == null) {
1454+
if (connectionMap == null || connectionMap.isEmpty()) {
14481455
return;
14491456
}
14501457

0 commit comments

Comments
 (0)