How to programmatically set rewriteBatchedStatements for mysql jdbc driver?

Here 's a way to speed up batch insert performance. Can rewriteBatchedStatements set programmatically, not through a URL?

+5
source share
2 answers

If you do not want to do this through the URL, you can use the Properties object with the DriverManager :

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

If you are using MysqlDataSource or MysqlConnectionPoolDataSource , you need to set the rewriteBatchedStatements (or set setter setRewriteBatchedStatements(boolean) property

To change this at runtime after you receive the connection, you can use:

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

Note. I just looked at the sources of MySQL Connector / J for this last parameter, I have not 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 careful with com.mchange - with another group. This code does not work.

+2
source

You must do this using Connection.setClientInfo :

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

Source: https://habr.com/ru/post/1243138/


All Articles