I would like to describe the problem I got in JPA / MySQL; it may inspire your research ...
- Start a global transaction
- transaction 1) a new row in the table Address (auto-increment)
- transaction 2) a new row on the Entreprise table with a foreign key on the Addres table; The new Entrepreneur Entered is associated with the new address # ID.
- End of global transaction
MYSQL dead-locks for this case with ResourceLocal / JPATransactionManager.
In fact, it seems that we cannot open several nested transactions. A global transaction seems to be combined with transactions 1) and 2). Transaction 2) ends in a dead end because the data cannot be loaded by the table. New #Id that is not ready.
However, we can see with the debugger a new line adresse # id between transactions 1 and 2.
Does this sound like your problem? Do you assume auto-increment is related to your dead end? These are the following possible solutions ...
Solution1 Change isolation level? -> How? !! I have no answer ... And I'm not sure if this will change anything.
Solution2 Replace the strategy for generating JPA object identifiers (auto or identifier) ββin the user sequence table.
Solution3
Check if you can use a cascading strategy for ManyToOne relationships.
EntrepriseEntity{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "id_entreprise") private int id; @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL) @JoinColumn(name = "id_address") private AddressEntity address;
And then save both lines in one merge ():
EntrepriseEntity e=new EntrepriseEntity(); e.setAddress(new AddressEntity()); e=entityManager.merge(e);
Returned instance returning new inserted #ids and magic: no longer deadlock ...
Solution # 3 is smarter, but requires more in-depth analysis and code changes ...
source share