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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:57:15+00:00 2026-05-26T16:57:15+00:00

I have a scenario in which I think I need to lock a table

  • 0

I have a scenario in which I think I need to lock a table against all activity (reads and writes). I am using Propel 1.6.x and intend to write a small extra library to handle the locking.

My use case is a version table that has a primary key of (id, creator, version). The column pair (id, creator) is actually a PK on another table. When a new row is inserted, the MAX(version) is read in PHP code and then inserted into a row class for later saving. Between the max and the save, there is a race condition potential where another process could get the same max value, and then of course one of the inserts would fail due to non-uniqueness.

To avoid the need for locks, I would have preferred to use a subselect for the save, something like:

INSERT INTO
    test_model_test_organiser_versionable
(id, creator, version)
VALUES (
    3,
    1, 
    (
        SELECT COALESCE(MAX(version), 1)
        FROM test_model_test_organiser_versionable
        WHERE id = 3
        AND creator = 1
    )
);

However this isn’t supported in Propel, and if I do it through PDO I won’t (afaik) have a way of discovering the value given to the ‘version’ part of the composite PK.

Now, I could have an additional auto-incrementing column, since the auto-incremented value could be read after the insert to look up the row, and hence I can do a reselect in Propel. But I would like to avoid the extraneous column if I can help it – seems less than elegant in my opinion.

So… my feeling is that wrapping the max & save calls in a lock is the way to go. This would have to lock the whole table IMO, and guard against reads as well as writes (otherwise a subsequent max call would not wait, and hence would lead to a non-uniqueness fail again).

I need this to work with all the platforms supported by Propel, so – unless there is a better approach I should take – is there a PHP library that will do this locking against any PDO database? If not, I can do it myself – but it is nice if someone else has done the testing donkey-work against a variety of dbs (set up MSSQL Server? – no thanks 🙂

  • 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-26T16:57:15+00:00Added an answer on May 26, 2026 at 4:57 pm

    I went with locking the table completely whilst the MAX() and INSERT/UPDATE is done. I found these worked:

    • MySQL: LOCK TABLES my_table WRITE
    • PostgreSQL: LOCK TABLE my_table IN SHARE ROW EXCLUSIVE MODE

    I plan to look up and test the same for Oracle, MSSQL and SQLite in due course – but the first two are probably the most popular for F/OSS projects.

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

Sidebar

Related Questions

I have a scenario where i have one control, which is using another control
I have a scenario which I think I can convey giving PC Monitor example
I have a scenario in which I'm serving a file from codebehind. which file,
I have a scenario in which I have REST API which manages a Resource
I have implement a scenario which involves two way communication between child and parent
we have this scenario: A server which contains needed data and client component which
I have a performance run scenario which calls cd %AUTORUN_DIR% call abc.bat cd %AUTORUN_DIR%
I have a very simple scenario which I cannot get to work. I am
I am new to RSpec and I have a test scenario in which I
I have a scenario in a system which I've tried to simplify as best

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.