2016-02-16 35 views

回答

2

如果你不想通過URL來做到這一點,你可以使用Properties對象與DriverManager

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

如果您使用MysqlDataSourceMysqlConnectionPoolDataSource那麼您需要設置屬性rewriteBatchedStatements(或致電設置器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此代碼不起作用。

+0

@Cherry感謝您的編輯。我完全忘了提到'unwrap',它可能適用於正常的MySQL連接,也適用於其他數據源的包裝MySQL連接。 –

0

你應該能夠做到這一點使用Connection.setClientInfo

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

你確定知道這個作品嗎?我只是快速瀏覽了MySQL Connector/J源代碼,但setClientInfo似乎只設置了有關當前連接的服務器端信息。 –

相關問題