7

Here is a way to speed up batch insert performance. Can rewriteBatchedStatements be set programatically, not via url?

Community
  • 1
  • 1
Cherry
  • 31,309
  • 66
  • 224
  • 364
  • My be this existing question in SO will help you. http://stackoverflow.com/questions/26307760/mysql-and-jdbc-with-rewritebatchedstatements-true – Srijani Ghosh Feb 16 '16 at 13:24

2 Answers2

3

If you don't want to do it through the URL, you can use the Properties object with DriverManager:

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

If you use a MysqlDataSource or MysqlConnectionPoolDataSource then you need to set the property rewriteBatchedStatements (or call setter setRewriteBatchedStatements(boolean)

To change this at runtime after you have obtained a connection, you should be able to use:

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

Note: I have only looked at the MySQL Connector/J sources for this last option, I haven't tested it.

UPDATED

For c3p0 you can use the following:

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

c3p0 should be com.mchange:c3p0:0.9.5.2, be carefull with com.mchange - with other groupId this code does not work.

Cherry
  • 31,309
  • 66
  • 224
  • 364
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • @Cherry Thanks for the edit. I totally forgot to mention `unwrap`, it will probably work for the normal MySQL connection, and for wrapped MySQL connections of other data sources as well. – Mark Rotteveel Feb 17 '16 at 08:03
0

You should be able to do this using Connection.setClientInfo:

Connection c = ...;
c.setClientInfo("rewriteBatchedStatements", "true");
Ferrybig
  • 18,194
  • 6
  • 57
  • 79
  • Do you know for sure this works? I just took a quick glance at the MySQL Connector/J sourcecode, but setClientInfo only seems to set server side information about the current connection. – Mark Rotteveel Feb 16 '16 at 14:32