/
TransactionalService.java
127 lines (110 loc) · 4.09 KB
/
TransactionalService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.microprofile.concurrency.tck.cdi;
import org.eclipse.microprofile.concurrent.ManagedExecutor;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
@ApplicationScoped
class TransactionalService {
@Inject
private TransactionalBean bean;
int getValue() {
return bean.getValue();
}
@Transactional(Transactional.TxType.REQUIRES_NEW)
void requiresNew() {
bean.incrementValue();
}
@Transactional(Transactional.TxType.REQUIRED)
void required() {
bean.incrementValue();
}
@Transactional(Transactional.TxType.MANDATORY)
void mandatory() {
System.out.printf("%s: Transactional.TxType.MANDATORY%n", Thread.currentThread());
bean.incrementValue();
}
@Transactional(Transactional.TxType.NEVER)
void never() {
bean.incrementValue(); // should throe ContextNotActiveException
}
@Transactional(Transactional.TxType.NOT_SUPPORTED)
void notSupported() {
bean.incrementValue(); // should throe ContextNotActiveException
}
@Transactional(Transactional.TxType.SUPPORTS)
void supports() {
bean.incrementValue();
}
@Transactional(Transactional.TxType.REQUIRED)
int testAsync(ManagedExecutor executor) {
int currentValue = bean.getValue();
CompletableFuture<Void> stage;
try {
stage = executor.runAsync(this::required);
}
catch (IllegalStateException x) {
System.out.println("Propagation of active transactions is not supported. Skipping test.");
return JTACDITest.UNSUPPORTED;
}
try {
stage.join();
}
catch (CompletionException x) {
if (x.getCause() instanceof IllegalStateException) {
System.out.println("Propagation of active transactions to multiple threads in parallel is not supported. Skipping test.");
return JTACDITest.UNSUPPORTED;
}
else {
throw x;
}
}
return bean.getValue() - currentValue;
}
@Transactional(Transactional.TxType.REQUIRED) // run the method in transaction scope
int testConcurrentTransactionPropagation(ManagedExecutor executor) {
int currentValue = bean.getValue();
// run two concurrent transactional bean updates
CompletableFuture<Void> stage0;
try {
stage0 = executor.runAsync(this::required);
}
catch (IllegalStateException x) {
System.out.println("Propagation of active transactions is not supported. Skipping test.");
return JTACDITest.UNSUPPORTED;
}
CompletableFuture<Void> stage1 = executor.runAsync(this::required);
try {
CompletableFuture.allOf(stage0, stage1).join();
}
catch (CompletionException x) {
if (x.getCause() instanceof IllegalStateException) {
System.out.println("Propagation of active transactions to multiple threads in parallel is not supported. Skipping test.");
return JTACDITest.UNSUPPORTED;
}
else {
throw x;
}
}
return bean.getValue() - currentValue;
}
}