Skip to content

Commit d86e3b9

Browse files
authored
Update spring-data-testapp for transactions. (#1577)
Closed #1576.
1 parent aa09729 commit d86e3b9

File tree

8 files changed

+258
-298
lines changed

8 files changed

+258
-298
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2012-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.annotation.Id;
19+
import org.springframework.data.annotation.PersistenceConstructor;
20+
import org.springframework.data.annotation.Version;
21+
import org.springframework.data.couchbase.core.index.QueryIndexed;
22+
import org.springframework.data.couchbase.core.mapping.Document;
23+
24+
@Document
25+
/**
26+
* @author Michael Reiche
27+
*/
28+
public class AirlineGates {
29+
@Id String id;
30+
@Version Long version;
31+
32+
@QueryIndexed String name;
33+
String iata;
34+
Long gates;
35+
36+
@PersistenceConstructor
37+
public AirlineGates(String id, String name, String iata, Long gates) {
38+
this.id = id;
39+
this.name = name;
40+
this.iata = iata;
41+
this.gates = gates;
42+
}
43+
44+
public String getId() {
45+
return id;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public String getIata() {
53+
return iata;
54+
}
55+
56+
public Long getGates() {
57+
return gates;
58+
}
59+
60+
public String toString(){
61+
StringBuffer sb=new StringBuffer();
62+
sb.append("{");
63+
sb.append("\"id\":"+id);
64+
sb.append(", \"name\":"+name);
65+
sb.append(", \"iata\":"+iata);
66+
sb.append(", \"gates\":"+gates);
67+
sb.append("}");
68+
69+
return sb.toString();
70+
}
71+
72+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2017-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+
17+
package com.example.demo;
18+
19+
import org.springframework.data.couchbase.repository.CouchbaseRepository;
20+
import org.springframework.data.couchbase.repository.DynamicProxyable;
21+
import org.springframework.stereotype.Repository;
22+
23+
/**
24+
* @author Michael Reiche
25+
*/
26+
@Repository
27+
public interface AirlineGatesRepository
28+
extends CouchbaseRepository<AirlineGates, String>, DynamicProxyable<AirlineGatesRepository> {
29+
30+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

spring-data-testapp/src/main/java/com/example/demo/Airport.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

spring-data-testapp/src/main/java/com/example/demo/AirportRepository.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)