我已经使用Speedment实现了持久化层,我想使用spring引导单元测试来测试代码。我用以下注释注释了我的单元测试:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class MovieServiceTest {
...
}默认情况下,Spring将围绕每个测试方法启动一个新事务,并在回调结束时执行事务回滚。然而,对于Speedment,这似乎不起作用。
Speedment是否支持跨几个调用的事务,如果是,如何配置Spring以使用Speedment事务,或者如何配置Speedment以使用Spring提供的数据源?
发布于 2017-11-18 14:47:02
在Speedment 3.0.17中添加了事务支持。但是,它尚未与Spring @Transactional-annotation集成,因此您必须将要执行的代码包装为单个事务如图所示。
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();
}发布于 2017-11-21 18:52:16
很可能是在表上进行流,然后在流仍然打开时执行更新/删除操作。大多数数据库无法处理在ResultSet上打开的Connection,然后在同一连接上执行更新操作。
幸运的是,有一个简单的工作:考虑收集您想要在中间Collection (如List或Set)中修改的实体,然后使用该Collection执行所需的操作。
这种情况在“速度用户指南”( Speedment User's Guide 这里 )中作了说明
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();
}
);发布于 2017-10-31 08:05:34
AFAIK它还没有(还)纠正:它似乎设置了每个流/语句一个事务。
参见本文: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
https://stackoverflow.com/questions/47015196
复制相似问题