-
Notifications
You must be signed in to change notification settings - Fork 29
/
MnTransactionContext.java
135 lines (121 loc) · 5.22 KB
/
MnTransactionContext.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
128
129
130
131
132
133
134
135
/*
* Copyright 2020-2021 original authors
*
* 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
*
* https://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 info.novatec.micronaut.camunda.bpm.feature.tx;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.transaction.SynchronousTransactionManager;
import io.micronaut.transaction.support.TransactionSynchronization;
import io.micronaut.transaction.support.TransactionSynchronizationAdapter;
import io.micronaut.transaction.support.TransactionSynchronizationManager;
import org.camunda.bpm.engine.impl.cfg.TransactionContext;
import org.camunda.bpm.engine.impl.cfg.TransactionListener;
import org.camunda.bpm.engine.impl.cfg.TransactionState;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import java.sql.Connection;
import static org.camunda.bpm.engine.impl.cfg.TransactionState.*;
/**
* Micronaut specific implementation of {@link TransactionContext}
*
* @author Tobias Schäfer
*/
// Implementation based on https://github.com/camunda/camunda-bpm-platform/blob/master/engine-spring/core/src/main/java/org/camunda/bpm/engine/spring/SpringTransactionContext.java
public class MnTransactionContext implements TransactionContext {
protected final SynchronousTransactionManager<Connection> transactionManager;
protected final CommandContext commandContext;
protected TransactionState lastTransactionState = null;
public MnTransactionContext(CommandContext commandContext, SynchronousTransactionManager<Connection> transactionManager) {
this.commandContext = commandContext;
this.transactionManager = transactionManager;
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void beforeCommit(boolean readOnly) {
lastTransactionState = COMMITTING;
}
@Override
public void afterCommit() {
lastTransactionState = COMMITTED;
}
@Override
public void beforeCompletion() {
lastTransactionState = ROLLINGBACK;
}
@Override
public void afterCompletion(@NonNull Status status) {
if (Status.ROLLED_BACK == status) {
lastTransactionState = ROLLED_BACK;
}
}
});
}
@Override
public void commit() {
// managed transaction, ignore
}
@Override
public void rollback() {
// Just in case the rollback isn't triggered by an
// exception, we mark the current transaction rollBackOnly.
transactionManager.getTransaction(null).setRollbackOnly();
}
@Override
public void addTransactionListener(TransactionState transactionState, TransactionListener transactionListener) {
TransactionSynchronization transactionSynchronization;
switch (transactionState) {
case COMMITTING:
transactionSynchronization = new TransactionSynchronizationAdapter() {
@Override
public void beforeCommit(boolean readOnly) {
transactionListener.execute(commandContext);
}
};
break;
case COMMITTED:
transactionSynchronization = new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
transactionListener.execute(commandContext);
}
};
break;
case ROLLINGBACK:
transactionSynchronization = new TransactionSynchronizationAdapter() {
@Override
public void beforeCompletion() {
transactionListener.execute(commandContext);
}
};
break;
case ROLLED_BACK:
transactionSynchronization = new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(@NonNull Status status) {
if (Status.ROLLED_BACK == status) {
transactionListener.execute(commandContext);
}
}
};
break;
default:
throw new IllegalStateException("Unknown transaction state: " + transactionState);
}
TransactionSynchronizationManager.registerSynchronization(transactionSynchronization);
}
@Override
public boolean isTransactionActive() {
return TransactionSynchronizationManager.isActualTransactionActive()
&& ROLLED_BACK != lastTransactionState
&& ROLLINGBACK != lastTransactionState;
}
}