Spring测试事务回滚问题

发布于 2024-11-19 23:00:36 字数 2134 浏览 3 评论 0原文

我在理解 @Transactional 和 @TransactionConfiguration(defaultRollback = true) 注释时遇到问题。

我有一个测试代码,它将 userAccount 插入数据库,然后应插入另一个同名帐户,这应该会导致 DataIntegrityViolationException,因为 AccountName 被标记为 Unique。如果未在 TestClass 级别指定 @Transactional 和 @TransactionConfiguration(defaultRollback = true),则此方法可以正常工作。但是,如果启用了回滚,我不会收到异常,因为即使在相同的方法中,数据也不会插入到数据库中。如果我在插入第一个帐户后设置断点,数据库仍然是空的。

这是我的代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class DaoTests {

    @Autowired
    private Repository repo;
    @Autowired
    private AccountService userService;

    @Before
    public void orgInstAccount() {
        Organization o = new Organization();
        o.setName("Organisation 1");
        repo.saveEntity(o);

        Institution i1 = new Institution();
        i1.setName("xyz");
        i1.setOwningOrganization(o);
        repo.saveEntity(i1);

    }

    @Test(expected = DataIntegrityViolationException.class)
    public void saveUserFail() {

        Account user = new Account();
        user.setAccountname("chz");
        user.setPassword(userService.calcMD5Hash("123"));
        user.setOwningInstitution(repo.getInstitutionByName("xyz"));
        repo.saveEntity(user);
        Assert.assertNotNull(repo.getAccountByName("chz"));


        Account userNew = new Account();
        userNew.setAccountname("chz");
        userNew.setPassword(userService.calcMD5Hash("123"));
        userNew.setOwningInstitution(repo.getInstitutionByName("xyz"));
        repo.saveEntity(userNew);
        //Here the Exception should be thrown but everything works fine.

    }

}

存储库实现是:

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class RepositoryHibernateImpl implements Repository {

    @Autowired
    private SessionFactory factory;

    @Override
    public void saveEntity(Entity hce) {
        factory.getCurrentSession().save(hce);
    }
}

也许问题是因为存储库和 TestClass 标记有 @Transactional?

先感谢您。

i have a Problem understanding the @Transactional and @TransactionConfiguration(defaultRollback = true) Annotations.

I have a test Code which inserts a userAccount into the Database, then another account with the same name should be inserted, which should result in a DataIntegrityViolationException because the AccountName is marked as Unique. This works fine if @Transactional and @TransactionConfiguration(defaultRollback = true) is not sepcified at the TestClass level. But if Rollback is enabled i don't get the Exception because even in the same method the Data is not inserted into the databasse. If i set a breakpoint after inserting the first Account, the Database is still empty.

Here is my Code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class DaoTests {

    @Autowired
    private Repository repo;
    @Autowired
    private AccountService userService;

    @Before
    public void orgInstAccount() {
        Organization o = new Organization();
        o.setName("Organisation 1");
        repo.saveEntity(o);

        Institution i1 = new Institution();
        i1.setName("xyz");
        i1.setOwningOrganization(o);
        repo.saveEntity(i1);

    }

    @Test(expected = DataIntegrityViolationException.class)
    public void saveUserFail() {

        Account user = new Account();
        user.setAccountname("chz");
        user.setPassword(userService.calcMD5Hash("123"));
        user.setOwningInstitution(repo.getInstitutionByName("xyz"));
        repo.saveEntity(user);
        Assert.assertNotNull(repo.getAccountByName("chz"));


        Account userNew = new Account();
        userNew.setAccountname("chz");
        userNew.setPassword(userService.calcMD5Hash("123"));
        userNew.setOwningInstitution(repo.getInstitutionByName("xyz"));
        repo.saveEntity(userNew);
        //Here the Exception should be thrown but everything works fine.

    }

}

The Repository Implementation is:

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class RepositoryHibernateImpl implements Repository {

    @Autowired
    private SessionFactory factory;

    @Override
    public void saveEntity(Entity hce) {
        factory.getCurrentSession().save(hce);
    }
}

Maybe the Problem is because the Repository and the TestClass are marked with @Transactional?

Thank you in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-11-26 23:00:36

调用flush(),它将尝试立即调用sql,而不是将其推迟到事务边界

factory.getCurrentSession().flush()

Call flush() which will try to call sql immediatly instead of deferring it till transaction boundary

factory.getCurrentSession().flush()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文