0

I ran the reader () method and reader2 () using two different threads and using the main thread.

After checking the times for the two cases, I realized that there were no significant differences.

My doubts: the access to the basics of oracle through JDBC data, it is only possible to make a query at a time?

Thank you.

public class ReadingTime {

public static void main(String[] args) throws InterruptedException {

    //reader("pessoas");
    //reader("pessoas_dw");

    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            reader("pessoas");
        }
    });

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            reader2("PESSOAS_DW");
        }
    });

    t1.start();
    t2.start();

    t1.join();
    t2.join();

}

public static void reader(String s){
    long tempoInicial = System.currentTimeMillis();
    MyConnection conn = new MyConnection("oracle", "localhost", "1521", "orcl", "username1", "password1");
    conn.openConnection();
    Statement st = conn.createStatement();
    ResultSet tabela1 = conn.executeQuery(st, "select * from "+s);
    int i = 1;
    try {
        while(tabela1.next()){
            i++;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Thread 1");
    System.out.println(i);
    long tempoFinal = System.currentTimeMillis();
    System.out.println( tempoFinal - tempoInicial );
    System.out.println("____________________________");

}

public static void reader2(String s){
    long tempoInicial = System.currentTimeMillis();
    MyConnection conn = new MyConnection("oracle", "localhost", "1521", "orcl", "username2", "password2");
    conn.openConnection();
    Statement st = conn.createStatement();
    ResultSet tabela1 = conn.executeQuery(st, "select * from "+s);
    int i = 1;
    try {
        while(tabela1.next()){
            i++;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Thread 2");
    System.out.println(i);
    long tempoFinal = System.currentTimeMillis();
    System.out.println( tempoFinal - tempoInicial );
    System.out.println("__________________________");

}
10
  • How long does the query take? If it is quite slow, then running two queries in parallel (assuming the database itself is up to it) could be faster. If it is very fast, you won't notice a difference.
    – Thilo
    Feb 4, 2016 at 2:49
  • Oracle can have as many simultaneous accesses to a database as there are connections. Oracle has multithreading mechanisms which allow you to specify concurrency policies for such environments. Moreover, RDBMS has mechanisms to 'retire' liveness failures should deadlocks occur. So, no ... Oracle and many other RDBMS with multithreading allow more than one query at a time.
    – scottb
    Feb 4, 2016 at 2:51
  • As per the "I" in the ACID principle, you can run as many concurrent queries as the database supports (depends on your DB), and the result should be consistent in the end.
    – Kon
    Feb 4, 2016 at 2:54
  • I dont think and it would not be true and correct statement, if you will say Oracle RDBMS wont allow to use it to achieve concurrency and parallel execution of query on same DB object. Until you apply some locking to control consistency.
    – Gautam
    Feb 4, 2016 at 2:55
  • @KaroLa: Oracle, and many other RDBMS servers, have concurrent access mechanisms built into the database software to control the integrity and consistency of its own data (these are the so-called ACID-compliant databases). The JDBC classes in Java, however, are not thread-safe, but the OP's two threads have each created their own connections and these connection objects are thread-confined. There is therefore no problem of thread-safety in his program as written.
    – scottb
    Feb 4, 2016 at 3:00

1 Answer 1

0

I'm gonna make a guess here. You don't close the resultset, nor the statement, nor the connection (particularly not closing the connection).

My guess is that the MyConnection (which we can't see) might be reusing the previous connection when it is on the same (main) thread.

So, assuming the connection establishment time is much greater than the resultset iteration, it would make sense.

Try to close the rs, st and conn to see if it makes a difference.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.