当前位置: 首页 > 知识库问答 >
问题:

Db2.SQLintegrityConstraintViolationException:SQLCode=-803,SQLState=23505

呼延沈义
2023-03-14

我正在读取Oracle中的一个表,并将整个转储插入到DB2中。我使用简单的scala类来完成上面提到的任务。我已将插入批大小设置为300。在更新了几个批处理之后,类将抛出以下异常

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]

由于该异常与IntegrityConstraint相关,我尝试检查是否存在复合主键(ID+TimeStamp+9999-12-31 00.00.000000),但Oracle表和Db2表中都不存在组合键。Db2表的约束条件如下

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

我在SO中搜索了其他这样的问题,但没有一个修复对我有效。执行此任务的代码

    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()

共有1个答案

陈昂熙
2023-03-14

>

  • 检查源数据库(Oracle)中是否真的存在相同的完整性约束。否则,您可能会导入源表中存在的行(因为没有约束),但目标表中无法导入的行。

    从JDBC的角度检查两个表(Oracle和DB2)中的列索引是否真的相同。其他工具可以按列名等排序,更好的方法是:使用列名(迭代元数据中的列名)。如果对列重新排序(这可能发生),如果删除和重新添加列等,那么基于索引的方法将不起作用。例如,如果您有SELECT*from xINSERT into x values(...)列顺序是相关的。

    检查源表中作为约束一部分的列中是否有null值。如果Oracle由索引支持,那么它处理约束中的null值的方式可能与DB2不同。

  •  类似资料: