I have a table in MySQL that contains a special ID in a particular column. My requirement is to get last inserted ID value (Select... ORDER BY ID DESC LIMIT 1
), generate a new value based on the acquired ID value and then insert a new record with this generated value.
The problem is that two users will be able to read last inserted ID concurrently and generate two identical IDs.
What I need to do is avoid concurrent read access across the entire table for other threads when one thread is already reading the value.
Is there a way I can achieve this in MySQL without using stored procedures? I've already synchronized the method in my Java application (using spring JdbcTemplate
) to read/write to db.
Would synchronizing Java method be a good way to implement this behavior or should I use stored procedures to get the behavior?