Teradata JDBC executeBatch 错误处理
Teradata JDBC executeBatch errorhandling
我正在使用 executeBatch 方法将数据插入 teradata table。目前,如果批处理中的一个插入失败,则批处理中的所有其他插入也会失败,最终不会插入任何记录。如果任何插入失败,我如何更改此行为以使批处理中的其他插入成功,并且具有跟踪被拒绝记录的某些能力。
PS:我已确保将 TMODE 设置为 TERA 并启用自动提交。
更新:
目标table定义。
CREATE SET TABLE mydb.mytable ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
col1 INTEGER,
col2 VARCHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL)
PRIMARY INDEX ( col1 );
下面是示例 Scala 代码。如您所见,该批处理包含 5 个插入语句。 First insert 设置为失败,因为它试图将 null 插入到非 null 字段 (col2) 中。其他 4 个插入没有任何问题,应该会成功。但是正如您从下面看到的那样,该批次中的所有 5 个插入都失败了。有什么方法可以使其他插入成功吗?。如上所述,tmode 是 tera,并且启用了自动提交。如果除了单独重新提交所有失败的查询之外别无他法,那么我们将不得不减少批处理大小并满足于较低的吞吐量。
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(","))
}
}
结果
-3,-3,-3,-3,-3
这来自 Teradata 的 JDBC manual:
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.
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.
我正在使用 executeBatch 方法将数据插入 teradata table。目前,如果批处理中的一个插入失败,则批处理中的所有其他插入也会失败,最终不会插入任何记录。如果任何插入失败,我如何更改此行为以使批处理中的其他插入成功,并且具有跟踪被拒绝记录的某些能力。
PS:我已确保将 TMODE 设置为 TERA 并启用自动提交。
更新:目标table定义。
CREATE SET TABLE mydb.mytable ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
col1 INTEGER,
col2 VARCHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL)
PRIMARY INDEX ( col1 );
下面是示例 Scala 代码。如您所见,该批处理包含 5 个插入语句。 First insert 设置为失败,因为它试图将 null 插入到非 null 字段 (col2) 中。其他 4 个插入没有任何问题,应该会成功。但是正如您从下面看到的那样,该批次中的所有 5 个插入都失败了。有什么方法可以使其他插入成功吗?。如上所述,tmode 是 tera,并且启用了自动提交。如果除了单独重新提交所有失败的查询之外别无他法,那么我们将不得不减少批处理大小并满足于较低的吞吐量。
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(","))
}
}
结果
-3,-3,-3,-3,-3
这来自 Teradata 的 JDBC manual:
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.
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.