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

  • SEARCH
  • Home
  • 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 7248713
In Process

The Archive Base Latest Questions

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

Overview: I am trying to avoid a race condition with accessing an IndexedDB from

  • 0

Overview:
I am trying to avoid a race condition with accessing an IndexedDB from both a webpage and a web-worker.

Setup:
Webpage that is saving items to the local IndexedDB as the user works with the site. Whenever a user saves data to the local DB the record is marked as “Unsent”.

Web-worker background thread that is pulling data from the IndexedDB, sending it to the server and once the server receives it, marking the data in the IndexedDB as “Sent”.

Problem:
Since access to the IndexedDB is asynchronous, I can not be guaranteed that the user won’t update a record at the same time the web-worker is sending it to the server. The timeline is shown below:

  1. Web-worker gets data from DB and sends it to the server
  2. While the transfer is happening, the user updates the data saving it to the DB.
  3. The web-worker gets the response from the server and then updates the DB to “Sent”
  4. There is now data in DB that hasn’t been sent to the server but marked as “Sent”

Failed Solution:
After getting the response from the server, I can recheck to row to see if anything has been changed. However I am still left with a small window where data can be written to the DB and it will never be sent to the server.

Example:
After server says data is saved, then:

IndexedDB.HasDataChanged(
    function(changed) { 
        // Since this is async, this changed boolean could be lying.
        // The data might have been updated after I checked and before I was called.
        if (!changed){ 
          IndexedDB.UpdateToSent() }
    });

Other notes:
There is a sync api according to the W3 spec, but no one has implemented it yet so it can not be used (http://www.w3.org/TR/IndexedDB/#sync-database). The sync api was designed to be used by web-workers, to avoid this exact situation I would assume.

Any thoughts on this would be greatly appreciated. Have been working on it for about a week and haven’t been able to come up with anything that will work.

  • 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. Editorial Team
    Editorial Team
    2026-05-28T22:10:34+00:00Added an answer on May 28, 2026 at 10:10 pm

    I think I found a work around for this for now. Not really as clean as I would like, but it seems to be thread safe.

    I start by storing the datetime into a LastEdit field, whenever I update the data.
    From the web-worker, I am posting a message to the browser.

    self.postMessage('UpdateDataSent#' + data.ID + '#' + data.LastEdit);
    

    Then in the browser I am updating my sent flag, as long as the last edit date hasn’t changed.

    // Get the data from the DB in a transaction
    if (data.LastEdit == lastEdit)
    {
        data.Sent = true;
        var saveStore = trans.objectStore("Data");
        var saveRequest = saveStore.put(data);
        console.log('Data updated to Sent');
    }
    

    Since this is all done in a transaction in the browser side, it seems to work fine. Once the browsers support the Sync API I can throw it all away anyway.

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

Sidebar

Related Questions

Overview: I'm trying to design an application that will encrypt files to safely send
I'm trying to build a quick overview that shows the upcoming calendar week. I
I'm trying to setup a connection to a webservice from VB.NET I'm totally clueless
Overview: I am trying to implement the Johnson Trotter Algorithm in Java so that
I'm trying to make an iOS 5 app that features real-time things coming from
I'm trying to pull values from a database for a web app where a
Overview I am trying to get a photo feed on to my site using
Just before I begin heres a small overview of what I'm trying to achieve
Overview: I have an array of 20 byte strings that needs to be stored
I have a overview page containing a list with some links from which multiple

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.