Search code examples
speedment

Does Speedment support transactions?


I have implemented the persistence layer using Speedment and I would like to test the code using spring boot unit tests. I have annotated my unit tests with the following annotations:

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

By default, Spring will start a new transaction surrounding each test method and @Before/@After callbacks, performing a roll back of the transaction at the end. With Speedment however this does not seem to work.

Does Speedment support transactions across several invocations, and if yes, how do I have to configure Spring to use the Speedment transactions or how doe I have to configure Speedment to use the data source provided by Spring?


Solution

  • Transaction support was added in Speedment 3.0.17. However, it does not integrate with the Spring @Transactional-annotation yet so you will have to wrap the code you want to execute as a single transaction 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();
    }