Wednesday, 15 September 2010

java - MySQL and JDBC with rewriteBatchedStatements=true -



java - MySQL and JDBC with rewriteBatchedStatements=true -

i've been reading around, here, here , here advantages of using rewritebatchedstatements=true

if understood right, rewritebatchedstatements=true jdbc pack many queries possible single network packet, lowering way network overhead. right?

then comes attending value defined in mysql server max_allowed_packet may cause problems queries (queries not beingness executed on server).

so sec question is, jdbc knows value assigned max_allowed_packet , hence create packet smaller defined value max_allowed_packet or developer has take in consideration?

if understood wrong, please allow me know well.

with rewritebatchedstatements=true jdbc pack many queries possible single network packet, lowering way network overhead. right?

yes. next code

class="lang-java prettyprint-override">string myconnectionstring = "jdbc:mysql://localhost:3307/mydb?" + "useunicode=true&characterencoding=utf-8"; seek (connection con = drivermanager.getconnection(myconnectionstring, "root", "whatever")) { seek (preparedstatement ps = con.preparestatement("insert jdbc (`name`) values (?)")) { (int = 1; <= 5; i++) { ps.setstring(1, string.format( "line %d: lorem ipsum dolor sit down amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim advertisement minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", i)); ps.addbatch(); } ps.executebatch(); ps.close(); } con.close(); }

will send individual insert statements though have created batch

class="lang-jsql prettyprint-override">insert jdbc (`name`) values ('line 1: lorem ipsum ...') insert jdbc (`name`) values ('line 2: lorem ipsum ...')

however, if alter connection string include rewritebatchedstatements=true

class="lang-java prettyprint-override">string myconnectionstring = "jdbc:mysql://localhost:3307/mydb?" + "useunicode=true&characterencoding=utf-8" + "&rewritebatchedstatements=true";

then jdbc send 1 or more multi-row insert statements

class="lang-sql prettyprint-override">insert jdbc (`name`) values ('line 1: lorem ipsum ...'),('line 2: lorem ipsum ...')

does jdbc knows value assigned max_allowed_packet , hence create packet smaller defined value max_allowed_packet ... ?

yes. if enable mysql general log , check see mysql connector/j inspects bunch of variables when connects, 1 of max_allowed_packet. can set little max_allowed_packet value , verify jdbc splits batch several multi-row insert statements if single such statement whole batch exceed max_allowed_packet.

java mysql sql jdbc

No comments:

Post a Comment