简体   繁体   English

Teradata JDBC executeBatch错误处理

[英]Teradata JDBC executeBatch errorhandling

I am inserting data into a teradata table using executeBatch method. 我正在使用executeBatch方法将数据插入到Teradata表中。 Currently if one insert in the batch fails all the other inserts in the batch also fails and no records end up being inserted. 当前,如果批次中的一个插入失败,则批次中的所有其他插入也会失败,并且最终不会插入任何记录。 How can I change this behaviour to let the other inserts in the batch succeed if any inserts fails and the some ability to track the rejected records. 如果任何插入失败并且具有跟踪拒绝记录的某些功能,如何更改此行为以使批处理中的其他插入成功。

PS: I have ensured that TMODE is set to TERA and autocommit enabled. PS:我确保将TMODE设置为TERA并启用自动提交。

UPDATE: 更新:

target table definition. 目标表定义。

Class.forName("com.teradata.jdbc.TeraDriver");
val conn = DriverManager.getConnection("jdbc:teradata://teradata-server/mydb,tmode=TERA","username","password")
val insertSQL =  "INSERT INTO mydb.mytable VALUES (?,?)"
val stmt = conn.prepareStatement(insertSQL)


stmt.setInt(1,1)
stmt.setNull(2,Types.VARCHAR)  // Inserting Null here. This insert will fail
stmt.addBatch()

stmt.setInt(1,2)
stmt.setString(2,"XXX")
stmt.addBatch()

stmt.setInt(1,3)
stmt.setString(2,"YYY")
stmt.addBatch()

stmt.setInt(1,4)
stmt.setString(2,"ZZZ")
stmt.addBatch()

stmt.setInt(1,5)
stmt.setString(2,"ABC")
stmt.addBatch()

try {
val res = stmt.executeBatch()
println(res.mkString(","))
}
catch {
 case th: BatchUpdateException => {
        println(th.getUpdateCounts().mkString(","))
 }
}

Below is the sample scala code. 以下是示例Scala代码。 As you can see, this batch contains 5 insert statements. 如您所见,该批处理包含5个insert语句。 The First insert is set to fail because it is trying to insert null into an not null field (col2). 第一次插入设置为失败,因为它试图将null插入非null字段(col2)。 The other 4 inserts dont have any issues and should succeed. 其他4个插入没有任何问题,应该会成功。 But as you can see from below all 5 inserts in the batch failed. 但是正如您从下面看到的,该批次中的所有5个插入操作均失败。 Is there any way we can make other inserts succeed?. 有什么方法可以使其他插入成功? As stated above tmode is tera and autocommit is enabled. 如上所述,tmode是tera,并且启用了自动提交。 if there is no way other than re-submitting all failed queries individually then we would have to reduce the batch size and settle for lower throughput. 如果除了单独重新提交所有失败的查询之外别无选择,那么我们将不得不减小批处理大小并为较低的吞吐量做好准备。

 Class.forName("com.teradata.jdbc.TeraDriver"); val conn = DriverManager.getConnection("jdbc:teradata://teradata-server/mydb,tmode=TERA","username","password") val insertSQL = "INSERT INTO mydb.mytable VALUES (?,?)" val stmt = conn.prepareStatement(insertSQL) stmt.setInt(1,1) stmt.setNull(2,Types.VARCHAR) // Inserting Null here. This insert will fail stmt.addBatch() stmt.setInt(1,2) stmt.setString(2,"XXX") stmt.addBatch() stmt.setInt(1,3) stmt.setString(2,"YYY") stmt.addBatch() stmt.setInt(1,4) stmt.setString(2,"ZZZ") stmt.addBatch() stmt.setInt(1,5) stmt.setString(2,"ABC") stmt.addBatch() try { val res = stmt.executeBatch() println(res.mkString(",")) } catch { case th: BatchUpdateException => { println(th.getUpdateCounts().mkString(",")) } } 

Result 结果

-3,-3,-3,-3,-3 -3,-3,-3,-3,-3

This is from Teradata's JDBC manual : 这来自Teradata的JDBC手册

Beginning with Teradata Database 13.10 and Teradata JDBC Driver 13.00.00.16, PreparedStatement batch execution can return individual success and error conditions for each parameter set. 从Teradata数据库13.10和Teradata JDBC驱动程序13.00.00.16开始,PreparedStatement批处理执行可以为每个参数集返回单独的成功和错误条件。

An application using the PreparedStatement executeBatch method must have a catch-block for BatchUpdateException and the application must examine the error code returned by the BatchUpdateException getErrorCode method. 使用PreparedStatement executeBatch方法的应用程序必须具有BatchUpdateException的catch块,并且该应用程序必须检查BatchUpdateException getErrorCode方法返回的错误代码。

PreparedStatement BatchUpdateException Handling PreparedStatement BatchUpdateException处理

Execute a multi-statement request using a PreparedStatement batch request and demonstrates the handling of the PreparedStatement BatchUpdateException 使用PreparedStatement批处理请求执行多语句请求,并演示PreparedStatement BatchUpdateException的处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM