About transaction do..end block (or critical section)

Greetings all,

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!

Cheers