我在春天理解@Transactional
注释时遇到问题,并且共同知道它是如何工作的。
我有这个片段(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();
}
}
请阅读代码中的注释并修复我的错误。
感谢。