Does the transaction do..end block guaranteed a critical section or
just a database transaction? I want to save some file to the file system
and then save the file path to a DB table, there are race conditions
because other rails process/thread may create a file with a same name
and this application may destroy the file by overwrite it. I want to
construct a critical section like:
/* critical section do */
file_path = generate_filepath
some_process if file_path.exist?
dbrec1.file_path = file_path
dbrec1.save
dbrec2.save
File.write(file_path)
/* end */
Does the transaction do..end block guaranteed a critical section or
just a database transaction?
Just a database transaction
I want to save some file to the file system
and then save the file path to a DB table, there are race conditions
because other rails process/thread may create a file with a same name
and this application may destroy the file by overwrite it. I want to
construct a critical section like:
Does the transaction do..end block guaranteed a critical section or
just a database transaction? I want to save some file to the file system
and then save the file path to a DB table, there are race conditions
because other rails process/thread may create a file with a same name
and this application may destroy the file by overwrite it. I want to
construct a critical section like:
/* critical section do */
file_path = generate_filepath
some_process if file_path.exist?
dbrec1.file_path = file_path
dbrec1.save
dbrec2.save
File.write(file_path)
/* end */
How can I achieve this? Thanks very much!
Perhaps by using a lock file, either with File::flock, or the
NFS-safe lockfile library: