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, Difei -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-04 19:40 UTC
Re: about transaction do..end block (or critical section)
On 4 Sep 2008, at 19:19, Difei Zhao <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Greetings all, > > 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: >Open the file with an exclusive lock? Fred> /* 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, > Difei > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Sep-04 19:47 UTC
Re: about transaction do..end block (or critical section)
Difei Zhao wrote:> 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: http://raa.ruby-lang.org/project/lockfile/1.3.0 -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Difei Zhao
2008-Sep-05 03:58 UTC
Re: about transaction do..end block (or critical section)
Frederick Cheung wrote:> On 4 Sep 2008, at 19:19, Difei Zhao <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> >> Greetings all, >> >> 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: >> > Open the file with an exclusive lock? > > FredThank you all. But I think something like: File.new("blabla", File::CREAT|File::WRONLY).flock is not atomic and may lock the file generated by aother process/thread? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Difei Zhao wrote:> Frederick Cheung wrote:> > File.new("blabla", File::CREAT|File::WRONLY).flock > > is not atomic and may lock the file generated by aother process/thread?I overlooked the O_EXCL, thanks all. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---