Skip to content

Commit

Permalink
CONJ-619 correcting Statement.executeBatch using LOAD DATA INFILE
Browse files Browse the repository at this point in the history
LOAD DATA INFILE commands cannot cannot be used when using pipelining, because of multiple exchanges. This fix the issue ensuring having no command LOAD DATA INFILE when using pipeline.
  • Loading branch information
rusher committed May 12, 2021
1 parent 1acaffc commit 6644194
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import javax.sql.rowset.serial.SerialException;
import org.mariadb.jdbc.LocalInfileInterceptor;
import org.mariadb.jdbc.MariaDbConnection;
Expand Down Expand Up @@ -108,7 +109,8 @@ public class AbstractQueryProtocol extends AbstractConnectProtocol implements Pr
private static final Logger logger = LoggerFactory.getLogger(AbstractQueryProtocol.class);
private static final Set<Integer> LOCK_DEADLOCK_ERROR_CODES =
new HashSet<>(Arrays.asList(1205, 1213, 1614));

private static final Pattern LOAD_LOCAL_INFILE =
Pattern.compile("^(load\\s*(data|xml)\\s*local\\s*infile)", Pattern.CASE_INSENSITIVE);
private ThreadPoolExecutor readScheduler = null;
private int transactionIsolationLevel = 0;
private InputStream localInfileInputStream;
Expand Down Expand Up @@ -750,7 +752,18 @@ public void executeBatchStmt(
*/
private void executeBatch(Results results, final List<String> queries) throws SQLException {

if (!options.useBatchMultiSend) {
// Ensure not having load local infile
boolean usePipeline = options.useBatchMultiSend;
if (usePipeline) {
for (int i = 0; i < queries.size(); i++) {
if (LOAD_LOCAL_INFILE.matcher(queries.get(i)).find()) {
usePipeline = false;
break;
}
}
}

if (!usePipeline) {

String sql = null;
SQLException exception = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private static String buildMsgText(
}

if (options != null
&& deadLockException != null
&& options.includeThreadDumpInDeadlockExceptions) {
&& deadLockException != null
&& options.includeThreadDumpInDeadlockExceptions) {
if (threadName != null) {
msg.append("\nthread name: ").append(threadName);
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/mariadb/jdbc/LocalInfileInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void initClass() throws SQLException {
drop();
try (Statement stmt = sharedConnection.createStatement()) {
stmt.execute("CREATE TABLE LocalInfileInputStreamTest(id int, test varchar(100))");
stmt.execute("CREATE TABLE LocalInfileInputStreamTest2(id int, test varchar(100))");
stmt.execute("CREATE TABLE LocalInfileXmlInputStreamTest(id int, test varchar(100))");
stmt.execute("CREATE TABLE ttlocal(id int, test varchar(100))");
stmt.execute("CREATE TABLE ttXmllocal(id int, test varchar(100))");
Expand All @@ -82,6 +83,7 @@ public static void initClass() throws SQLException {
public static void drop() throws SQLException {
try (Statement stmt = sharedConnection.createStatement()) {
stmt.execute("DROP TABLE IF EXISTS LocalInfileInputStreamTest");
stmt.execute("DROP TABLE IF EXISTS LocalInfileInputStreamTest2");
stmt.execute("DROP TABLE IF EXISTS LocalInfileXmlInputStreamTest");
stmt.execute("DROP TABLE IF EXISTS ttlocal");
stmt.execute("DROP TABLE IF EXISTS ttXmllocal");
Expand All @@ -90,6 +92,21 @@ public static void drop() throws SQLException {
}
}

@Test
public void loadDataInBatch() throws SQLException {
String batch_update =
"LOAD DATA LOCAL INFILE 'dummy.tsv' INTO TABLE LocalInfileInputStreamTest2 (id, test)";
String builder = "1\thello\n2\tworld\n";
try (Connection con = setConnection()) {
Statement smt = con.createStatement();
InputStream inputStream = new ByteArrayInputStream(builder.getBytes());
((MariaDbStatement) smt).setLocalInfileInputStream(inputStream);
smt.addBatch(batch_update);
smt.addBatch("SET UNIQUE_CHECKS=1");
smt.executeBatch();
}
}

@Test
public void testLocalInfileInputStream() throws SQLException {
Assume.assumeFalse((!isMariadbServer() && minVersion(8, 0, 3)));
Expand Down

0 comments on commit 6644194

Please sign in to comment.