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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:19:44+00:00 2026-06-16T05:19:44+00:00

Suppose I have a game of chess stored in a SQL database with something

  • 0

Suppose I have a game of chess stored in a SQL database with something like the following schema:

CREATE TABLE chessgames(
    game_id INTEGER,
    move_id INTEGER,
    move char(4)
};

So if an ongoing game, with game_id 0, has the moves e4 e5 then the table would have the tuples (0, 1, “e4”) and (0, 2, “e5”).

Now suppose a client tries to corrupt the database by sending both the move d4 and Nf3 at the same time. Trying to get two moves processed and effectively trying to insert the tuples (0, 3, “d4”) and (0, 3, “Nf3”), both with the same move_id, breaking the uniqueness of move_id.

What is the best idiomatic way to ensure uniqueness? One possibility that occurred to me would be to have my C++ code contain a list of mutexes, one mutex for each game. When a move such as d4 arrives, the C++ code locks the mutex for the corresponding game, runs the following SQL query

SELECT move_id, move FROM chessgames WHERE game_id = 0

to fetch all the moves for the game (in the example I gave this would be e4 and e5), the C++ code takes those moves and checks that no row already has move_id = 3 and then plays out the moves to construct the current position so that it can check that the move d4 is valid. If it is valid it runs

INSERT INTO chessgames VALUES (0, 3, "d4")

to store the move in the database and then it frees the mutex.

This way if the Nf3 move arrives at the same time as the d4 move processing of it will get blocked by the locked mutex and when Nf3 is finally processed it will see that a row with move_id = 3 already exists and it will be ignored.

Is there a better way of doing this? Is my database schema even reasonable for what I am trying to do?

  • 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-16T05:19:45+00:00Added an answer on June 16, 2026 at 5:19 am

    I learned that what I need is optimistic locking much like the answer to this
    question.

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

Sidebar

Related Questions

Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
Suppose I have a GAME table with the following fields user_id, result I wish
Suppose I have the following table GAME match_id, user_id, score 1, 10, 45 1,
Suppose I have following data in my game. I have developed game in cocos2d.
Suppose I have a table with following data: gameId difficultyLevel numberOfQuestions -------------------------------------------- 1 1
Suppose you have a database schema with tournaments related to games. A tournament may
Suppose I have a flash application; let's say a chess game. The user is
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have a game written in C/C++. For simplicity, I have: struct masterStruct
I currently have a GAME table with two fields user_id, win win = 1

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.