I have service like this:
class ServiceImpl implements Service {
@Override
@Transactional
public int method() {
//some logic in transaction
method2();//I want run this without transaction
return 2;
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void method2() {
//external api call
}
}
How can I run method2 without transaction or in new transaction? Can I run in my controller class this 2 method like this:
service.method();
service.method2();
@Transactional is powered by Aspect-Oriented Programming. Therefore, processing occurs when a bean is called from another bean. You can resolve this problem by
class ServiceImpl implements Service {
@Lazy private final Service self;
@Override
@Transactional
public int method() {
//some logic in transaction
self.method2();//I want run this without transaction
return 2;
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void method2() {
//external api call
}
}
class ServiceImpl implements Service {
private final ExternalService service;
@Override
@Transactional
public int method() {
//some logic in transaction
method2();//I want run this without transaction
return 2;
}
@Override
public void method2() {
service.method2();
}
}
@Service
class ExternalService{
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void method2() {
//external api call
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.