使应用程序多线程并在四个不同的数据库中同时独立地触发查询

时间:2022-11-23 22:53:25

I have the below program to connect to oracle database through java in which i have use the thin driver , now the query is that i have to execute the same query in total three different databases more at the same time so i am looking that my application is multi threaded in which the same sql query will be fired but there will be a separate thread for each database and each thread will be have a responsibility from the beginning onwards of establishing the connection with database and firing the query in database so total there are four different database and there will be total four threads , please advise how to achieve this..below is the program im which rite now i am firing the simple query in one databse itself

我有以下程序通过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");

    }
}

1 个解决方案

#1


This is untested. I just took your code, and made a concurrent version of it.

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

The part you might want to actually vary is the query string used.

您可能希望实际变化的部分是使用的查询字符串。

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

    }
}

#1


This is untested. I just took your code, and made a concurrent version of it.

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

The part you might want to actually vary is the query string used.

您可能希望实际变化的部分是使用的查询字符串。

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

    }
}