Skip to content

Commit ba36595

Browse files
committed
恢复不影响启动的 Druid 相关代码
1 parent b3ec484 commit ba36595

File tree

2 files changed

+144
-144
lines changed

2 files changed

+144
-144
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@
272272
<!-- </dependency>-->
273273

274274
<!-- 需要用的 Druid 数据库连接池库,1.0.29 以上 -->
275-
<!-- <dependency>-->
276-
<!-- <groupId>com.alibaba</groupId>-->
277-
<!-- <artifactId>druid</artifactId>-->
278-
<!-- <version>1.2.9</version>-->
279-
<!-- </dependency>-->
275+
<dependency>
276+
<groupId>com.alibaba</groupId>
277+
<artifactId>druid</artifactId>
278+
<version>1.2.9</version>
279+
</dependency>
280280

281281
<!-- <dependency>-->
282282
<!-- <groupId>redis.clients</groupId>-->

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

Lines changed: 139 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//import apijson.milvus.MilvusUtil;
2626
//import apijson.mongodb.MongoUtil;
2727
import apijson.orm.SQLConfig;
28-
//import com.alibaba.druid.pool.DruidDataSource;
28+
import com.alibaba.druid.pool.DruidDataSource;
2929
import com.alibaba.fastjson.JSONObject;
3030
//import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
3131
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@@ -124,155 +124,155 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
124124
public static final String DATABASE_NEBULA = "NEBULA";
125125

