Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL and JDBC with rewriteBatchedStatements=true

Tags:

java

sql

mysql

jdbc

I've been reading around, here, here and here about the advantages of using rewriteBatchedStatements=true

If I understood it right, with rewriteBatchedStatements=true the JDBC will pack as many queries as possible into a single network packet, lowering this way the network overhead. Am I right?

Then it comes into my attention that the value defined in the MySQL server for the max_allowed_packet may cause problems with the queries (queries not being executed on the server).

So my second question is, does JDBC knows the value assigned to max_allowed_packet and therefore make the packet smaller than the defined value for max_allowed_packet or that is something that the developer has to take in consideration?

If I understood something wrong, please let me know as well.

like image 932
dazito Avatar asked Oct 10 '14 20:10

dazito


People also ask

Does JDBC work with MySQL?

To connect to MySQL from Java, you have to use the JDBC driver from MySQL. The MySQL JDBC driver is called MySQL Connector/J. You find the latest MySQL JDBC driver under the following URL: http://dev.mysql.com/downloads/connector/j. The download contains a JAR file which we require later.

What is the JDBC connection string for MySQL?

Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and sonoo is the database name.

What JDBC driver used to connect to MySQL?

The driver that JDBC needs to connect to MySQL is called Connector/J. It is developed by the MySQL engineering team, and the latest version is available for free from their website. Download the binary distribution appropriate for your platform, extract the JAR file, and copy it to "$CATALINA_HOME/lib".

How does JDBC connect to VS code in MySQL?

this will open Classpath Configuration in a new tab. Scroll to the bottom and then click add on the referenced libraries. This will open an explorer pop-up window. Select the java-mysql connector jar file and then it should work.

What is rewritebatchedstatements=true in JDBC?

1 Answer 1. with rewriteBatchedStatements=true the JDBC will pack as many queries as possible into a single network packet, lowering this way the network overhead.

Can server-sided prepared statements take advantage of rewritebatchedstatements?

The description for the connection property rewriteBatchedStatements has been corrected, removing the limitation that server-sided prepared statements could not take advantage of the rewrite option. (Bug #34022110)

Why is my MySQL INSERT statement not being executed?

Because the rewriteBatchedStatements is false, each INSERT statement will be executed individually using the executeUpdateInternal method call. So, even if we used addBatch and executeBatch, by default, MySQL still executes the INSERT statements individually when using the plain JDBC Statement object.

Is myconnectionstring a JDBC string?

Yes. The following code String myConnectionString = "jdbc:mysql://localhost:3307/mydb?"


1 Answers

with rewriteBatchedStatements=true the JDBC will pack as many queries as possible into a single network packet, lowering this way the network overhead. Am I right?

Yes. The following code

String myConnectionString =
        "jdbc:mysql://localhost:3307/mydb?" +
        "useUnicode=true&characterEncoding=UTF-8";
try (Connection con = DriverManager.getConnection(myConnectionString, "root", "whatever")) {
    try (PreparedStatement ps = con.prepareStatement("INSERT INTO jdbc (`name`) VALUES (?)")) {
        for (int i = 1; i <= 5; i++) {
            ps.setString(1, String.format(
                    "Line %d: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", 
                    i));
            ps.addBatch();
        }
        ps.executeBatch();
    }
}

will send individual INSERT statements even though I have created a Batch

INSERT INTO jdbc (`name`) VALUES ('Line 1: Lorem ipsum ...')
INSERT INTO jdbc (`name`) VALUES ('Line 2: Lorem ipsum ...')

However, if I change the connection string to include rewriteBatchedStatements=true

String myConnectionString =
        "jdbc:mysql://localhost:3307/mydb?" +
        "useUnicode=true&characterEncoding=UTF-8" +
        "&rewriteBatchedStatements=true";

then JDBC will send one or more multi-row INSERT statements

INSERT INTO jdbc (`name`) VALUES ('Line 1: Lorem ipsum ...'),('Line 2: Lorem ipsum ...')

does JDBC knows the value assigned to max_allowed_packet and therefore make the packet smaller than the defined value for max_allowed_packet ... ?

Yes. If you enable the MySQL general log and check it you will see that MySQL Connector/J inspects a bunch of variables when it connects, one of which is max_allowed_packet. You can also set a small max_allowed_packet value and verify that JDBC splits a batch into several multi-row INSERT statements if a single such statement for the whole batch would exceed max_allowed_packet.

like image 97
Gord Thompson Avatar answered Oct 23 '22 17:10

Gord Thompson