• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple threads try to Insert into db  RSS feed

 
suresh selvarajj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I written the below code for to insert list of employee in to database. if I do executeBatch for every single addBatch() then working fine. but if I do executeBatch for every 5 addBatch() by using if(incr==5) then missing lot of entry in database. please help me to achive all the list to insert.

I'm new to this blog, If I did violated any blog rules please forgive me.

 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Tutotial on the Internet says that you should add all batches at first and then call executeBatch() to add all batches:

This code I found here.
It is not required to call
Class.forName("oracle.jdbc.driver.OracleDriver");
in jdbc4
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally it should work. Have you debug the code which entry are going missing?
Also see this thread, it might help.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
suresh selvarajj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This code related to multi thread if I do insert without using thread its working fine. but my requirement is to insert 3007 records in 5 tables thats why im looking for multi threading code. And one more doubt is maximum how many statement can add in addBatch().

@Volodymyr
if we can get the connection using below code, it will return which driver object(suppose I have oracle and sqlserver jar in classpath).
Connection connection = new getConnection();

Not good way to add all the records into batch and do execute batch in single stretch.

@Muhammad Khojaye
If I put break point then dead lock is occuring.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But your "connection" variable is static, so all the threads are using the same connection. That's why you're having the problem. They should all use different connections.

But you only have 3000 records to insert? In my opinion it isn't worth spending a lot of time to use multiple threads for such a small number of records. Just use one thread and get it done.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!