0

I am inserting the data in one method(has @Transactional(propagation = Propagation.Required)) but in the other method(has @Transactional(propagation = Propagation.Required)) if I try to get the same data it is giving null.

Both methods are wrote in service layer with @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)

How to get the data which is inserted in the same transaction. Something like :-

 @Service
    public class Service{
        @Transactional
        public void method(){
            mapper.insert();        //insert to DB(Using Mapper interface)
            ServiceLayer.method2()
        }
    }

@Service
public void ServiceLayer{

    @Transactional
    public static void method2(){
        result  = mapper.select() //Select inserted data - returning null
    }
}
Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52

3 Answers3

1

In order to persist the changes made to the current session you can invoke entityManager.flush();

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
0

It may be worked, but it's not a solution. In your case, your Transaction from Service.method() created a transaction that is not committed yet. That's why you can't fetch it.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
NikolaP
  • 1
  • 4
-2

I found the answer...after removing @transactional from ServiceLayer.method2() it's worked fine.

Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52