-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJakartaEntityTransaction.java
303 lines (277 loc) · 9.6 KB
/
JakartaEntityTransaction.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**********************************************************************
Copyright (c) 2006 Erik Bengtson and others. All rights reserved.
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.
Contributors:
2006 Andy Jefferson - replaced PersistenceManager with ExecutionContext
2007 Andy Jefferson - added setOption methods
...
**********************************************************************/
package org.datanucleus.api.jakarta;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.RollbackException;
import org.datanucleus.ExecutionContext;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.transaction.NucleusTransactionException;
import org.datanucleus.transaction.TransactionEventListener;
import org.datanucleus.transaction.TransactionUtils;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.Localiser;
/**
* EntityTransaction implementation for Jakarta Persistence for ResourceLocal transaction.
* Utilises the underlying ExecutionContext and its real transaction, providing a Jakarta Persistence layer on top.
*/
public class JakartaEntityTransaction implements EntityTransaction
{
/** The underlying transaction */
org.datanucleus.transaction.Transaction tx;
/**
* Constructor.
* @param ec The ExecutionContext providing the transaction.
*/
public JakartaEntityTransaction(ExecutionContext ec)
{
this.tx = ec.getTransaction();
}
/**
* Indicate whether a transaction is in progress.
* @throws PersistenceException if an unexpected error condition is encountered.
*/
public boolean isActive()
{
return tx.isActive();
}
/**
* Start a resource transaction.
* @throws IllegalStateException if the transaction is active
*/
public void begin()
{
assertNotActive();
try
{
tx.begin();
}
catch (NucleusException ne)
{
throw JakartaAdapter.getJakartaExceptionForNucleusException(ne);
}
}
/**
* Commit the current transaction, writing any unflushed changes to the database.
* @throws IllegalStateException if isActive() is false.
* @throws RollbackException if the commit fails.
*/
public void commit()
{
assertActive();
if (tx.getRollbackOnly())
{
// This is thrown by the underlying transaction but we want to have a RollbackException here so intercept it
if (NucleusLogger.TRANSACTION.isDebugEnabled())
{
NucleusLogger.TRANSACTION.debug(Localiser.msg("015020"));
}
throw new RollbackException(Localiser.msg("015020"));
}
try
{
tx.commit();
}
catch (NucleusTransactionException nte)
{
Throwable cause = nte.getCause();
Throwable pe = null;
if (cause instanceof NucleusException)
{
pe = JakartaAdapter.getJakartaExceptionForNucleusException((NucleusException)cause);
}
else
{
pe = cause;
}
throw new RollbackException(Localiser.msg("015007"), pe);
}
catch (NucleusException ne)
{
throw JakartaAdapter.getJakartaExceptionForNucleusException(ne);
}
}
/**
* Roll back the current transaction.
* @throws IllegalStateException if isActive() is false.
* @throws PersistenceException if an unexpected error condition is encountered.
*/
public void rollback()
{
assertActive();
try
{
tx.rollback();
}
catch (NucleusException ne)
{
throw JakartaAdapter.getJakartaExceptionForNucleusException(ne);
}
}
/**
* Determine whether the current transaction has been marked for rollback.
* @throws IllegalStateException if isActive() is false.
*/
public boolean getRollbackOnly()
{
assertActive();
return tx.getRollbackOnly();
}
/**
* Mark the current transaction so that the only possible outcome of the transaction is for the transaction to be rolled back.
* @throws IllegalStateException Thrown if the transaction is not active
*/
public void setRollbackOnly()
{
assertActive();
tx.setRollbackOnly();
}
/**
* Convenience accessor for setting a transaction option.
* @param option option name
* @param value The value
*/
public void setOption(String option, int value)
{
tx.setOption(option, value);
}
/**
* Convenience accessor for setting a transaction option.
* @param option option name
* @param value The value
*/
public void setOption(String option, boolean value)
{
tx.setOption(option, value);
}
/**
* Convenience accessor for setting a transaction option.
* @param option option name
* @param value The value
*/
public void setOption(String option, String value)
{
if (option.equals(org.datanucleus.transaction.Transaction.TRANSACTION_ISOLATION_OPTION))
{
int isolationLevel = TransactionUtils.getTransactionIsolationLevelForName(value);
tx.setOption(org.datanucleus.transaction.Transaction.TRANSACTION_ISOLATION_OPTION, isolationLevel);
return;
}
tx.setOption(option, value);
}
/**
* Method to mark the current point as a savepoint with the provided name.
* @param name Name of the savepoint.
* @throws UnsupportedOperationException if the underlying datastore doesn't support savepoints
* @throws IllegalStateException if no name is provided
*/
public void setSavepoint(String name)
{
if (name == null)
{
throw new IllegalStateException("No savepoint name provided so cannot set savepoint");
}
if (tx.isActive())
{
tx.setSavepoint(name);
}
else
{
throw new IllegalStateException("No active transaction so cannot set savepoint");
}
}
/**
* Method to mark the current point as a savepoint with the provided name.
* @param name Name of the savepoint.
* @throws UnsupportedOperationException if the underlying datastore doesn't support savepoints
* @throws IllegalStateException if no name is provided, or the name doesn't correspond to a known savepoint
*/
public void releaseSavepoint(String name)
{
if (name == null)
{
throw new IllegalStateException("No savepoint name provided so cannot release savepoint");
}
if (tx.isActive())
{
tx.releaseSavepoint(name);
}
else
{
throw new IllegalStateException("No active transaction so cannot release a savepoint");
}
}
/**
* Method to mark the current point as a savepoint with the provided name.
* @param name Name of the savepoint.
* @throws UnsupportedOperationException if the underlying datastore doesn't support savepoints
* @throws IllegalStateException if no name is provided, or the name doesn't correspond to a known savepoint
*/
public void rollbackToSavepoint(String name)
{
if (name == null)
{
throw new IllegalStateException("No savepoint name provided so cannot rollback to savepoint");
}
if (tx.isActive())
{
tx.rollbackToSavepoint(name);
}
else
{
throw new IllegalStateException("No active transaction so cannot rollback to savepoint");
}
}
/**
* Convenience method to throw an exception if the transaction is not active.
* @throws IllegalStateException Thrown if the transaction is not active.
*/
protected void assertActive()
{
if (!tx.isActive())
{
throw new IllegalStateException(Localiser.msg("015040"));
}
}
/**
* Convenience method to throw an exception if the transaction is active.
* @throws IllegalStateException Thrown if the transaction is active.
*/
protected void assertNotActive()
{
if (tx.isActive())
{
throw new IllegalStateException(Localiser.msg("015032"));
}
}
/**
* Method to register a listener for transaction events.
* @param listener The listener
*/
public void registerEventListener(TransactionEventListener listener)
{
tx.bindTransactionEventListener(listener);
}
/**
* Method to deregister a listener for transaction events.
* @param listener The listener to remove
*/
public void deregisterEventListener(TransactionEventListener listener)
{
tx.removeTransactionEventListener(listener);
}
}