0

Hi All following is the problem scenario:

I am using MYSQL (Innodb engine), one of my application(C++/MYSQLCAPI) is doing following operation :

START TRANSACTION

truncate my_table

load Data infile into table my_table.

if both the above command [truncate and load ] are successful then COMMIT

else ROLLBACK

now another application(C++/MYSQLCAPI) which is reading this table in every second by following command.

select * from my_table

ERROR: in this read attempt sometime it gets 0 data , what could be the reason for this ?

birubisht
  • 890
  • 8
  • 16
  • I guess the reader gets no data because he tries to set a read lock (depends on isolation level) and doesn't get the lock because of your transaction. So he could try to reda with nolock. – user743414 Sep 19 '16 at 07:38
  • no , reader application is not taking any lock . – birubisht Sep 19 '16 at 07:42

2 Answers2

1

You're seeing an empty table since truncate table has an implicit commit. If you need to change the entire table in a transaction you can use delete then insert, or try the rename solution presented in this answer

Community
  • 1
  • 1
C. Broadbent
  • 753
  • 4
  • 11
1
CREATE TABLE new LIKE real;
load `new` by whatever means
if something went wrong, don't do the next two steps.
RENAME TABLE real TO old, new TO real;
DROP TABLE old;

This avoids the problem you mentioned, plus lots of other problems. In particular, it needs no special transaction handling; the RENAME is atomic and very fast.

Rick James
  • 135,179
  • 13
  • 127
  • 222