Search code examples
javaspringspring-mvcspring-mybatis

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


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
    }
}

Solution

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