我正在尝试以非常高的速率批量插入到 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) 列有关
最佳答案
解决方案是停止使用:
preparedStatement.setBinaryStream(inputstream)
我用的是
preparedStatement.setBytes(byte[])
为了重写它必须需要计算总大小,这是它无法从输入流预先计算的。它现在工作得很好,我的写入速度非常棒!
关于mysql - 尝试为 MySQL 和 Java 执行 rewriteBatchedStatements 时出现空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30785327/