Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 85807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:08:50+00:00 2026-05-10T22:08:50+00:00

I need to do transactions (begin, commit or rollback), locks (select for update). How

  • 0

I need to do transactions (begin, commit or rollback), locks (select for update). How can I do it in a document model db?

Edit:

The case is this:

  • I want to run an auctions site.
  • And I think how to direct purchase as well.
  • In a direct purchase I have to decrement the quantity field in the item record, but only if the quantity is greater than zero. That is why I need locks and transactions.
  • I don’t know how to address that without locks and/or transactions.

Can I solve this with CouchDB?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:08:51+00:00Added an answer on May 10, 2026 at 10:08 pm

    No. CouchDB uses an ‘optimistic concurrency’ model. In the simplest terms, this just means that you send a document version along with your update, and CouchDB rejects the change if the current document version doesn’t match what you’ve sent.

    It’s deceptively simple, really. You can reframe many normal transaction based scenarios for CouchDB. You do need to sort of throw out your RDBMS domain knowledge when learning CouchDB, though. It’s helpful to approach problems from a higher level, rather than attempting to mold Couch to a SQL based world.

    Keeping track of inventory

    The problem you outlined is primarily an inventory issue. If you have a document describing an item, and it includes a field for ‘quantity available’, you can handle concurrency issues like this:

    1. Retrieve the document, take note of the _rev property that CouchDB sends along
    2. Decrement the quantity field, if it’s greater than zero
    3. Send the updated document back, using the _rev property
    4. If the _rev matches the currently stored number, be done!
    5. If there’s a conflict (when _rev doesn’t match), retrieve the newest document version

    In this instance, there are two possible failure scenarios to think about. If the most recent document version has a quantity of 0, you handle it just like you would in a RDBMS and alert the user that they can’t actually buy what they wanted to purchase. If the most recent document version has a quantity greater than 0, you simply repeat the operation with the updated data, and start back at the beginning. This forces you to do a bit more work than an RDBMS would, and could get a little annoying if there are frequent, conflicting updates.

    Now, the answer I just gave presupposes that you’re going to do things in CouchDB in much the same way that you would in an RDBMS. I might approach this problem a bit differently:

    I’d start with a ‘master product’ document that includes all the descriptor data (name, picture, description, price, etc). Then I’d add an ‘inventory ticket’ document for each specific instance, with fields for product_key and claimed_by. If you’re selling a model of hammer, and have 20 of them to sell, you might have documents with keys like hammer-1, hammer-2, etc, to represent each available hammer.

    Then, I’d create a view that gives me a list of available hammers, with a reduce function that lets me see a ‘total’. These are completely off the cuff, but should give you an idea of what a working view would look like.

    Map

    function(doc)  {      if (doc.type == 'inventory_ticket' && doc.claimed_by == null ) {          emit(doc.product_key, { 'inventory_ticket' :doc.id, '_rev' : doc._rev });      }  } 

    This gives me a list of available ‘tickets’, by product key. I could grab a group of these when someone wants to buy a hammer, then iterate through sending updates (using the id and _rev) until I successfully claim one (previously claimed tickets will result in an update error).

    Reduce

    function (keys, values, combine) {     return values.length; } 

    This reduce function simply returns the total number of unclaimed inventory_ticket items, so you can tell how many ‘hammers’ are available for purchase.

    Caveats

    This solution represents roughly 3.5 minutes of total thinking for the particular problem you’ve presented. There may be better ways of doing this! That said, it does substantially reduce conflicting updates, and cuts down on the need to respond to a conflict with a new update. Under this model, you won’t have multiple users attempting to change data in primary product entry. At the very worst, you’ll have multiple users attempting to claim a single ticket, and if you’ve grabbed several of those from your view, you simply move on to the next ticket and try again.

    Reference: https://wiki.apache.org/couchdb/Frequently_asked_questions#How_do_I_use_transactions_with_CouchDB.3F

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See Installing curl from hackage on Cygwin. May 11, 2026 at 10:06 pm
  • Editorial Team
    Editorial Team added an answer You are mistaken that Rectangle does not support this: <Rectangle… May 11, 2026 at 10:06 pm
  • Editorial Team
    Editorial Team added an answer VSTO or Visual Studio Tools for Office May 11, 2026 at 10:06 pm

Related Questions

We have 2 databases, say DB1 and DB2. DB1 contains all the stored procedures
I want to run an update query against a production database and as good
I have a MSSSQL stored procedure performing a distributed transaction that looks like this:
I have a class 'Database' that works as a wrapper for ADO.net. For instance,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.