Skip to content

Commit 9b0b993

Browse files
authored
解决依赖 APIJSON 4.7.2 + 时对接不上连接池
1 parent 4d029c7 commit 9b0b993

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLExecutor.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import apijson.Log;
2626
import apijson.boot.DemoApplication;
2727
import apijson.framework.APIJSONSQLExecutor;
28-
import apijson.orm.SQLConfig;
28+
import apijson.orm.SQLConfig;
2929

3030

31-
/**SQL 执行
31+
/**SQL 执行
3232
* @author Lemon
3333
*/
3434
public class DemoSQLExecutor extends APIJSONSQLExecutor {
@@ -48,15 +48,16 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor {
4848
// super.removeCache(sql, type);
4949
// }
5050

51-
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
52-
@Override
51+
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
52+
@Override
5353
public Connection getConnection(SQLConfig config) throws Exception {
5454
String datasource = config.getDatasource();
5555
Log.d(TAG, "getConnection config.getDatasource() = " + datasource);
56-
57-
Connection c = connectionMap.get(datasource);
58-
if (datasource != null && (c == null || c.isClosed())) {
59-
try {
56+
57+
String key = datasource + "-" + config.getDatabase();
58+
Connection c = connectionMap.get(key);
59+
if (datasource != null && (c == null || c.isClosed())) {
60+
try {
6061
DataSource ds;
6162
switch (datasource) {
6263
case "HIKARICP":
@@ -80,18 +81,18 @@ public Connection getConnection(SQLConfig config) throws Exception {
8081
}
8182
break;
8283
}
83-
84-
connectionMap.put(datasource, ds == null ? null : ds.getConnection());
85-
} catch (Exception e) {
86-
Log.e(TAG, "getConnection try { "
87-
+ "DataSource ds = DemoApplication.getApplicationContext().getBean(DataSource.class); .."
88-
+ "} catch (Exception e) = " + e.getMessage());
89-
}
90-
}
91-
92-
// 必须最后执行 super 方法,因为里面还有事务相关处理。
93-
// 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表
94-
return super.getConnection(config);
84+
85+
connectionMap.put(key, ds == null ? null : ds.getConnection());
86+
} catch (Exception e) {
87+
Log.e(TAG, "getConnection try { "
88+
+ "DataSource ds = DemoApplication.getApplicationContext().getBean(DataSource.class); .."
89+
+ "} catch (Exception e) = " + e.getMessage());
90+
}
91+
}
92+
93+
// 必须最后执行 super 方法,因为里面还有事务相关处理。
94+
// 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表
95+
return super.getConnection(config);
9596
}
9697

9798

@@ -100,6 +101,6 @@ public Connection getConnection(SQLConfig config) throws Exception {
100101
// protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
101102
// int columnIndex, Map<String, JSONObject> childMap) throws Exception {
102103
// return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod());
103-
// }
104-
104+
// }
105+
105106
}

0 commit comments

Comments
 (0)