java - Transaction Isolation Level -
java - Transaction Isolation Level -
i seek describe problem in jpa transaction isolation levels.
database structure:
table1 -> pk defined date ('ddmmyyyy') table2 -> fk table1 jpa( isolation level :: read_commited) - code:
query query = em.createquery("from table1 trd trd.id = :d"); query.setparameter("d", date); table1 t = null; try{ t = (table1) query.getsingleresult(); }catch(javax.persistence.noresultexception e){ t = null; } if(t==null){ t=new table1 (date); em.persist(trd); } for(table2 q:tables2){ q.settable1(t); em.merge(q); } so procedure check if record exists in db , if not create new one. method corect if scheme in based on 1 thread. otherwise there possible situation this:
thread 1 : check if entity represent date exists in database thread 2 : sameboth of them think such record has not exist yet, add together new one. ok until moment of commiting transactions. first 1 commited without exception, purchase sec 1 rising exception related primary key duplication.
is possibility preserve such situation except changing isolation level serializable?
yes, in general isolation level = serializable should solve problem. not underestimate side effects when changing isolation level stiff option. may influence database utilization request throughput. maybe can explicitly commit trx after creating t1, open trx: entitymanager.gettransaction().commit()
you still have grab duplicate key exception.
java database hibernate jpa transactions
Comments
Post a Comment