【问题标题】:Null pointer when trying to do rewriteBatchedStatements for MySQL and Java尝试为 MySQL 和 Java 执行 rewriteBatchedStatements 时的空指针
【发布时间】:2015-06-11 15:38:18
【问题描述】:

我正在尝试以非常高的速率批量插入 mysql。我想尝试 rewriteBatchedStatements 配置选项,因为我读过它会显着影响性能。但是,当我添加选项时,出现以下异常:

java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.computeMaxParameterSetSizeAndBatchSize(PreparedStatement.java:1694)
at com.mysql.jdbc.PreparedStatement.computeBatchSize(PreparedStatement.java:1651)
at com.mysql.jdbc.PreparedStatement.executeBatchedInserts(PreparedStatement.java:1515)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1272)
at com.zaxxer.hikari.proxy.StatementProxy.executeBatch(StatementProxy.java:116)
at com.zaxxer.hikari.proxy.PreparedStatementJavassistProxy.executeBatch(PreparedStatementJavassistProxy.java)

这是我的插入代码:

try (Connection connection = DBUtil.getInstance().getConnection();
         PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        connection.setAutoCommit(false);
        for (TransactionBatch batch : batches) {
            try {                   
                preparedStatement.setString(1, batch.getDeviceID());
                preparedStatement.setBinaryStream(2, new ByteArrayInputStream(dataArray));
                preparedStatement.addBatch();
            } catch (Exception e) {
               e.printStackTrace();
            }
        }

        preparedStatement.executeBatch();
    } catch (Exception e) {
        e.printStackTrace();
    }

这是我的 jdbc 网址:

jdbc:mysql://url:port/tableName?user=userame&password=password&useServerPrepStmts=false&rewriteBatchedStatements=true

我也使用 HikariCP 作为我的连接池。

编辑:更新 - 看起来问题与表中有一个 varbinary(10000) 列有关

【问题讨论】:

  • 我认为你不能重写包含 blob 数据的批次。

标签: mysql prepared-statement hikaricp batch-insert


【解决方案1】:

解决办法是停止使用:

preparedStatement.setBinaryStream(inputstream)

我用了

preparedStatement.setBytes(byte[])

为了重写它必须需要计算它不能从输入流预先计算的总大小。它现在运行良好,我的写入速度也很棒!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2018-12-11
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多