126126
// 适配连接池,如果这里能拿到连接池的有效 Connection,则 SQLConfig<Long> 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
127-
// @Override
128-
// public Connection getConnection(SQLConfig<Long> config) throws Exception {
129-
//// if (DATABASE_NEBULA.equals(config.getDatabase())) { // 3.0.0 及以下要这样连接
130-
//// String uri = config.getDBUri();
131-
////
132-
//// int start = uri.indexOf("://");
133-
//// String prefix = uri.substring(0, start);
134-
////
135-
//// uri = uri.substring(start + "://".length());
136-
//// int end = uri.indexOf("/");
137-
//// String space = uri.substring(end + 1);
138-
////
139-
//// Properties props = new Properties();
140-
//// props.put("url", prefix + "://" + space);
141-
//// props.put("graphSpace", space);
142-
////
143-
//// NebulaDriver driver = new NebulaDriver(uri.substring(0, end));
144-
//// return driver.connect(prefix + "://" + space, props);
145-
//// // return DriverManager.getConnection("jdbc:nebula://JDBC_TEST_SPACE", "root", "nebula");
146-
//// }
127+
@Override
128+
public Connection getConnection(SQLConfig<Long> config) throws Exception {
129+
// if (DATABASE_NEBULA.equals(config.getDatabase())) { // 3.0.0 及以下要这样连接
130+
// String uri = config.getDBUri();
147131
//
148-
// String datasource = config.getDatasource();
149-
// Log.d(TAG, "getConnection config.getDatasource() = " + datasource);
132+
// int start = uri.indexOf("://");
133+
// String prefix = uri.substring(0, start);
150134
//
151-
// String key = datasource + "-" + config.getDatabase();
152-
// Connection c = connectionMap.get(key);
153-
// if (datasource != null && (c == null || c.isClosed())) {
154-
// try {
155-
// DataSource ds;
156-
// switch (datasource) {
157-
//// case "HIKARICP":
158-
//// ds = DemoApplication.getApplicationContext().getBean(HikariDataSource.class);
159-
//// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_HIKARICP 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_HIKARICP.getConnection();
160-
//// break;
161-
// default:
162-
// Map<String, DruidDataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DruidDataSource.class);
163-
// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_DRUID 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_DRUID.getConnection();
164-
// switch (datasource) {
165-
// case "DRUID-TEST":
166-
// ds = dsMap.get("druidTestDataSource");
167-
// break;
168-
// case "DRUID-ONLINE":
169-
// ds = dsMap.get("druidOnlineDataSource");
170-
// break;
171-
// case "DRUID":
172-
// ds = dsMap.get("druidDataSource");
173-
// break;
174-
// default:
175-
// ds = null;
176-
// break;
177-
// }
135+
// uri = uri.substring(start + "://".length());
136+
// int end = uri.indexOf("/");
137+
// String space = uri.substring(end + 1);
138+
//
139+
// Properties props = new Properties();
140+
// props.put("url", prefix + "://" + space);
141+
// props.put("graphSpace", space);
142+
//
143+
// NebulaDriver driver = new NebulaDriver(uri.substring(0, end));
144+
// return driver.connect(prefix + "://" + space, props);
145+
// // return DriverManager.getConnection("jdbc:nebula://JDBC_TEST_SPACE", "root", "nebula");
146+
// }
147+
148+
String datasource = config.getDatasource();
149+
Log.d(TAG, "getConnection config.getDatasource() = " + datasource);
150+
151+
String key = datasource + "-" + config.getDatabase();
152+
Connection c = connectionMap.get(key);
153+
if (datasource != null && (c == null || c.isClosed())) {
154+
try {
155+
DataSource ds;
156+
switch (datasource) {
157+
// case "HIKARICP":
158+
// ds = DemoApplication.getApplicationContext().getBean(HikariDataSource.class);
159+
// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_HIKARICP 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_HIKARICP.getConnection();
178160
// break;
161+
default:
162+
Map<String, DruidDataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DruidDataSource.class);
163+
// 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_DRUID 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_DRUID.getConnection();
164+
switch (datasource) {
165+
case "DRUID-TEST":
166+
ds = dsMap.get("druidTestDataSource");
167+
break;
168+
case "DRUID-ONLINE":
169+
ds = dsMap.get("druidOnlineDataSource");
170+
break;
171+
case "DRUID":
172+
ds = dsMap.get("druidDataSource");
173+
break;
174+
default:
175+
ds = null;
176+
break;
177+
}
178+
break;
179+
}
180+
181+
connectionMap.put(key, ds == null ? null : ds.getConnection());
182+
} catch (Exception e) {
183+
Log.e(TAG, "getConnection try { "
184+
+ "DataSource ds = DemoApplication.getApplicationContext().getBean(DataSource.class); .."
185+
+ "} catch (Exception e) = " + e.getMessage());
186+
}
187+
}
188+
189+
// 必须最后执行 super 方法,因为里面还有事务相关处理。
190+
// 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表
191+
return super.getConnection(config);
192+
}
193+
194+
@Override
195+
public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
196+
boolean isMilvus = DATABASE_MILVUS.equals(config.getDatabase()); // APIJSON 6.4.0+ 可用 config.isMilvus();
197+
boolean isCassandra = config.isCassandra();
198+
boolean isInfluxDB = config.isInfluxDB();
199+
200+
if (isMilvus || isCassandra || isInfluxDB) {
201+
// TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数
202+
String sql = config.getSQL(false); // config.isPrepared());
203+
if (sql != null && config.getMethod() == null) {
204+
String trimmedSQL = sql.trim();
205+
String sqlPrefix = trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
206+
if (sqlPrefix.startsWith("INSERT ")) {
207+
config.setMethod(RequestMethod.POST);
208+
}
209+
else if (sqlPrefix.startsWith("UPDATE ")) {
210+
config.setMethod(RequestMethod.PUT);
211+
}
212+
else if (sqlPrefix.startsWith("DELETE ")) {
213+
config.setMethod(RequestMethod.DELETE);
214+
}
215+
}
216+
217+
boolean isWrite = ! RequestMethod.isQueryMethod(config.getMethod());
218+
219+
List<JSONObject> cache = isWrite ? null : getCache(sql, config);
220+
int position = config.getPosition();
221+
JSONObject result = getCacheItem(cache, position, config);
222+
if (result != null) {
223+
if (position == 0 && cache != null && cache.size() > 1) {
224+
result.put(KEY_RAW_LIST, cache);
225+
}
226+
return result;
227+
}
228+
229+
230+
List<JSONObject> resultList = new ArrayList<>();
231+
232+
// if (isMilvus) {
233+
// if (isWrite) {
234+
// return MilvusUtil.executeUpdate(config, sql);
179235
// }
180236
//
181-
// connectionMap.put(key, ds == null ? null : ds.getConnection());
182-
// } catch (Exception e) {
183-
// Log.e(TAG, "getConnection try { "
184-
// + "DataSource ds = DemoApplication.getApplicationContext().getBean(DataSource.class); .."
185-
// + "} catch (Exception e) = " + e.getMessage());
237+
// resultList = MilvusUtil.executeQuery(config, sql, unknownType);
186238
// }
187-
// }
188-
//
189-
// // 必须最后执行 super 方法,因为里面还有事务相关处理。
190-
// // 如果这里是 return c,则会导致 增删改 多个对象时只有第一个会 commit,即只有第一个对象成功插入数据库表
191-
// return super.getConnection(config);
192-
// }
193-
//
194-
// @Override
195-
// public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
196-
// boolean isMilvus = DATABASE_MILVUS.equals(config.getDatabase()); // APIJSON 6.4.0+ 可用 config.isMilvus();
197-
// boolean isCassandra = config.isCassandra();
198-
// boolean isInfluxDB = config.isInfluxDB();
199-
//
200-
// if (isMilvus || isCassandra || isInfluxDB) {
201-
// // TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数
202-
// String sql = config.getSQL(false); // config.isPrepared());
203-
// if (sql != null && config.getMethod() == null) {
204-
// String trimmedSQL = sql.trim();
205-
// String sqlPrefix = trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
206-
// if (sqlPrefix.startsWith("INSERT ")) {
207-
// config.setMethod(RequestMethod.POST);
208-
// }
209-
// else if (sqlPrefix.startsWith("UPDATE ")) {
210-
// config.setMethod(RequestMethod.PUT);
239+
// else if (isCassandra) {
240+
// if (isWrite) {
241+
// return CassandraUtil.executeUpdate(config, sql);
211242
// }
212-
// else if (sqlPrefix.startsWith("DELETE ")) {
213-
// config.setMethod(RequestMethod.DELETE);
214-
// }
215-
// }
216-
//
217-
// boolean isWrite = ! RequestMethod.isQueryMethod(config.getMethod());
218243
//
219-
// List<JSONObject> cache = isWrite ? null : getCache(sql, config);
220-
// int position = config.getPosition();
221-
// JSONObject result = getCacheItem(cache, position, config);
222-
// if (result != null) {
223-
// if (position == 0 && cache != null && cache.size() > 1) {
224-
// result.put(KEY_RAW_LIST, cache);
225-
// }
226-
// return result;
244+
// resultList = CassandraUtil.executeQuery(config, sql, unknownType);
227245
// }
246+
// else if (isInfluxDB) {
247+
// if (isWrite) {
248+
// return InfluxDBUtil.executeUpdate(config, sql);
249+
// }
228250
//
229-
//
230-
// List<JSONObject> resultList = new ArrayList<>();
231-
//
232-
//// if (isMilvus) {
233-
//// if (isWrite) {
234-
//// return MilvusUtil.executeUpdate(config, sql);
235-
//// }
236-
////
237-
//// resultList = MilvusUtil.executeQuery(config, sql, unknownType);
238-
//// }
239-
//// else if (isCassandra) {
240-
//// if (isWrite) {
241-
//// return CassandraUtil.executeUpdate(config, sql);
242-
//// }
243-
////
244-
//// resultList = CassandraUtil.executeQuery(config, sql, unknownType);
245-
//// }
246-
//// else if (isInfluxDB) {
247-
//// if (isWrite) {
248-
//// return InfluxDBUtil.executeUpdate(config, sql);
249-
//// }
250-
////
251-
//// resultList = InfluxDBUtil.executeQuery(config, sql, unknownType);
252-
//// }
253-
//
254-
// // TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数
255-
// result = resultList.isEmpty() ? new JSONObject() : resultList.get(0);
256-
// if (resultList.size() > 1) {
257-
// result.put(KEY_RAW_LIST, resultList);
251+
// resultList = InfluxDBUtil.executeQuery(config, sql, unknownType);
258252
// }
259-
//
260-
// putCache(sql, resultList, config);
261-
//
262-
// return result;
263-
// }
264-
//
265-
// return super.execute(config, unknownType);
266-
// }
267-
//
268-
// @Override
269-
// public void close() {
270-
// super.close();
271-
//
272-
//// MilvusUtil.closeAllClient();
273-
//// CassandraUtil.closeAllSession();
274-
//// InfluxDBUtil.closeAllClient();
275-
// }
253+
254+
// TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数
255+
result = resultList.isEmpty() ? new JSONObject() : resultList.get(0);
256+
if (resultList.size() > 1) {
257+
result.put(KEY_RAW_LIST, resultList);
258+
}
259+
260+
putCache(sql, resultList, config);
261+
262+
return result;
263+
}
264+
265+
return super.execute(config, unknownType);
266+
}
267+
268+
@Override
269+
public void close() {
270+
super.close();
271+
272+
// MilvusUtil.closeAllClient();
273+
// CassandraUtil.closeAllSession();
274+
// InfluxDBUtil.closeAllClient();
275+
}
276276

277277
// 不需要隐藏字段这个功能时,取消注释来提升性能
278278
// @Override

0 commit comments

Comments
 (0)