I have an application running on two different server instances. Application has a singleton EJB which has timer set to run after every one minute. The method in EJB reads records from table deletes it and publishes it over the queue (all part of one transaction). Once both servers started it tries to read the record from table simultaneously.
My question is how to allow only one application to read records until the transaction is over.
You can use select for update:
This will block the selected records for the time of transaction, i.e. the same queries issued by other server will wait until transaction is committed.
https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html