JDBC - 连接多个数据库 [英] JDBC - Connect Multiple Databases

查看:84
本文介绍了JDBC - 连接多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中我需要连接N个数据库系统[N范围在1到350之间的任何范围。]



这个想法是 - 用户将被呈现数据库列表,并且将被要求从列表中选择任何或所有数据库。



选择数据库后,我需要连接到每个数据库并执行存储过程。



我计划使用纯旧的JDBC,并获得每一个连接(或通过在多个线程中运行它们)并执行存储过程并关闭连接。



所有这些都应该发生在事务中。



如果没有JDBC ...任何其他高效的方法?



更新 -



存储过程实际上涉及运行一些sql - 例如更新列,授予用户等权限。


<我会创建一个线程池有合理的最大线程数,十到二十线程之间,可以帮助 Executors#newFixedThreadPool c $ c> ,并调用单独的DB连接和SP执行任务,每个作为 Callable 使用 ExecutorService#invokeAll()



每个可调用实现应该将连接详细信息和SP名称作为构造函数参数,以便可以为不同的DB调用重用相同的实现。






strong>更新:确定,这是一个web应用程序。你不想浪费线程。如果它应该由单个并发用户使用,那么你应该确保线程池正确 shutdown 在请求结束或会话最高结束时。但是如果它应该被多个并发用户使用,那么你想在应用程序范围内共享线程池。在这里,你需要确保它正确关闭时,webapp关闭。 ServletContextListener 在这里很有用。


I am working on an application where I need to connect N number of database systems [N ranges any where between 1 to 350].

The idea is - the user will be presented with a list of databases and will be asked to select any or all of the databases from the list.

Once the databases are selected, I need to connect to each of the database and execute a stored procedure.

I am planning to use plain old JDBC and obtain connection for each of them one a time [or by running them in multiple threads] and execute the store procedure and close the connection.

And all this should happen in a transaction. What is the best way to do this?

If not JDBC...any other efficient way?

Update -

The stored procedure is actually involved in running some sql - for example updating a column, grant permission for a user etc.

解决方案

I'd create a threadpool with a reasonable maximum amount of threads, between ten and twenty threads maybe, with help of Executors#newFixedThreadPool() and invoke the separate DB connecting and SP executing tasks each as a Callable using ExecutorService#invokeAll(). You would like to play with the threadcount and profile which yields the best performance after all.

Each Callable implementation should take the connection details and SP name as constructor argument so that you can reuse the same implementation for different DB calls.


Update: OK, it's a webapplication. You don't want to waste threads. If it is supposed to be used by a single concurrent user, then you should really ensure that the threadpool is properly shutdown at end of request or at highest end of session. But if it is supposed to be used by multiple concurrent users, then you'd like to share the threadpool in the application scope. Also here, you need to ensure that it is properly shutdown when the webapp shuts down. The ServletContextListener is useful here.

这篇关于JDBC - 连接多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