|
| 1 | += Transactions |
| 2 | + |
| 3 | +[quote,https://docs.spring.io/spring-framework/reference/data-access/transaction.html[spring - Transaction Management]] |
| 4 | + |
| 5 | +____ |
| 6 | +Comprehensive transaction support is among the *most compelling reasons* to use the Spring Framework. |
| 7 | +____ |
| 8 | + |
| 9 | +That's why we implemented *Spring Transactions*. |
| 10 | + |
| 11 | +Just like Spring JPA you can use https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative.html[declarative] or https://docs.spring.io/spring-framework/reference/data-access/transaction/programmatic.html[programmatic] transaction management. |
| 12 | + |
| 13 | +[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/VetService.java[Declarative example from complex demo]"] |
| 14 | +---- |
| 15 | +import org.springframework.stereotype.Service; |
| 16 | +import org.springframework.transaction.annotation.Transactional; |
| 17 | +//... |
| 18 | +@Service |
| 19 | +@Transactional |
| 20 | +public class VetService |
| 21 | +{ |
| 22 | + //... |
| 23 | + public void saveNewEntries() |
| 24 | + { |
| 25 | + final Vet vet = this.createVet(); |
| 26 | + this.vetRepository.save(vet); |
| 27 | + } |
| 28 | + //... |
| 29 | +---- |
| 30 | + |
| 31 | +[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/OwnerService.java[Programmatic example from complex demo]"] |
| 32 | +---- |
| 33 | +import org.springframework.stereotype.Service; |
| 34 | +import org.springframework.transaction.PlatformTransactionManager; |
| 35 | +//... |
| 36 | +@Service |
| 37 | +public class OwnerService |
| 38 | +{ |
| 39 | + private final PlatformTransactionManager transactionManager; |
| 40 | +
|
| 41 | + @Autowired |
| 42 | + public OwnerService( |
| 43 | + final OwnerRepository ownerRepository, |
| 44 | + final PlatformTransactionManager transactionManager) |
| 45 | + { |
| 46 | + this.ownerRepository = ownerRepository; |
| 47 | + this.transactionManager = transactionManager; |
| 48 | + } |
| 49 | + //... |
| 50 | + public void deleteAll() |
| 51 | + { |
| 52 | + new TransactionTemplate(this.transactionManager).execute( |
| 53 | + status -> |
| 54 | + { |
| 55 | + this.ownerRepository.deleteAll(); |
| 56 | + return null; |
| 57 | + }); |
| 58 | + } |
| 59 | + //... |
| 60 | +---- |
| 61 | + |
| 62 | +CAUTION: If you are using transaction, you need to define a ``Bean`` for ``PlatformTransactionManager``! This is easiest achieved by extending the ``EclipseStoreClientConfiguration``. See https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/ComplexConfiguration.java[the complex demo]. |
0 commit comments