Get the data which is inserted using @Transactional in the same transaction
Asked Answered
H

3

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
    }
}
Harlin answered 21/3, 2016 at 13:43 Comment(0)
H
-2

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

Harlin answered 11/5, 2016 at 7:24 Comment(0)
R
1

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

Rubinstein answered 21/3, 2016 at 14:21 Comment(1)
How?? i am using spring-mybatis.Harlin
K
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.

Kamasutra answered 9/8, 2021 at 14:4 Comment(0)
H
-2

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

Harlin answered 11/5, 2016 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.