[CONJ-206] Take max_allowed_packet into consideration when rewriting statements Created: 2015-10-20  Updated: 2015-11-02  Resolved: 2015-11-02

Status: Closed
Project: MariaDB Connector/J
Component/s: Other
Affects Version/s: 1.2.2, 1.2.3
Fix Version/s: 1.3.0

Type: Task Priority: Major
Reporter: Andrei Anishchenko Assignee: Diego Dupin
Resolution: Fixed Votes: 1
Labels: None
Environment:

Windows 7 Professional
MySQL 5.5.34-log Community Server

java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)


Attachments: Zip Archive MariaDB Connector.zip    
Issue Links:
Relates
relates to CONJ-99 Support for batch statements rewrite Closed

 Description   

Attached please find a couple of tests, one with MariaDB Connector and another with MySQL Connector. Both do the same, but with the default value of max_allowed_packet the first test fails while the other stands.

As per CONJ-99, batched statements are rewritten. However, the value of max_allowed_packet is only taken into consideration when already rewritten statement is about to be sent. If resulting (rewritten) statement is too big then the whole operation fails.

MySQL Connector, on the other hand, rewrites the statement, splitting the result as needed in order not to exceed the value of max_allowed_packet (see com.mysql.jdbc.PreparedStatement#computeBatchSize).

import org.junit.Before;
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
public class MaxPacketAllowedTest {
 
    private Properties properties;
 
    @Before
    public void setUp() throws Exception {
        properties = new Properties();
        properties.put("user", "root");
        properties.put("password", "");
    }
 
    @Test
    public void testMariaDbConnector() throws Exception {
        executeBatch(new org.mariadb.jdbc.Driver());
    }
 
    @Test
    public void testMySqlDbConnector() throws Exception {
        executeBatch(new com.mysql.jdbc.Driver());
    }
 
    private void executeBatch(Driver driver) throws SQLException {
        Connection connection = driver.connect(
                "jdbc:mysql://localhost:3306/max?rewriteBatchedStatements=true",
                properties);
        Statement statement = connection.createStatement();
        statement.execute("DROP TABLE IF EXISTS test");
        statement.execute("CREATE TABLE test (id INT, text VARCHAR(50))");
 
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO test VALUES(?, ?)");
 
        for (int i = 0; i < 100000; i++) {
            preparedStatement.setInt(1, i);
            preparedStatement.setString(2, "test text");
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
    }
}



 Comments   
Comment by Diego Dupin [ 2015-10-23 ]

Good remark.

That is something that will be done to avoid max_allowed_packet Exception.

Comment by Diego Dupin [ 2015-11-02 ]

fixed in 1.3.0

Generated at Thu Feb 08 03:13:57 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.