Link to home
Start Free TrialLog in
Avatar of rajeevy69
rajeevy69

asked on

Batch queries execution issue

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..
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to handle BatchUpdateException properly - how are you doing that currently?
Avatar of rajeevy69
rajeevy69

ASKER


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

try
{
stmt.BatchExecute();
}
catch(SQLException e)
{
    e.printStackTrace();
}
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
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.
>>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?
In any case, you should probably be calling commit in the exception handler to ensure that successful updates are in fact committed
.. or you could do that in a finally block
i m waiting for transaction to complete. i will let u knw in a while
hey it rolleback alll statements. there zero rows in db after completion
See my last comment
you need to disable auto commit otherwise they will all get rolled back.
And catch BatchUpdateException and handle your logging in there
i did commit but now only 3800 records are getting commited. Earlier count was 7000. Total number of records to update are 99000.
Sounds like your driver might not continue on exception. What query are you executing?
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.
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
You might attach a compressed copy of your log file - it could be useful
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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







===================================================
> 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
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.
depends on the queries you are using

have you turned off auto commit as I suggested earlier?
Yes i have switched off auto commit. Its not working for me.
What are you doing now: executing a file import or doing a PreparedStatement insert?
i am doing statement insert. I could not understand file import. Can you pls send code sample for file import too.
>>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
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();
          }
      
      }
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
You need to see the comment i made at http:#24776270 - you're not doing that - in fact you're doing the opposite
      
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.
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.

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Above things still does not resolve issue. I cant say that it is database issue. So please suggest something
No final solution
NA
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