【问题标题】:How programatically set rewriteBatchedStatements for mysql jdbc driver?如何以编程方式为 mysql jdbc 驱动程序设置 rewriteBatchedStatements?
【发布时间】:2016-02-16 13:22:40
【问题描述】:

Here 是一种提高批量插入性能的方法。 rewriteBatchedStatements 是否可以通过 url 以编程方式设置,不?

【问题讨论】:

标签: java mysql jdbc


【解决方案1】:

如果你不想通过 URL 来做,你可以使用 Properties 对象和 DriverManager:

Properties props = new Properties();
props.setProperty("user", ...);
props.setProperty("password", ...);
props.setProperty("rewriteBatchedStatements", "true");
Connection connection = DriverManager.getConnection(url, props);

如果您使用MysqlDataSource 或MysqlConnectionPoolDataSource,则需要设置属性rewriteBatchedStatements(或调用setter setRewriteBatchedStatements(boolean)

要在获得连接后在运行时更改此设置,您应该可以使用:

((com.mysql.jdbc.ConnectionProperties) connection).setRewriteBatchedStatements(true);

注意:我只查看了最后一个选项的 MySQL 连接器/J 源,我还没有测试过。

更新

对于 c3p0,您可以使用以下内容:

ComboPooledDataSource cpds = ...
Connection connection = cpds.getConnection();
connection.unwrap(com.mysql.jdbc.ConnectionProperties.class).setRewriteBatchedStatements(true);

c3p0 应该是com.mchange:c3p0:0.9.5.2,注意com.mchange - 使用其他 groupId,此代码不起作用。

【讨论】:

  • @Cherry 感谢您的编辑。我完全忘了提到unwrap,它可能适用于普通的 MySQL 连接,也适用于其他数据源的包装 MySQL 连接。
【解决方案2】:

您应该可以使用Connection.setClientInfo:

Connection c = ...;
c.setClientInfo("rewriteBatchedStatements", "true");

【讨论】:

  • 你确定这有效吗?我只是快速浏览了一下 MySQL Connector/J 源代码,但 setClientInfo 似乎只设置了有关当前连接的服务器端信息。
猜你喜欢
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 2011-09-10
  • 2016-03-27
  • 2020-06-14
  • 2010-12-01
相关资源
最近更新 更多