Jump to content

Synchronizing Php And Protecting Mysql Db Access


nachumk
 Share

Recommended Posts

I am trying to add a new user to my database. I check the users table to find last id used and i increment it by one. I then add that user to the users table. I have to add that user to a second table too.I figure that I need to use a PHP semaphore or some synchronization object to make sure that I don't get a race condition during these 3 steps (if another user is signing up at the same time). Is there a better way to do this?How can I protect the db if the system dies between updating the users table and updating the second table? I am using mysql with innodb. What if the first or second update fails? Should I be using transactions / rollbacks?Thank you,

Link to comment
Share on other sites

Why not just use an AUTO_INCREMENT field for the ids? If you're really paranoid you can use a transaction for the two queries, but servers don't fail as often as you seem to think.

Link to comment
Share on other sites

And this is why I ask ... AUTO_INCREMENT is perfect for what I need.Then I will use transactions to accomplish the atomic inserts.I don't assume systems will crash often, but I am learning and this is how I learn. I ask the difficult questions to get an in depth understanding. I also like to design when possible with all scenarios in mind. I would hope that everyone programs this way.Thanx

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...