使用 Spring Data Repository 限制交易数量
Limiting number of transactions with Spring Data Repository
我正在使用 Spring 数据存储库编写 Spring 启动应用程序。我有重置数据库并用示例数据填充它的方法。它有效,但是 Spring 使用数百个事务来执行此操作。有没有办法将存储库创建的事务数限制为 1 或根本不使用它们?
我想在 fillApples
和 fillBananas
方法中重复使用相同的事务。我试过使用 @Transactional(propagation = Propagation.SUPPORTS)
的不同组合,但它没有改变任何东西。
interface AppleRepository extends CrudRepository<Apple, Long>
interface BananaRepository extends JpaRepository<Banana, Long>
@Service
public class FruitService{
@Autowired
private final AppleRepository appleRepository;
@Autowired
private final BananaRepository bananaRepository;
public void reset(){
clearDb();
fillApples();
fillBananas();
//more fill methods
}
private void clearDb(){
appleRepository.deleteAll();
bananaRepository.deleteAll();
}
private void fillApples(){
for(int i = 0; i < n; i++){
Apple apple = new Apple(...);
appleRepository.save(apple);
}
}
private void fillBananas(){
for(int i = 0; i < n; i++){
Banana banana = new Banana(...);
bananaRepository.save(banana);
}
}
}
@RestController
public class FruitController{
@Autowired
private FruitService fruitService;
@RequestMapping(...)
public void reset(){
fruitService.reset();
}
}
您必须使用 @Transaction
和传播配置来注释您的 reset()
方法,以确保该方法在事务中运行(创建或重用现有注释 - 例如传播 REQUIRED
(@Transactional 的默认值))
您的代码没有 @Transactional
但在您的评论中您写道您有一个,但是您使用 "wrong" Propagation = SUPPORTS
。因为SUPPORTS
的意思是:
SUPPORTS Support a current transaction, execute non-transactionally if none exists.
所以如果有 none (@Transactional(propagation = SUPPORTS)
永远不会做任何事情,这意味着什么都不做)
所以你必须使用@Transactional(propagation = REQUIRED)
@Transactional(propagation = REQUIRED)
public void reset(){
clearDb();
fillApples();
fillBananas();
//more fill methods
}
@see: Propagation java doc
我正在使用 Spring 数据存储库编写 Spring 启动应用程序。我有重置数据库并用示例数据填充它的方法。它有效,但是 Spring 使用数百个事务来执行此操作。有没有办法将存储库创建的事务数限制为 1 或根本不使用它们?
我想在 fillApples
和 fillBananas
方法中重复使用相同的事务。我试过使用 @Transactional(propagation = Propagation.SUPPORTS)
的不同组合,但它没有改变任何东西。
interface AppleRepository extends CrudRepository<Apple, Long>
interface BananaRepository extends JpaRepository<Banana, Long>
@Service
public class FruitService{
@Autowired
private final AppleRepository appleRepository;
@Autowired
private final BananaRepository bananaRepository;
public void reset(){
clearDb();
fillApples();
fillBananas();
//more fill methods
}
private void clearDb(){
appleRepository.deleteAll();
bananaRepository.deleteAll();
}
private void fillApples(){
for(int i = 0; i < n; i++){
Apple apple = new Apple(...);
appleRepository.save(apple);
}
}
private void fillBananas(){
for(int i = 0; i < n; i++){
Banana banana = new Banana(...);
bananaRepository.save(banana);
}
}
}
@RestController
public class FruitController{
@Autowired
private FruitService fruitService;
@RequestMapping(...)
public void reset(){
fruitService.reset();
}
}
您必须使用 @Transaction
和传播配置来注释您的 reset()
方法,以确保该方法在事务中运行(创建或重用现有注释 - 例如传播 REQUIRED
(@Transactional 的默认值))
您的代码没有 @Transactional
但在您的评论中您写道您有一个,但是您使用 "wrong" Propagation = SUPPORTS
。因为SUPPORTS
的意思是:
SUPPORTS Support a current transaction, execute non-transactionally if none exists.
所以如果有 none (@Transactional(propagation = SUPPORTS)
永远不会做任何事情,这意味着什么都不做)
所以你必须使用@Transactional(propagation = REQUIRED)
@Transactional(propagation = REQUIRED)
public void reset(){
clearDb();
fillApples();
fillBananas();
//more fill methods
}
@see: Propagation java doc