Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How programatically set rewriteBatchedStatements for mysql jdbc driver?

Tags:

java

mysql

jdbc

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

like image 980
Cherry Avatar asked Feb 16 '16 13:02

Cherry


Video Answer


1 Answers

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.

like image 128
Mark Rotteveel Avatar answered Oct 10 '22 22:10

Mark Rotteveel