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

**URL:** <https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730>\
**Category:** Rails\
**Created:** [September 4, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730 "2008-09-04T19:19:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [September 4, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730/1 "2008-09-04T19:19:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [September 4, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730/2 "2008-09-04T22:58:40Z")

</div>

On 4 Sep 2008, at 19:19, Difei Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [September 5, 2008, 5:58am UTC](https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730/3 "2008-09-05T05:58:10Z")

</div>

Frederick C. wrote:

> On 4 Sep 2008, at 19:19, Difei Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

Thank 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?

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [September 5, 2008, 8:30am UTC](https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730/4 "2008-09-05T08:30:08Z")

</div>

Difei Z. wrote:

> Frederick C. 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.

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [September 4, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/about-transaction-do-end-block-or-critical-section/147730/5 "2008-09-04T21:48:48Z")

</div>

Difei Z. wrote:

> 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](http://raa.ruby-lang.org/project/lockfile/1.3.0)

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
