【问题标题】:SQLException during executeBatch() when I'm handling BatchUpdateException当我处理 BatchUpdateException 时,executeBatch() 期间的 SQLException
【发布时间】:2014-11-17 17:12:21
【问题描述】:

我遇到了一些麻烦。

我正在尝试列出有关在 executeUpdate() 期间失败的查询的一些数据,并且我已经读过可以捕获 BatchUpdateException 然后获取 updateCount,它会告诉您哪些查询有效,哪些无效,但是当查询由于数据转换错误而失败,它会启动 SQLException,我无法捕获整个批处理执行错误。

进行查询的数据是从 XML 中获取的,无需任何类型的验证。

代码如下:

try {
    pSt_insertCabecera.executeBatch();
    } 
catch (BatchUpdateException buex){
    int[] updateCounts = buex.getUpdateCounts();  
    for (int i = 0; i < updateCounts.length; i++) {  
        if (updateCounts[i] == Statement.EXECUTE_FAILED) {  
            logger.error(nombreClase + "[ESB_P]: Ha fallado la inserción de la cabecera de pedidos: " + cabecerasAInsertar.get(i).toString());
            throw new SQLException();
        }
    }
}

除了 if 之外抛出的 SQLException 是因为稍后我在代码中捕获它以执行回滚。

StackTrace 如下所示:

com.microsoft.sqlserver.jdbc.SQLServerException: Error al convertir una cadena de caracteres en fecha y/u hora.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatementBatch(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtBatchExecCmd.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(Unknown Source)

我使用的是 SQLServer 2012。

如果您需要更多代码或信息,请询问。

非常感谢大家。

【问题讨论】:

  • 你得到了什么异常?
  • 我得到一个 SQLException。感谢您这么快回复!
  • 你能贴出你的堆栈跟踪吗?
  • 我已经发布了。再次感谢。
  • @KickButtowski 根据 Google 翻译,堆栈跟踪中的消息是“无法将字符串转换为日期和/或时间。”

标签: java sql-server jdbc


【解决方案1】:

我能够重现您的问题。对于名为 [Clients] 且具有名为 [DOB] 的 datetime 列的表,代码

String[] datesToApply = new String[] 
        {
        "1978-12-31",
        "junk",
        "1981-11-11"
        };

String sql = 
        "UPDATE Clients SET DOB=? WHERE ID=1";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
    for (String dt : datesToApply) {
        ps.setString(1, dt);
        ps.addBatch();
    }
    int[] updateCounts = null;
    try {
        updateCounts = ps.executeBatch();
    } catch (BatchUpdateException bue) {
        System.out.println("executeBatch threw BatchUpdateException: " + bue.getMessage());
        updateCounts = bue.getUpdateCounts();
    } catch (SQLException se) {
        System.out.println("executeBatch threw SQLException: " + se.getMessage());
    }
    if (updateCounts != null) {
        for (int uc : updateCounts) {
            System.out.println(uc);
        }
    }
    if (!conn.getAutoCommit()) {
        conn.commit();
    }

如果 AutoCommit 关闭,则抛出 SQLException,但如果 AutoCommit 开启,则抛出 BatchUpdateException。检查.getUpdateCounts() 返回的数组向我们展示了批处理中发生第一次失败的点

executeBatch threw BatchUpdateException: Conversion failed when converting date and/or time from character string.
1
-3
-3

但它也表明 SQL Server 在第一次失败后“放弃”。似乎没有选项告诉 SQL Server 继续处理批处理的其余部分(如 MySQL 连接器/J 的 continueBatchOnError)。

此外,由于 AutoCommit 已“启用”,因此已提交到该点的更新,因此无法选择回滚这些更改大量。如果您希望批处理是“全有或全无”,您必须编写一些代码来返回并反转在第一次失败之前成功应用的更改。

(当然,另一种选择是在尝试将数据放入数据库之前验证数据。)

【讨论】:

  • 感谢上帝!正在评估是否在执行更新之前验证数据,或者在引发 SQLException 的情况下,然后调用相同的方法但将 AutoCommit 设置为“on”并进行相同的操作并插入临时表以获取错误。但我个人找到了一个更好的选择来在插入之前进行验证,但他们想要快速修复,而不是开发......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多