|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.demo; |
| 17 | + |
| 18 | +import org.springframework.data.couchbase.core.CouchbaseTemplate; |
| 19 | +import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations; |
| 20 | +import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; |
| 21 | +import org.springframework.data.couchbase.core.TransactionalSupport; |
| 22 | +import org.springframework.stereotype.Service; |
| 23 | +import org.springframework.transaction.annotation.Transactional; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Michael Reiche |
| 28 | + */ |
| 29 | +@Service |
| 30 | +public class AirlineGatesService { |
| 31 | + CouchbaseTemplate template; |
| 32 | + ReactiveCouchbaseTemplate reactiveTemplate; |
| 33 | + public AirlineGatesService(CouchbaseTemplate template) { |
| 34 | + this.template = template; |
| 35 | + this.reactiveTemplate = template.reactive(); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + // The @Transactional annotation results in the method of the proxy for the service executing this in a transaction |
| 40 | + @Transactional |
| 41 | + public void transferGates(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) { |
| 42 | + // May wish to include this check to confirm this is actually in a transaction. |
| 43 | + TransactionalSupport.checkForTransactionInThreadLocalStorage().map((h) -> { |
| 44 | + if (!h.isPresent()) |
| 45 | + throw new RuntimeException("not in transaction!"); |
| 46 | + return h; |
| 47 | + }); |
| 48 | + |
| 49 | + AirlineGates fromAirlineGates = template.findById(AirlineGates.class).one(fromId); |
| 50 | + AirlineGates toAirlineGates = template.findById(AirlineGates.class).one(toId); |
| 51 | + toAirlineGates.gates += gatesToTransfer; |
| 52 | + fromAirlineGates.gates -= gatesToTransfer; |
| 53 | + template.save(fromAirlineGates); |
| 54 | + if(exceptionToThrow != null){ |
| 55 | + throw exceptionToThrow; |
| 56 | + } |
| 57 | + template.save(toAirlineGates); |
| 58 | + } |
| 59 | + // The @Transactional annotation results in the method of the proxy for the service executing this in a transaction |
| 60 | + @Transactional |
| 61 | + public Mono<Void> transferGatesReactive(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) { |
| 62 | + return Mono.deferContextual(ctx -> { |
| 63 | + // May wish to include this check to confirm this is actually in a transaction. |
| 64 | + TransactionalSupport.checkForTransactionInThreadLocalStorage().map((h) -> { |
| 65 | + if (!h.isPresent()) |
| 66 | + throw new RuntimeException("not in transaction!"); |
| 67 | + return h; |
| 68 | + }); |
| 69 | + |
| 70 | + AirlineGates fromAirlineGates = template.findById(AirlineGates.class).one(fromId); |
| 71 | + AirlineGates toAirlineGates = template.findById(AirlineGates.class).one(toId); |
| 72 | + toAirlineGates.gates += gatesToTransfer; |
| 73 | + fromAirlineGates.gates -= gatesToTransfer; |
| 74 | + template.save(fromAirlineGates); |
| 75 | + if(exceptionToThrow != null){ |
| 76 | + throw exceptionToThrow; |
| 77 | + } |
| 78 | + return reactiveTemplate.save(toAirlineGates).then(); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + // This does not have the @Transactional annotation therefore is not executed in a transaction |
| 83 | + public AirlineGates save(AirlineGates airlineGates) { |
| 84 | + return template.save(airlineGates); |
| 85 | + } |
| 86 | + |
| 87 | + // This does not have the @Transactional annotation therefore is not executed in a transaction |
| 88 | + public AirlineGates findById(String id) { |
| 89 | + return template.findById(AirlineGates.class).one(id); |
| 90 | + } |
| 91 | +} |
0 commit comments