java – 如何以编程方式为mysql jdbc驱动程序设置rewriteBatchedStatements?

Here是一种加快批量插入性能的方法.可以以编程方式设置rewriteBatchedStatements,而不是通过url设置吗?
最佳答案
如果您不想通过URL执行此操作,则可以将Driver对象与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 Connector / 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这个代码不起作用.

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。