Reputation: 6825
I am using Spring and Hibernate. I have a method annotated with @Transactional
. This method has two database calls. One call would update the data in the table and the other call would retrieve some data from the same table based on the first call's updated data.
The problem is first call's database change is not reflected immediately. Change is reflected after the flow comes out of the method which is annotated with @Transactional
. I still tried calling session.flush()
but no use. Please suggest me.
@Transactional
public void method1(){
dao.updateM1();
dao.getData();
}
Upvotes: 0
Views: 2613
Reputation: 1065
It's not suggest calling session.flush()
manually, I think you can separate the two calls with dao to two different service method, make them have different transaction propagation attributes,
@Transactional
public void method1(){
this.updateM1Service();
this.getDataService();
}
@Transactional (propagation = Propagation.REQUIRE_NEW)
public void updateM1Service(){
dao.updateM1();
}
@Transactional
public void getDataService(){
dao.getData();
}
Upvotes: 0
Reputation: 1645
Could you elaborate on your scenario? For example: what propagation level did you declare for updateM1 and getData methods?
In case, you didn't describe Transactional for updateM1, and getData methods, the problem is both methods are in one same transaction. As a result, Hibernate won't update the data until the transaction is committed. To solve that problem, you just describe the transaction for updateM1 as below:
@Transactional (propagation = Propagation.REQUIRE_NEW)
public E updateM1() {}
With that, a new transaction will be created every time updateM1 is called. When the updateM1 is finished, that new transaction is committed and all changes will be persisted in the database.
About session.flush not working, here is one clear answer: Question about Hibernate session.flush()
Upvotes: 2
Reputation: 6657
Why is it important what's updated in the database?
Think rather about the entities' state in the persistence context.
Perhaps you should try session.refresh()
to refresh the entities' state but your code is not enough detailed to see why it doesn't work.
How are your update
and get
methods implemented?
Upvotes: 0
Reputation: 4562
As far as I know, when you you use @Transaction
annotation on method. Any database change in the logic of your body should be committed at the end of method call.
Upvotes: 0
Reputation: 40358
In my knowledge After completion of transaction method only the changes will reflect in the database.
But here you are calling before completion of the method.So you are not getting the updated result.
Upvotes: 1