6
I’m having a competition control problem in entering data in my web application.
Context: I have 3 tables (ex: X, Y and Z) that record hospitalizations of patients (already registered). A patient cannot have more than one ACTIVE hospitalization simultaneously.
Currently, my application only checks if there is already some active hospitalization for that patient before starting the transaction that makes the Inserts in tables X, Y and Z.
However this verification does not work when two or more users try to admit two patients at the same time.
Currently the treatment is done so:
1 - verifica se o paciente está internado (SELECT na tabela X);
Se não:
inicia transação A;
2 - INSERT na tabela X;
3 - INSERT na tabela Y;
4 - INSERT na tabela Z;
finaliza transação A;
As I said earlier, check-in 1 occurs to prevent patients being admitted more than once at the same time. However, if two (or more) users try to admit the same patient at the same time, the 1 check-up does not work.
What I thought of solution:
I thought of something that involves blocking SELECT in 1, so that it will only be executed when the A transaction is completed. In this case, when executed, SELECT would identify that the patient is already registered.
I would like to address this problem using LOCK in the database. I use Postgresql and, for what I studied, I would need to use ACCESS EXCLUSIVE (only that blocks SELECT)
How to do this using Hibernate?
Note: I have already analyzed and cannot deal with constraints in the database
Why it is not possible to treat with Constraint?
– Dherik