Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MySqlBulkLoader be used with a transaction?

Can MySqlBulkLoader be used with a transaction? I don't see a way to explicitly attach a transaction to an instance of the loader. Is there another way?

like image 710
Daniel Avatar asked Feb 11 '11 23:02

Daniel


2 Answers

As stated here by member of MySQL documentation team:

It's not atomic.  The records loaded prior to the error will be in the
table.

Work arround is to import data to dedicated table and then execute INSERT INTO ... SELECT ... which will be atomic operation. On huge data sets this is potential problem becasue of long transaction.

like image 70
k2s Avatar answered Oct 21 '22 22:10

k2s


The MySQL manual indicates that the MySqlBulkLoader is a wrapper of 'LOAD DATA INFILE'. While looking at the 'LOAD DATA INFILE' documentation I noticed this paragraph:

If you specify IGNORE, input rows that duplicate an existing row on a unique key value are skipped. If you do not specify either option, the behavior depends on whether the LOCAL keyword is specified. Without LOCAL, an error occurs when a duplicate key value is found, and the rest of the text file is ignored. With LOCAL, the default behavior is the same as if IGNORE is specified; this is because the server has no way to stop transmission of the file in the middle of the operation.

I found no discussion on transactions but the above paragraph would indicate that transactions are not possible.

A workaround would be to import the data into a import table and then use a separate stored procedure to process the data using transactions into the desired table.

So in answ

like image 1
John M Avatar answered Oct 21 '22 21:10

John M