我在理解@Transactional
spring 中的注释以及它的工作原理方面遇到了问题。
我有这个片段(VacancyService 类标记为事务性):
@TransactionConfiguration(defaultRollback = false)
@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
public class AbstractServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
....
@Test
public void delete(){
//here session opens and transaction starts
vacancyService.delete(vacancy.getId());
sessionFactory.getCurrentSession().flush();
Vacancy vacancyFromDb = vacancyService.findById(vacancy.getId());
sessionFactory.getCurrentSession().flush();
Assert.assertNull(vacancyFromDb);
//here transactions commit and session close
}
}
第二个片段:
@Controller
public class VacancyMenuController extends AbstractController{
...
@RequestMapping("/path")
public String delete(Model model){
//here session opens and transaction starts
vacancyService.delete(vacancy.getId());
//here transactions commit and session close
sessionFactory.getCurrentSession().flush();
//here session opens and transaction starts
Vacancy vacancyFromDb = vacancyService.findById(vacancy.getId());
//here transactions commit and session close
sessionFactory.getCurrentSession().flush();
}
}
请阅读代码中的注释并修复我的错误。
谢谢。