Ini Koq Apah
Ini Koq Apah

Reputation: 75

JDBC MySQL multiple connection, which one is the best?

I have thread that use mysql connection. That thread will be multiply so I will have multi thread. This is summary of my thread.

public class t extends Thread{
...
run() {
// i will do database update here
}
...
}

I will have about 10.000 records and this records will be processed by 5 threads. So if update process finished, the thread will be re-used. I have 3 options for my problem :

  1. create single connection and use it in every thread
  2. for every thread, I create new connection (in the thread), and close it if thread finished.
  3. create multiple-connection for each thread (outside the thread) and give it to thread when needed

Which one is faster?

Upvotes: 0

Views: 570

Answers (2)

Leos Literak
Leos Literak

Reputation: 9394

I think that each thread having own JDBC connection is best from performance reasons, because there is no synchronization overhead.

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32535

I would go for using some ORM/JPA implementation like Hibernate and/or use some kind of connections pool for automatic connection management.

Upvotes: 0

Related Questions