0

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

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
  • Start here: docs.oracle.com/javase/tutorial/essential/concurrency And then read "Java Concurrency In Practice, available from your favorite bookseller. It was written in 2006 but is still relevant and essential reading. Commented May 2, 2015 at 15:44

1 Answer 1

0

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"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to close the connections.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.