We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Batch queries execution issue

rajeevy69
rajeevy69 asked
on
Medium Priority
271 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..
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You need to handle BatchUpdateException properly - how are you doing that currently?

Author

Commented:

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

try
{
stmt.BatchExecute();
}
catch(SQLException e)
{
    e.printStackTrace();
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
In any case, you should probably be calling commit in the exception handler to ensure that successful updates are in fact committed
CERTIFIED EXPERT
Top Expert 2016

Commented:
.. or you could do that in a finally block

Author

Commented:
i m waiting for transaction to complete. i will let u knw in a while

Author

Commented:
hey it rolleback alll statements. there zero rows in db after completion
CERTIFIED EXPERT
Top Expert 2016

Commented:
See my last comment
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

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

Author

Commented:
i did commit but now only 3800 records are getting commited. Earlier count was 7000. Total number of records to update are 99000.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sounds like your driver might not continue on exception. What query are you executing?

Author

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
You might attach a compressed copy of your log file - it could be useful
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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







===================================================
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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

Author

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
depends on the queries you are using

have you turned off auto commit as I suggested earlier?

Author

Commented:
Yes i have switched off auto commit. Its not working for me.
CERTIFIED EXPERT
Top Expert 2016

Commented:
What are you doing now: executing a file import or doing a PreparedStatement insert?

Author

Commented:
i am doing statement insert. I could not understand file import. Can you pls send code sample for file import too.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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

Author

Commented:
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();
          }
      
      }
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
You need to see the comment i made at http:#24776270 - you're not doing that - in fact you're doing the opposite
      
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

Commented:
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.

Author

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Above things still does not resolve issue. I cant say that it is database issue. So please suggest something

Author

Commented:
No final solution

Author

Commented:
NA

Author

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.