mavrav
mavrav

Reputation: 580

Db2 .SqlIntegrityConstraintViolationException: SQLCODE=-803, SQLSTATE=23505

I am reading from a table in Oracle and inserting the entire dump into Db2. The table structures are the same.I am using Simple scala class which does the above mentioned task. I have set the insert batchsize as 300. After a few batches gets updated, the class is throwing the below exception

com.ibm.db2.jcc.am.SqlIntegrityConstraintViolationException: Error for batch element #10: DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC=1;PME.TM_ASSET_LQA_DETL, DRIVER=4.13.127
   at com.ibm.db2.jcc.am.id.a(id.java:673) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.id.a(id.java:60) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.id.a(id.java:127) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.t4.cb.a(cb.java:481) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.t4.cb.a(cb.java:70) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.t4.q.a(q.java:57) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.t4.tb.a(tb.java:225) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.oo.a(oo.java:3434) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.oo.d(oo.java:5550) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.oo.a(oo.java:4992) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.oo.c(oo.java:4664) ~[db2jcc-4.13.127.jar:na]
    at com.ibm.db2.jcc.am.oo.executeBatch(oo.java:2934) ~[db2jcc-4.13.127.jar:na]
    at com.baml.regw.db.replicator.ReplicationRunnable$$anonfun$run$3.apply(SimpleReplicator.scala:105) ~[regw-db-replicator-0.0.933-SNAPSHOT.jar:na]
    at com.baml.regw.db.replicator.ReplicationRunnable$$anonfun$run$3.apply(SimpleReplicator.scala:80) ~[regw-db-replicator-0.0.933-SNAPSHOT.jar:na]

Since the exception was related to IntegrityConstraint I tried checking for presence of Composite primary key(ID+TimeStamp+9999-12-31 00.00.000000) but the combo is neither present in the Oracle table nor the Db2 table. The constraints on thE Db2 table are

 COLUMN NAME                         UNIQUE RULE
    +ID+BUSINESS_STOP+BUSINESS_START    Primary
    +ID                                 Duplicate
    +BUSINESS_START                     Duplicate
    +LOW_QUALITY_IND                    Duplicate
    +IDENTIFIER1                        Duplicate    
    +IDENTIFIER2                        Duplicate

I scoured through the other such issues in SO but none of the fixes worked for me. The code which is performing this task

    logger.info("Retrieving based on query string: " + queryStr + " for thread " + threadNum)
    val start = System.currentTimeMillis()
    val rs = stmt.executeQuery(queryStr)
    val rsMd = rs.getMetaData()
    val end = System.currentTimeMillis()
    logger.info("Query execution time: " + (end - start) + "ms.")

    done = true
    var stmtCount = 0
    Iterator.continually(rs).takeWhile(_.next()).foreach { rs =>
      if (sourceConf.hasPath("blockSize")) {
        done = false
      }

      //Subtract one to ignore the timestamp field that we are using
      for (idx <- 1 to (rsMd.getColumnCount()-extraColumnCount)) {
        try  {  
          logger.info("destStmt.setObject"+rs.getObject(idx)+" column Type "+ rsMd.getColumnType(idx))
          destStmt.setObject(idx, rs.getObject(idx), rsMd.getColumnType(idx))
        }
        catch  {
          case e:Exception => {
            logger.warn("While attempting to set (1-based) index: " + idx + 
                " to value of type " + {if(rs.getObject(idx) != null) rs.getObject(idx).getClass().getName() else "[NULL]"} + 
                " received error: " + e.getMessage())
            throw e
          }
        }
      }


      destStmt.addBatch()
      stmtCount += 1

      if(stmtCount % { if (destConf.hasPath("batchSize")) destConf.getInt("batchSize") else 200 } == 0)  {           
        destStmt.executeBatch()
        destDbConn.commit()
        destStmt.clearBatch()
        stmtCount = 0
      }
    }

    if(stmtCount > 0)  {
      destStmt.executeBatch()
      destDbConn.commit()
      destStmt.clearBatch()
    }
    rs.close()
    stmt.close()

Upvotes: 4

Views: 61583

Answers (1)

Beryllium
Beryllium

Reputation: 13008

  • Check if there is really the same integrity constraint in your source database (Oracle). Otherwise you might import rows which exist in your source table (because there isn't a constraint), but which can't be imported in the target table.

  • Check if the column indices are really the same in both tables (Oracle and DB2) from the perspective of JDBC. Other tools may sort by column name etc. Better yet: Use column names (iterate over the column names in the meta data). Your index-based approach won't work, if the columns are reordered which possibly happens, if columns are dropped and re-added etc. For example, if you have a select * from x and an insert into x values(...) the column order is relevant.

  • Check if you have null values in your source table in the columns which are part of the constraint. Possibly Oracle handles null values in the constraint differently than DB2, if it's backed by an index.

Upvotes: 2

Related Questions