Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
atomic INSERT or UPDATE with activerecord
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
eugenio.modesti@gmail.com  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 10:41
From: "eugenio.mode...@gmail.com" <eugenio.mode...@gmail.com>
Date: Sun, 8 Nov 2009 02:41:30 -0800 (PST)
Local: Sun 8 Nov 2009 10:41
Subject: atomic INSERT or UPDATE with activerecord
what i need:
if :record is in the database -> upgrade his counter
else create :record (with his counter on 1)

this should be done with an atomic operation, to avoid race
conditions.
What is the best way to do this with activerecord?
I read about transactions, but as i understand a transaction acts on a
single connection to the database.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frederick Cheung  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:22
From: Frederick Cheung <frederick.che...@gmail.com>
Date: Sun, 8 Nov 2009 04:22:46 -0800 (PST)
Local: Sun 8 Nov 2009 12:22
Subject: Re: atomic INSERT or UPDATE with activerecord

On Nov 8, 10:41 am, "eugenio.mode...@gmail.com"

<eugenio.mode...@gmail.com> wrote:
> what i need:
> if :record is in the database -> upgrade his counter
> else create :record (with his counter on 1)

> this should be done with an atomic operation, to avoid race
> conditions.
> What is the best way to do this with activerecord?
> I read about transactions, but as i understand a transaction acts on a
> single connection to the database.

incrementing the counter is simple enough - AR's increment_counter
method basically does update foos set counter = counter + 1 where ...

To avoid duplicate records being created I'd create a unique index on
whatever it is that identifies a record

Fred


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 17:50
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Sun, 8 Nov 2009 18:50:42 +0100
Local: Sun 8 Nov 2009 17:50
Subject: Re: atomic INSERT or UPDATE with activerecord

eugenio.mode...@gmail.com wrote:
> what i need:
> if :record is in the database -> upgrade his counter
> else create :record (with his counter on 1)

> this should be done with an atomic operation, to avoid race
> conditions.
> What is the best way to do this with activerecord?
> I read about transactions, but as i understand a transaction acts on a
> single connection to the database.

This would be a single DB connection, so it seems like a good candidate
for a transaction.  What's the problem with doing that?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
eugenio  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 09:49
From: eugenio <eugenio.mode...@gmail.com>
Date: Mon, 9 Nov 2009 01:49:50 -0800 (PST)
Local: Mon 9 Nov 2009 09:49
Subject: Re: atomic INSERT or UPDATE with activerecord

On Nov 8, 6:50 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-

s.net> wrote:
> This would be a single DB connection, so it seems like a good candidate
> for a transaction.  What's the problem with doing that?

i think that multiple requests use multiple db connections, didn't
they?  This is obviously true having two database server in a multi-
master configuration. I think that Frederick Cheung's solution should
solve the problem anyway.
thank you.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 11:20
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Mon, 9 Nov 2009 12:20:44 +0100
Local: Mon 9 Nov 2009 11:20
Subject: Re: atomic INSERT or UPDATE with activerecord

eugenio wrote:
> On Nov 8, 6:50 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-
> s.net> wrote:
>> This would be a single DB connection, so it seems like a good candidate
>> for a transaction. What's the problem with doing that?
> i think that multiple requests use multiple db connections, didn't
> they?  

Of course.  But you shouldn't need a transaction over multiple requests;
just encapsulating the INSERT and counter increment in a transaction,
within a single request cycle, should do the trick.

> This is obviously true having two database server in a multi-
> master configuration.

Well, that's just asking for trouble. :)

> I think that Frederick Cheung's solution should
> solve the problem anyway.
> thank you.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google