Java 使应用程序多线程,同时独立地在四个不同的数据库中启动查询
我有下面的程序通过java连接到oracle数据库,我在其中使用了瘦驱动程序,现在的问题是,我必须同时在三个不同的数据库中执行相同的查询,因此我希望我的应用程序是多线程的,其中将触发相同的sql查询,但每个数据库将有一个单独的线程,每个线程从一开始就有责任建立与数据库的连接并在数据库中启动查询,因此总共有四个不同的数据库,总共有四个线程,请建议如何实现这一点。下面是程序im,我现在在一个数据库中启动简单查询Java 使应用程序多线程,同时独立地在四个不同的数据库中启动查询,java,jdbc,Java,Jdbc,我有下面的程序通过java连接到oracle数据库,我在其中使用了瘦驱动程序,现在的问题是,我必须同时在三个不同的数据库中执行相同的查询,因此我希望我的应用程序是多线程的,其中将触发相同的sql查询,但每个数据库将有一个单独的线程,每个线程从一开始就有责任建立与数据库的连接并在数据库中启动查询,因此总共有四个不同的数据库,总共有四个线程,请建议如何实现这一点。下面是程序im,我现在在一个数据库中启动简单查询 public class OracleJdbcExample { public
public class OracleJdbcExample {
public static void main(String args[]) throws SQLException {
String url = "jdbc:oracle:thin:@localhost:1632:DEVROOT32";
//properties for creating connection to Oracle database
Properties props = new Properties();
props.setProperty("user", "scott");
props.setProperty("password", "tiger");
Connection conn = DriverManager.getConnection(url,props);
String sql ="select sysdate as current_day from dual";
PreparedStatement preStatement = conn.prepareStatement(sql);
ResultSet result = preStatement.executeQuery();
while(result.next()){
System.out.println("Current Date from Oracle : " + result.getString("current_day"));
}
System.out.println("done");
}
}
这是未经测试的。我只是把你的代码,做了一个并发版本 您可能希望实际更改的部分是所使用的查询字符串
public class OracleJdbcExample {
static class DatabaseQuery implements Callable<ResultSet>
{
private final Supplier<Connection> connectionSupplier;
private final String query;
DatabaseQuery(Supplier<Connection> connectionSupplier, String query)
{
//Check for nulls
Preconditions.checkNotNull(connectionSupplier);
Preconditions.checkArgument(!Strings.isNullOrEmpty(query));
this.connectionSupplier = connectionSupplier;
this.query = query;
}
ResultSet call() throws Exception
{
//The concurrect part is here.
Connection connection = connectionSupplier.get();
PreparedStatement statement = connection.prepareStatement(query);
return statement.execute();
}
}
public static void main(String args[]) throws Exception
{
String url = "jdbc:oracle:thin:@localhost:1632:DEVROOT32";
//properties for creating connection to Oracle database
Properties props = new Properties();
props.setProperty("user", "scott");
props.setProperty("password", "tiger");
//Use a Connection Provider for on-demand connection creation.
Supplier<Connection> connectionSupplier = () -> DriverManager.getConnection(url, props);
String sql ="select sysdate as current_day from dual";
//However many concurrent operations you want
int desiredThreads = 4;
ExecutorService executor = Executores.newFixedThreadPool(desiredThreads);
List<Callable<ResultSet>> queries = new ArrayList<>();
for(int i = 0; i < desiredThreads; ++i)
{
//Create a separately configured DatabaseQuery
queries.add(new DatabaseQuery(connectionSupplier, query));
}
//Launch operations and get references to the
List<Future<ResultSet>> operations = executor.invokeAll(queries);
for(Future<ResultSet> operation : operations)
{
//Calling get on the Future causes the current thread to block until the operation is done.
ResultSet result = operation.get();
while(result.next())
{
System.out.println("Current Date from Oracle : " + result.getString("current_day"));
}
}
System.out.println("Done");
}
}
公共类OracleJdbcExample{
静态类DatabaseQuery实现可调用
{
私人最终供应商连接供应商;
私有最终字符串查询;
数据库查询(供应商连接供应商,字符串查询)
{
//检查空值
先决条件。checkNotNull(连接供应商);
前提条件.checkArgument(!Strings.isNullOrEmpty(query));
this.connectionSupplier=连接供应商;
this.query=query;
}
ResultSet call()引发异常
{
//concurrect部分在这里。
Connection=connectionSupplier.get();
PreparedStatement=connection.prepareStatement(查询);
return语句。execute();
}
}
公共静态void main(字符串args[])引发异常
{
String url=“jdbc:oracle:thin:@localhost:1632:DEVROOT32”;
//用于创建到Oracle数据库的连接的属性
Properties props=新属性();
props.setProperty(“用户”、“斯科特”);
props.setProperty(“密码”、“老虎”);
//使用连接提供程序按需创建连接。
供应商连接供应商=()->DriverManager.getConnection(url,props);
String sql=“从dual中选择sysdate作为当前日”;
//无论您需要多少并发操作
int desiredThreads=4;
ExecutorService executor=Executors.newFixedThreadPool(desiredThreads);
列表查询=新建ArrayList();
for(int i=0;i
从这里开始:然后阅读“Java并发实践”,可从您最喜欢的书商处获得。该书编写于2006年,但仍然是相关的基本读物。别忘了关闭连接。