What i do
I have a async
system which react whenever a new entity is created in main
system and this async
system query the database for the newly created entities.
How i do it
For creating the msgs for async
system i use hibernate interceptor
.
So whenever a new entity is created the onSave
method of the interceptor is called and i
save the entity id in a list
, and now when afterTransactionComplete(Transaction tx)
is
called , i flush all the entity id
in the list
to async
system using a messaging system(ActiveMq).
What is the problem
Now the problem
arises when there are more then one transaction are in process.. and both
transactions creates entities.
Let me take a example:
Tx_A
create entity EA1, EA2
.
Tx_B
creates entity EB1, EB2
.
Now let say execution flow happens this way:
[1] onSave for EA1, i add EA1 id to flushList
[2] onSave for EB1, i add EB1 id to flushList
[3] afterTransactionComplete(tx) for Tx_A
Now at this point i will flush EA1 and EB1
id to async
system which when query database
for EB1
found null as transaction Tx_B
is still not completed.
Now this issue can be solve if in onsave
call i can get the transaction id and then in afterTransactionCompletion
i can flush only entity related to that transaction
[1] Now Is there a way to get this transaction id ?
[2] Is there some other solution for the above problem ?
i am using hibernate 4.2.2