【发布时间】:2016-02-16 13:22:40
【问题描述】:
Here 是一种提高批量插入性能的方法。 rewriteBatchedStatements 是否可以通过 url 以编程方式设置,不?
【问题讨论】:
-
我在 SO 中存在的这个问题会对您有所帮助。 stackoverflow.com/questions/26307760/…
Here 是一种提高批量插入性能的方法。 rewriteBatchedStatements 是否可以通过 url 以编程方式设置,不?
【问题讨论】:
如果你不想通过 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,此代码不起作用。
【讨论】:
unwrap,它可能适用于普通的 MySQL 连接,也适用于其他数据源的包装 MySQL 连接。
您应该可以使用Connection.setClientInfo:
Connection c = ...;
c.setClientInfo("rewriteBatchedStatements", "true");
【讨论】: