在以下代碼中調用時,當methodInner()從內methodOuter調用,應當根據事務邊界。但事實並非如此。 但是當methodInner()直接從MyController類中調用時,它被事務綁定。任何解釋?彈簧@Transactional不用於註解的方法工作,從服務類
這是控制器類。
@Controller
public class MyController {
@Autowired
@Qualifier("abcService")
private MyService serviceObj;
public void anymethod() {
// below call cause exception from methodInner as no transaction exists
serviceObj.methodOuter();
}
}
這是服務類。
@Service("abcService")
public class MyService {
public void methodOuter() {
methodInner();
}
@Transactional
public void methodInner() {
.....
//does db operation.
.....
}
}
您是否已將@EnableTransactionManagement添加到您的配置中? – 2014-09-26 11:08:47
按預期工作,因爲這是代理如何工作的方式,只有外部方法調用(即調用INTO對象)被攔截,來自對象內部的調用不會被攔截。 – 2014-09-26 11:11:35
http://stackoverflow.com/a/24902434/2504224用@ Transactional替換'@ Async'並有效地得到你的答案 – geoand 2014-09-26 11:11:43