1

I have data in a java object as data1, data2.

data1 and data2 together forms a composite key in myTable where I want to insert the object.

The writing is happening as a batch. Like 10 insert statements are prepared using 10 objects and are executed as a batch.

I want to insert the above data with the constraint: data1 + data2 should not already be present in myTable i.e. data1 + data2 should be unique --- if unique then write else just ignore.

The query I am using is:

Insert into mySchema.myTable(column1, column2)
  select 'abc', '123'
  from SYSIBM.DUAL
  where not exists
        ( select 1 
          from mySchema.myTable A 
          where 'abc' = A.column1 
            and '123' = A.column2
        )

Running above query independently for single set of data runs successfully.

However, while running in batch scenario I am getting "com.ibm.db2.jcc.b.ie: Non-atomic batch failure." error.

I think it has something to do with using SYSIBM.DUAL in batch scenario.

Code which is failing:

Insert Query:

Insert into mySchema.myTable(column1, column2)
select ?,? from SYSIBM.DUAL
where not exists (
  select 1 from mySchema.myTable A
  where ?=A.column1 and ?=A.column2)

Statement Setters:

ps.setString(1, item.getColumn1());
ps.setString(2, item.getColumn2());
ps.setString(3, item.getColumn1());
ps.setString(4, item.getColumn2());

where item is the java object holding the two columns to write.

Error is:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [Insert into mySchema.myTable(column1, column2) select ?,? from SYSIBM.DUAL where not exists (select 1 from mySchema.myTable A where ?=A.column1 and ?=A.column2)]; nested exception is com.ibm.db2.jcc.b.ie: Non-atomic batch failure. The batch was submitted, but at least one exception occurred on an individual member of the batch. Use getNextException() to retrieve the exceptions for specific batched elements.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Vicky
  • 16,679
  • 54
  • 139
  • 232
  • @ypercube: if you remember, above question is a follow up on http://stackoverflow.com/questions/7147219/how-to-use-not-exists-with-composite-keys-in-sql-for-inserting-data-from-pojo where you gave the answer which worked. – Vicky Sep 05 '11 at 10:53
  • An you add the code that fails? – ypercubeᵀᴹ Sep 05 '11 at 11:00
  • I don't think I can help in that. See this, perhaps it can help: http://www.ibm.com/developerworks/forums/thread.jspa?threadID=225410 – ypercubeᵀᴹ Sep 05 '11 at 11:17
  • You could also try to find what exceptions are raised with the `getNextException()` as it suggested in the Oracle forum: https://forums.oracle.com/forums/thread.jspa?messageID=7317260 – ypercubeᵀᴹ Sep 05 '11 at 11:20
  • @ypercube: ok.. will read and check.. thanks!! – Vicky Sep 05 '11 at 11:29
  • If you don't get an answer here in a day, you can post at the http://dba.stackexchange.com/ site. – ypercubeᵀᴹ Sep 05 '11 at 11:41
  • Is there a reason why you aren't coding this as a straight INSERT and using a standard exception handler to recover from a duplicate key error if one occurs? – Fred Sobotka Sep 07 '11 at 17:04

0 Answers0