【问题标题】:making application multithreaded and firing the query in four different database at the same time independently使应用程序多线程并独立地同时在四个不同的数据库中触发查询
【发布时间】:2015-05-02 15:13:09
【问题描述】:

我有下面的程序通过java连接到oracle数据库,我在其中使用了瘦驱动程序,现在的查询是我必须同时执行三个不同的数据库中的相同查询,所以我正在寻找我的应用程序是多线程的,其中将触发相同的 sql 查询,但每个数据库都有一个单独的线程,每个线程从一开始就负责建立与数据库的连接并在数据库中触发查询所以总共有四个不同的数据库,总共有四个线程,请告知如何实现这一点..下面是我现在在一个数据库中触发简单查询的程序

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");

    }
}

【问题讨论】:

标签: java jdbc


【解决方案1】:

这是未经测试的。我刚刚拿走了你的代码,并制作了它的并发版本。

您可能想要实际改变的部分是使用的查询字符串。

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");

    }
}

【讨论】:

  • 别忘了关闭连接。
猜你喜欢
  • 2014-12-07
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多