Speedment 是否支持交易?

Does Speedment support transactions?

我已经使用 Speedment 实现了持久层,我想 使用 spring 启动单元测试测试代码。我用以下注释对我的单元测试进行了注释:

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class MovieServiceTest {
  ...
}

默认情况下,Spring 将围绕每个测试方法和@Before/@After 回调启动一个新事务,最后执行事务回滚。然而,对于 Speedment,这似乎不起作用。

Speedment 是否支持跨多个调用的事务,如果是,我必须如何配置 Spring 才能使用 Speedment 事务,或者我必须如何配置 Speedment 才能使用 Spring?

据我所知,它还没有——更正:它似乎为每个流/语句设置一个事务。

查看这篇文章:https://dzone.com/articles/best-java-orm-frameworks-for-postgresql

但是应该可以通过编写自定义扩展来实现:https://github.com/speedment/speedment/wiki/Tutorial:-Writing-your-own-extensions

编辑:

根据速度开发人员的说法,一个流映射到一个事务:https://www.slideshare.net/Hazelcast/webinar-20150305-speedment-2

Speedment 3.0.17 中添加了事务支持。但是,它尚未与 Spring @Transactional 注释集成,因此您必须将要执行的代码包装为单个事务 like shown here:

txHandler.createAndAccept(tx ->

    Account sender = accounts.stream()
        .filter(Account.ID.equal(1))
        .findAny()
        .get();

    Account receiver = accounts.stream()
        .filter(Account.ID.equal(2))
        .findAny()
        .get();

    accounts.update(sender.setBalance(sender.getBalance() - 100));
    accounts.update(receiver.setBalance(receiver.getBalance() + 100));

    tx.commit();
}

您可能正在通过 table 进行流式传输,然后在流式传输仍处于打开状态时进行 update/remove 操作。大多数数据库无法处理在 Connection 上打开 ResultSet,然后在同一连接上执行更新操作。

幸运的是,有一个简单的解决方法:考虑在中间 Collection(例如 ListSet)中收集您想要修改的实体,然后使用Collection 执行所需的操作。

这种情况在 Speedment 用户指南中有描述here

txHandler.createAndAccept(
    tx -> {
       // Collect to a list before performing actions
        List<Language> toDelete = languages.stream()
            .filter(Language.LANGUAGE_ID.notEqual((short) 1))
            .collect(toList());

        // Do the actual actions
        toDelete.forEach(languages.remover());

        tx.commit();
    }
);