简体   繁体   中英

Accessing databases using different threads - java

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM