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 7906225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:50:19+00:00 2026-06-03T10:50:19+00:00

I’ve got a database that maintains Jobs to be processed by various processing machines.

  • 0

I’ve got a database that maintains Jobs to be processed by various processing machines. Its basic schema is thus:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| ID          | int(11)      | NO   | PRI | NULL    | auto_increment |
| EndTime     | datetime     | YES  |     | NULL    |                |
| GroupID     | varchar(255) | NO   | MUL | NULL    |                |
| HostAddress | varchar(15)  | YES  |     | NULL    |                |
| StartTime   | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

ID is auto-increment, HostAddress represents the processing machine which has claimed this Job, StartTime represents the beginning of the most recent attempt at processing it, EndTime is the time at which it successfully completed processing, and GroupID is an arbitrary string for referencing other tables with.

All of the processing machines synchronize around this table for grabbing work. New records are only inserted manually, although all of the processing machines can update existing records. The idea was to have a processing machine do the following whenever it’s out of work:

  • See if any jobs belong to it (HostAddress = its IP) and have yet to be started.
  • If there are none, see if any jobs have yet to be claimed (HostAddress IS NULL).
  • If there are unclaimed jobs, claim some (update HostAddress to its IP).
  • Process all jobs that belong to it (same check as #1 except that we may have added some via #3).

I had thought that this sequence of operations would cause the database to synchronize different machines’ attempts at the same job for me; even if both machines tried to claim the same job at the same time, only one of their IPs would end up in the HostAddress column, so when they asked again for all jobs at their HostAddress only one of them would get that job back.

But that does not appear to be the case. When starting 35 processing machines nearly simultaneously last night, I observed multiple cases of multiple machines processing the same job, even though only one of them ended up with it claimed in the database. That implies to me that the last check isn’t working properly. Here’s the more concrete version of what I’m doing. The database calls use em.createNamedQuery which I’m just going to summarize below them for brevity. JPA is being provided by Hibernate 3.6.8 and the database is MySQL 5.1.61.

protected void poll(EntityManager em) {
    List<JobRecord> candidates = null;
    //Synchronized only for this machine. Others are running concurrently.
    synchronized (em) {
        //Check if anything is already claimed by us.
        candidates = JobRecord.selectReady(em);
        //SELECT record FROM JobRecord record WHERE HostAddress=[IP]
        //    AND StartTime IS NULL AND EndTime IS NULL;
            if (candidates.isEmpty()) {
            //None claimed. Check if any jobs aren't claimed by anyone.
            candidates = JobRecord.selectAvailable(em);
            //SELECT record FROM JobRecord record WHERE HostAddress IS NULL
            //    AND StartTime IS NULL AND EndTime IS NULL;
            if (candidates.isEmpty()) {
                //All jobs have been processed.
                return;
            }
            //Claim these jobs we found for ourselves.
            em.getTransaction().begin();
            for (JobRecord job : candidates) {
                job.setStartTime(null);
                job.setEndTime(null);
                job.setHostAddress([IP]);
                em.merge(job);
            }
            em.getTransaction().commit;
            //Only process what is actually claimed by us; could be nothing.
            candidates = JobRecord.selectReady(em);
            //(The first query again.)
        }
    //Do processing with candidates list.
}

The only explanation that comes to mind is that when I do the em.getTransaction().commit the results are cached somehow, and that when I do the selectReady NamedQuery just after it, it’s returning the cached result without bothering to consult the database. But that might not even be the case, and I’m not sure I could prove that. There might even be something fundamentally flawed with my scheme that I’m overlooking.

So, to actually pose my question, why did this database synchronization routine fail and what can I do to correct it?

  • 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-06-03T10:50:26+00:00Added an answer on June 3, 2026 at 10:50 am

    Multiple machines can invoke selectAvailable() before any of them perform the UPDATE transaction. Consequently, they may each think that the same jobs are available.

    You need to begin the transaction before the selectAvailable() call, which should use SELECT ... FOR UPDATE in order to lock the available job records so that no other database connection can read from them until the transaction is committed.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need to clean up various Word 'smart' characters in user input, including but
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.