21
21
import com .google .inject .Injector ;
22
22
import com .google .inject .persist .PersistService ;
23
23
import com .google .inject .persist .Transactional ;
24
+ import com .google .inject .persist .UnitOfWork ;
24
25
import java .io .FileNotFoundException ;
25
26
import java .io .IOException ;
26
27
import java .util .Date ;
27
28
import java .util .List ;
29
+ import javax .inject .Provider ;
28
30
import javax .persistence .EntityManager ;
29
31
import javax .persistence .EntityManagerFactory ;
30
32
import junit .framework .TestCase ;
@@ -62,6 +64,7 @@ public void tearDown() {
62
64
public void testSimpleTransaction () {
63
65
injector .getInstance (TransactionalObject .class ).runOperationInTxn ();
64
66
67
+ injector .getInstance (UnitOfWork .class ).begin ();
65
68
EntityManager session = injector .getInstance (EntityManager .class );
66
69
assertFalse (
67
70
"EntityManager was not closed by transactional service" ,
@@ -92,6 +95,7 @@ public void testSimpleTransactionRollbackOnChecked() {
92
95
// ignore
93
96
}
94
97
98
+ injector .getInstance (UnitOfWork .class ).begin ();
95
99
EntityManager session = injector .getInstance (EntityManager .class );
96
100
assertFalse (
97
101
"EntityManager was not closed by transactional service (rollback didnt happen?)" ,
@@ -119,6 +123,7 @@ public void testSimpleTransactionRollbackOnCheckedExcepting() {
119
123
// ignored
120
124
}
121
125
126
+ injector .getInstance (UnitOfWork .class ).begin ();
122
127
EntityManager session = injector .getInstance (EntityManager .class );
123
128
assertFalse (
124
129
"Txn was not closed by transactional service (commit didnt happen?)" ,
@@ -144,6 +149,7 @@ public void testSimpleTransactionRollbackOnUnchecked() {
144
149
// ignore
145
150
}
146
151
152
+ injector .getInstance (UnitOfWork .class ).begin ();
147
153
EntityManager session = injector .getInstance (EntityManager .class );
148
154
assertFalse (
149
155
"EntityManager was not closed by transactional service (rollback didnt happen?)" ,
@@ -184,9 +190,10 @@ public void testTransactionalDoesntAffectObjectMethods() {
184
190
185
191
@ Transactional
186
192
public static class TransactionalObject {
187
- @ Inject EntityManager session ;
193
+ @ Inject Provider < EntityManager > sessionProvider ;
188
194
189
195
public void runOperationInTxn () {
196
+ EntityManager session = sessionProvider .get ();
190
197
assertTrue (session .getTransaction ().isActive ());
191
198
JpaTestEntity entity = new JpaTestEntity ();
192
199
entity .setText (UNIQUE_TEXT );
@@ -196,10 +203,11 @@ public void runOperationInTxn() {
196
203
197
204
@ Transactional
198
205
public static class TransactionalObject4 {
199
- @ Inject EntityManager session ;
206
+ @ Inject Provider < EntityManager > sessionProvider ;
200
207
201
208
@ Transactional
202
209
public void runOperationInTxnThrowingUnchecked () {
210
+ EntityManager session = sessionProvider .get ();
203
211
assertTrue (session .getTransaction ().isActive ());
204
212
JpaTestEntity entity = new JpaTestEntity ();
205
213
entity .setText (TRANSIENT_UNIQUE_TEXT );
@@ -211,9 +219,10 @@ public void runOperationInTxnThrowingUnchecked() {
211
219
212
220
@ Transactional (rollbackOn = IOException .class , ignore = FileNotFoundException .class )
213
221
public static class TransactionalObject3 {
214
- @ Inject EntityManager session ;
222
+ @ Inject Provider < EntityManager > sessionProvider ;
215
223
216
224
public void runOperationInTxnThrowingCheckedExcepting () throws IOException {
225
+ EntityManager session = sessionProvider .get ();
217
226
assertTrue (session .getTransaction ().isActive ());
218
227
JpaTestEntity entity = new JpaTestEntity ();
219
228
entity .setText (UNIQUE_TEXT_2 );
@@ -225,9 +234,10 @@ public void runOperationInTxnThrowingCheckedExcepting() throws IOException {
225
234
226
235
@ Transactional (rollbackOn = IOException .class )
227
236
public static class TransactionalObject2 {
228
- @ Inject EntityManager session ;
237
+ @ Inject Provider < EntityManager > sessionProvider ;
229
238
230
239
public void runOperationInTxnThrowingChecked () throws IOException {
240
+ EntityManager session = sessionProvider .get ();
231
241
assertTrue (session .getTransaction ().isActive ());
232
242
JpaTestEntity entity = new JpaTestEntity ();
233
243
entity .setText (TRANSIENT_UNIQUE_TEXT );
0 commit comments