-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
TransactionSynchronizationRegistryWrapper.java
100 lines (87 loc) · 5.07 KB
/
TransactionSynchronizationRegistryWrapper.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
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.txn.service.internal.tsr;
import jakarta.transaction.Synchronization;
import jakarta.transaction.SystemException;
import jakarta.transaction.TransactionSynchronizationRegistry;
import org.jboss.as.txn.logging.TransactionLogger;
import org.wildfly.transaction.client.AbstractTransaction;
import org.wildfly.transaction.client.ContextTransactionManager;
import org.wildfly.transaction.client.ContextTransactionSynchronizationRegistry;
/**
* Most of this implementation delegates down to the underlying transactions implementation to provide the services of the
* TransactionSynchronizationRegistry. The one area it modifies is the registration of the interposed Synchronizations. The
* reason this implementation needs to differ is because the Jakarta Connectors Synchronization and Jakarta Persistence Synchronizations are both specified as
* Interposed however there are defined ordering requirements between them both.
*
* The current implementation orders Jakarta Connectors relative to all other Synchronizations. For beforeCompletion, it would be possible to
* restrict this to the one case where Jakarta Connectors are ordered before Jakarta Persistence, however it is possible that other interposed Synchronizations
* would require the services of Jakarta Connectors and as such if the Jakarta Connectors are allowed to execute delistResource during beforeCompletion as
* mandated in Jakarta Connectors spec the behaviour of those subsequent interactions would be broken. For afterCompletion the Jakarta Connectors
* synchronizations are called last as that allows Jakarta Connectors to detect connection leaks from frameworks that have not closed the Jakarta Connectors
* managed resources. This is described in (for example)
* http://docs.oracle.com/javaee/5/api/javax/transaction/TransactionSynchronizationRegistry
* .html#registerInterposedSynchronization(jakarta.transaction.Synchronization) where it says that during afterCompletion
* "Resources can be closed but no transactional work can be performed with them".
*
* One implication of this approach is that if the underlying transactions implementation has special handling for various types
* of Synchronization that can also implement other interfaces (i.e. if interposedSync instanceof OtherInterface) these
* behaviours cannot take effect as the underlying implementation will never directly see the actual Synchronizations.
*/
public class TransactionSynchronizationRegistryWrapper implements TransactionSynchronizationRegistry {
private final Object key = new Object();
public TransactionSynchronizationRegistryWrapper() {
}
@Override
public void registerInterposedSynchronization(Synchronization sync)
throws IllegalStateException {
try {
AbstractTransaction tx = ContextTransactionManager.getInstance().getTransaction();
if(tx == null) {
throw TransactionLogger.ROOT_LOGGER.noActiveTransactionToRegisterSynchronization(sync);
}
JCAOrderedLastSynchronizationList jcaOrderedLastSynchronization = (JCAOrderedLastSynchronizationList) tx.getResource(key);
if (jcaOrderedLastSynchronization == null) {
final ContextTransactionSynchronizationRegistry tsr = ContextTransactionSynchronizationRegistry.getInstance();
synchronized (key) {
jcaOrderedLastSynchronization = (JCAOrderedLastSynchronizationList) tx.getResource(key);
if (jcaOrderedLastSynchronization == null) {
jcaOrderedLastSynchronization = new JCAOrderedLastSynchronizationList();
tx.putResource(key, jcaOrderedLastSynchronization);
tsr.registerInterposedSynchronization(jcaOrderedLastSynchronization);
}
}
}
jcaOrderedLastSynchronization.registerInterposedSynchronization(sync);
} catch (SystemException e) {
throw new IllegalStateException(e);
}
}
@Override
public Object getTransactionKey() {
return ContextTransactionSynchronizationRegistry.getInstance().getTransactionKey();
}
@Override
public int getTransactionStatus() {
return ContextTransactionSynchronizationRegistry.getInstance().getTransactionStatus();
}
@Override
public boolean getRollbackOnly() throws IllegalStateException {
return ContextTransactionSynchronizationRegistry.getInstance().getRollbackOnly();
}
@Override
public void setRollbackOnly() throws IllegalStateException {
ContextTransactionSynchronizationRegistry.getInstance().setRollbackOnly();
}
@Override
public Object getResource(Object key) throws IllegalStateException {
return ContextTransactionSynchronizationRegistry.getInstance().getResource(key);
}
@Override
public void putResource(Object key, Object value)
throws IllegalStateException {
ContextTransactionSynchronizationRegistry.getInstance().putResource(key, value);
}
}