1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24 import javax.persistence.EntityManager;
25
26 import static org.apache.onami.persist.Preconditions.checkNotNull;
27
28
29
30
31 @Singleton
32 class JtaTransactionFacadeFactory
33 implements TransactionFacadeFactory
34 {
35
36
37
38
39 private final UserTransactionFacade utFacade;
40
41
42
43
44
45 private final EntityManagerProvider emProvider;
46
47
48
49
50
51
52
53 @Inject
54 public JtaTransactionFacadeFactory( UserTransactionFacade utFacade, EntityManagerProvider emProvider )
55 {
56 this.utFacade = checkNotNull( utFacade, "utFacade is mandatory!" );
57 this.emProvider = checkNotNull( emProvider, "emProvider is mandatory!" );
58 }
59
60
61
62
63
64 public TransactionFacade createTransactionFacade()
65 {
66 if ( utFacade.isActive() )
67 {
68 return new Inner( utFacade, emProvider.get() );
69 }
70 else
71 {
72 return new Outer( utFacade, emProvider.get() );
73 }
74 }
75
76
77
78
79
80
81 private static class Inner
82 implements TransactionFacade
83 {
84 private final UserTransactionFacade txn;
85
86 private final EntityManager em;
87
88 Inner( UserTransactionFacade txn, EntityManager em )
89 {
90 this.txn = checkNotNull( txn, "txn is mandatory!" );
91 this.em = checkNotNull( em, "em is mandatory!" );
92 }
93
94
95
96
97
98 public void begin()
99 {
100 em.joinTransaction();
101 }
102
103
104
105
106
107 public void commit()
108 {
109
110 }
111
112
113
114
115
116 public void rollback()
117 {
118 txn.setRollbackOnly();
119 }
120 }
121
122
123
124
125
126
127 private static class Outer
128 implements TransactionFacade
129 {
130 private final UserTransactionFacade txn;
131
132 private final EntityManager em;
133
134 Outer( UserTransactionFacade txn, EntityManager em )
135 {
136 this.txn = checkNotNull( txn, "txn is mandatory!" );
137 this.em = checkNotNull( em, "em is mandatory!" );
138 }
139
140
141
142
143
144 public void begin()
145 {
146 txn.begin();
147 em.joinTransaction();
148 }
149
150
151
152
153
154 public void commit()
155 {
156 if ( txn.getRollbackOnly() )
157 {
158 txn.rollback();
159 }
160 else
161 {
162 txn.commit();
163 }
164 }
165
166
167
168
169
170 public void rollback()
171 {
172 txn.rollback();
173 }
174 }
175
176 }