0
我有一个应用程序运行通过多个数据库,并为每个数据库运行所有表上的select查询并将其转储到hadoop。jdbc连接池使用ThreadpoolExecutor在春季启动
我的设计是一次创建一个数据源连接,并使用获取的连接池在多个线程中运行选择查询。完成此数据源后,关闭连接并创建一个新连接。
这里是异步代码
@Component
public class MySampleService {
private final static Logger LOGGER = Logger
.getLogger(MySampleService.class);
@Async
public Future<String> callAsync(JdbcTemplate template, String query) throws InterruptedException {
try {
jdbcTemplate.query(query);
//process the results
return new AsyncResult<String>("success");
}
catch (Exception ex){
return new AsyncResult<String>("failed");
}
}
这里被呼叫者
public String taskExecutor() throws InterruptedException, ExecutionException {
Future<String> asyncResult1 = mySampleService.callAsync(jdbcTemplate,query1);
Future<String> asyncResult2 = mySampleService.callAsync(jdbcTemplate,query2);
Future<String> asyncResult3 = mySampleService.callAsync(jdbcTemplate,query3);
Future<String> asyncResult4 = mySampleService.callAsync(jdbcTemplate,query4);
LOGGER.info(asyncResult1.get());
LOGGER.info(asyncResult2.get());
LOGGER.info(asyncResult3.get());
LOGGER.info(asyncResult4.get());
//now all threads finished, close the connection
jdbcTemplate.getConnection().close();
}
我想知道这是否是做或做任何退出/优化的解决方案一个正确的方式说出来箱我错过了。由于我的查询很复杂,我无法使用spring-data-jpa。
由于
我想你误解了我的问题。我知道我们获得了数据源。但我的问题是使用这个池数据源来处理当前多个表。 –