Solved

Batch queries execution issue

Posted on 2009-07-03
38
253 Views
Last Modified: 2012-05-07
Hi

I am tryning to batch execute a query set of 99000. But when i execute these as batch , it throws an exception after 4000 queries saying comma not found and dont execute rest of the queries.

I want that my rest of the queries should be executed. How to do it. All the falied queries should be listed in my logger but  these should not stop from executing other queries..
0
Comment
Question by:rajeevy69
  • 17
  • 12
  • 8
38 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to handle BatchUpdateException properly - how are you doing that currently?
0
 

Author Comment

by:rajeevy69
Comment Utility

for(i=0;i< queries.lenght;i++)
                 stmt.addBatch(queries[i])

try
{
stmt.BatchExecute();
}
catch(SQLException e)
{
    e.printStackTrace();
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Well, that's not really handling it at all ;-) You need to commit the ones that were successful. Something like

http://www.exampledepot.com/egs/java.sql/BatchUpdate.html?l=rel
0
 

Author Comment

by:rajeevy69
Comment Utility
From the code , it seems like it is either trying commit batch execute if all statements executed fine else rollback all ststements.

But i want to execute all statements in batch. Only thos that were not executed should be rolledbacked and inform me.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Only thos that were not executed should be rolledbacked

You don't need to roll back queries that don't execute - only ones that do.

Does your driver/db continue to process after a BatchUpdateException?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
In any case, you should probably be calling commit in the exception handler to ensure that successful updates are in fact committed
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
.. or you could do that in a finally block
0
 

Author Comment

by:rajeevy69
Comment Utility
i m waiting for transaction to complete. i will let u knw in a while
0
 

Author Comment

by:rajeevy69
Comment Utility
hey it rolleback alll statements. there zero rows in db after completion
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
See my last comment
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you need to disable auto commit otherwise they will all get rolled back.
And catch BatchUpdateException and handle your logging in there
0
 

Author Comment

by:rajeevy69
Comment Utility
i did commit but now only 3800 records are getting commited. Earlier count was 7000. Total number of records to update are 99000.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Sounds like your driver might not continue on exception. What query are you executing?
0
 

Author Comment

by:rajeevy69
Comment Utility
insert into table(column1, column2, column3) values('val1','val2','val3')

all queries are insert queries

Let me brief about history.

I have to write a utility to migrate data from logs to database table. Values in logs are comma separated. I m reading them and creating list of queries.

and than i am trying to insert them in database using batch update.

but it seems some of the values from logs were not read well and therefore creating sytax error.
But expect those queries, i want to insert all othr in database.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
use the update count array to determine which are failing

you may be better off not doing the updates via jdbc btw, and instead generte a script and execute that
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You might attach a compressed copy of your log file - it could be useful
0
 
LVL 92

Accepted Solution

by:
objects earned 84 total points
Comment Utility
> insert into table(column1, column2, column3) values('val1','val2','val3')

you're using a prepared statement to execute it aren't you, and placeholders for the values
0
 

Author Comment

by:rajeevy69
Comment Utility
i m not using prepared statement.

also can you pls explain how to create and execute script instead of jdbc.

CRHJ,

Sorry,I cant attach log bcs these are prod logs. but i can give u sample format

==================================================
12/31.2009, http://google.com, 123, ,
12/31.2009, http://google.com, 123, , 515
12/31.2009, http://google.com, 123, ,
12/31.2009, http://google.com, 123, , 515







===================================================
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 92

Expert Comment

by:objects
Comment Utility
> i m not using prepared statement.

you should be, at least for performance reasons.
are all the inserts going into same table?

> also can you pls explain how to create and execute script instead of jdbc.

output the inserts to a file, and execute it as an sql script
0
 

Author Comment

by:rajeevy69
Comment Utility
Can you pls give me some demo code for script.

Also please provide some way to execute all queries from batch execution even if some fails.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
depends on the queries you are using

have you turned off auto commit as I suggested earlier?
0
 

Author Comment

by:rajeevy69
Comment Utility
Yes i have switched off auto commit. Its not working for me.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
What are you doing now: executing a file import or doing a PreparedStatement insert?
0
 

Author Comment

by:rajeevy69
Comment Utility
i am doing statement insert. I could not understand file import. Can you pls send code sample for file import too.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Can you pls send code sample for file import too.

That's db specific and not really to do with Java.

Please post your current PreparedStatement code
0
 

Author Comment

by:rajeevy69
Comment Utility
public void performBatch(Connection conn, List queries) throws SQLException
      {
            String query;
            Statement stmt;
      
            
            try {
                  conn.setAutoCommit(false);
                      stmt = conn.createStatement();
        
            for (int i = 0; i < queries.size(); i++)
            {
                  stmt.addBatch((String)queries.get(i));
      
            }
          
              // Execute the batch
//              int [] updateCounts = stmt.executeBatch();
          
          
              // Since there were no errors, commit
              conn.commit();
          }
            catch (BatchUpdateException e)
            {
              // Not all of the statements were successfully executed
              int[] updateCounts = e.getUpdateCounts();
          
              // Some databases will continue to execute after one fails.
              // If so, updateCounts.length will equal the number of batched statements.
              // If not, updateCounts.length will equal the number of successfully executed statements
              processUpdateCounts(updateCounts);
          
              // Either commit the successfully executed statements or rollback the entire batch
              conn.rollback();
          }
            catch (SQLException e)
            {
                  System.out.println("Query Falied to execute:" + (String)queries.get(i));
          }
          finally
          {
                conn.commit();
          }
      
      }
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you rollback the entire batch by the looks, you want to commit the successful ones
and you shouldn't be committing in finally lock, its already been handled
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to see the comment i made at http:#24776270 - you're not doing that - in fact you're doing the opposite
      
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
though if you you always want commit() all the successful, then you would do the commit() in your finally block, but get rid of the other commits and rollbacks.
0
 

Author Comment

by:rajeevy69
Comment Utility
I removed rollback from catch and added commit in place of it than also same problem is occuring.

Also i removed commit from everywhere and tried keeping only in finally block, still same issue.

0
 

Author Comment

by:rajeevy69
Comment Utility
But if when i am executing queries individually instead of batch, most of the queries are getting executed successfully. But it is taking to much of time.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 84 total points
Comment Utility
have you tried breaking the batch down into smaller batchs. shouldn't need to but worth testing if thats the problem.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 41 total points
Comment Utility
If you have a driver/db that doesn't continue on error, there's not much you can do. You'll probably find that a non-batched PreparedStatement insert gets you there faster in the end
0
 

Author Comment

by:rajeevy69
Comment Utility
Above things still does not resolve issue. I cant say that it is database issue. So please suggest something
0
 

Author Comment

by:rajeevy69
Comment Utility
No final solution
0
 

Author Comment

by:rajeevy69
Comment Utility
NA
0
 

Author Closing Comment

by:rajeevy69
Comment Utility
Did not found solution of the issue. Only came to know the limitation but I still think that there should be some way to solve this issue
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now