@@ -81,11 +81,12 @@ void before() {
81
81
82
82
@ Test
83
83
void testSimpleTransaction () {
84
- TestTransactionSynchronization sync = new TestTransactionSynchronization (
85
- TransactionSynchronization .STATUS_COMMITTED );
84
+ when (connectionMock .isAutoCommit ()).thenReturn (false );
86
85
AtomicInteger commits = new AtomicInteger ();
87
86
when (connectionMock .commitTransaction ()).thenReturn (
88
87
Mono .fromRunnable (commits ::incrementAndGet ));
88
+ TestTransactionSynchronization sync = new TestTransactionSynchronization (
89
+ TransactionSynchronization .STATUS_COMMITTED );
89
90
90
91
TransactionalOperator operator = TransactionalOperator .create (tm );
91
92
@@ -98,6 +99,7 @@ void testSimpleTransaction() {
98
99
.verifyComplete ();
99
100
100
101
assertThat (commits ).hasValue (1 );
102
+ verify (connectionMock ).isAutoCommit ();
101
103
verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
102
104
verify (connectionMock ).commitTransaction ();
103
105
verify (connectionMock ).close ();
@@ -131,8 +133,10 @@ void testBeginFails() {
131
133
}
132
134
133
135
@ Test
134
- void appliesTransactionDefinition () {
136
+ void appliesTransactionDefinitionAndAutoCommit () {
137
+ when (connectionMock .isAutoCommit ()).thenReturn (true , false );
135
138
when (connectionMock .commitTransaction ()).thenReturn (Mono .empty ());
139
+ when (connectionMock .setAutoCommit (true )).thenReturn (Mono .empty ());
136
140
137
141
DefaultTransactionDefinition definition = new DefaultTransactionDefinition ();
138
142
definition .setName ("my-transaction" );
@@ -152,6 +156,7 @@ void appliesTransactionDefinition() {
152
156
verify (connectionMock ).beginTransaction (txCaptor .capture ());
153
157
verify (connectionMock , never ()).setTransactionIsolationLevel (any ());
154
158
verify (connectionMock ).commitTransaction ();
159
+ verify (connectionMock ).setAutoCommit (true );
155
160
verify (connectionMock ).close ();
156
161
157
162
io .r2dbc .spi .TransactionDefinition def = txCaptor .getValue ();
@@ -162,29 +167,8 @@ void appliesTransactionDefinition() {
162
167
}
163
168
164
169
@ Test
165
- void doesNotSetIsolationLevelIfMatch () {
166
- when (connectionMock .getTransactionIsolationLevel ()).thenReturn (
167
- IsolationLevel .READ_COMMITTED );
168
- when (connectionMock .commitTransaction ()).thenReturn (Mono .empty ());
169
-
170
- DefaultTransactionDefinition definition = new DefaultTransactionDefinition ();
171
- definition .setIsolationLevel (TransactionDefinition .ISOLATION_READ_COMMITTED );
172
-
173
- TransactionalOperator operator = TransactionalOperator .create (tm , definition );
174
-
175
- ConnectionFactoryUtils .getConnection (connectionFactoryMock )
176
- .as (operator ::transactional )
177
- .as (StepVerifier ::create )
178
- .expectNextCount (1 )
179
- .verifyComplete ();
180
-
181
- verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
182
- verify (connectionMock , never ()).setTransactionIsolationLevel (any ());
183
- verify (connectionMock ).commitTransaction ();
184
- }
185
-
186
- @ Test
187
- void doesNotSetAutoCommitDisabled () {
170
+ void doesNotSetAutoCommitIfRestoredByDriver () {
171
+ when (connectionMock .isAutoCommit ()).thenReturn (true , true );
188
172
when (connectionMock .commitTransaction ()).thenReturn (Mono .empty ());
189
173
190
174
DefaultTransactionDefinition definition = new DefaultTransactionDefinition ();
@@ -204,6 +188,7 @@ void doesNotSetAutoCommitDisabled() {
204
188
205
189
@ Test
206
190
void appliesReadOnly () {
191
+ when (connectionMock .isAutoCommit ()).thenReturn (false );
207
192
when (connectionMock .commitTransaction ()).thenReturn (Mono .empty ());
208
193
when (connectionMock .setTransactionIsolationLevel (any ())).thenReturn (Mono .empty ());
209
194
Statement statement = mock ();
@@ -222,6 +207,7 @@ void appliesReadOnly() {
222
207
.expectNextCount (1 )
223
208
.verifyComplete ();
224
209
210
+ verify (connectionMock ).isAutoCommit ();
225
211
verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
226
212
verify (connectionMock ).createStatement ("SET TRANSACTION READ ONLY" );
227
213
verify (connectionMock ).commitTransaction ();
@@ -231,7 +217,9 @@ void appliesReadOnly() {
231
217
232
218
@ Test
233
219
void testCommitFails () {
234
- when (connectionMock .commitTransaction ()).thenReturn (Mono .defer (() -> Mono .error (new R2dbcBadGrammarException ("Commit should fail" ))));
220
+ when (connectionMock .isAutoCommit ()).thenReturn (false );
221
+ when (connectionMock .commitTransaction ()).thenReturn (Mono .defer (() ->
222
+ Mono .error (new R2dbcBadGrammarException ("Commit should fail" ))));
235
223
when (connectionMock .rollbackTransaction ()).thenReturn (Mono .empty ());
236
224
237
225
TransactionalOperator operator = TransactionalOperator .create (tm );
@@ -242,6 +230,7 @@ void testCommitFails() {
242
230
.as (StepVerifier ::create )
243
231
.verifyError (BadSqlGrammarException .class );
244
232
233
+ verify (connectionMock ).isAutoCommit ();
245
234
verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
246
235
verify (connectionMock ).createStatement ("foo" );
247
236
verify (connectionMock ).commitTransaction ();
@@ -252,6 +241,7 @@ void testCommitFails() {
252
241
253
242
@ Test
254
243
void testRollback () {
244
+ when (connectionMock .isAutoCommit ()).thenReturn (false );
255
245
AtomicInteger commits = new AtomicInteger ();
256
246
when (connectionMock .commitTransaction ()).thenReturn (
257
247
Mono .fromRunnable (commits ::incrementAndGet ));
@@ -269,6 +259,7 @@ void testRollback() {
269
259
270
260
assertThat (commits ).hasValue (0 );
271
261
assertThat (rollbacks ).hasValue (1 );
262
+ verify (connectionMock ).isAutoCommit ();
272
263
verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
273
264
verify (connectionMock ).rollbackTransaction ();
274
265
verify (connectionMock ).close ();
@@ -278,7 +269,8 @@ void testRollback() {
278
269
@ Test
279
270
@ SuppressWarnings ("unchecked" )
280
271
void testRollbackFails () {
281
- when (connectionMock .rollbackTransaction ()).thenReturn (Mono .defer (() -> Mono .error (new R2dbcBadGrammarException ("Commit should fail" ))), Mono .empty ());
272
+ when (connectionMock .rollbackTransaction ()).thenReturn (Mono .defer (() ->
273
+ Mono .error (new R2dbcBadGrammarException ("Commit should fail" ))), Mono .empty ());
282
274
283
275
TransactionalOperator operator = TransactionalOperator .create (tm );
284
276
operator .execute (reactiveTransaction -> {
@@ -287,6 +279,7 @@ void testRollbackFails() {
287
279
.doOnNext (connection -> connection .createStatement ("foo" )).then ();
288
280
}).as (StepVerifier ::create ).verifyError (BadSqlGrammarException .class );
289
281
282
+ verify (connectionMock ).isAutoCommit ();
290
283
verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
291
284
verify (connectionMock ).createStatement ("foo" );
292
285
verify (connectionMock , never ()).commitTransaction ();
@@ -298,7 +291,8 @@ void testRollbackFails() {
298
291
@ Test
299
292
@ SuppressWarnings ("unchecked" )
300
293
void testConnectionReleasedWhenRollbackFails () {
301
- when (connectionMock .rollbackTransaction ()).thenReturn (Mono .defer (() -> Mono .error (new R2dbcBadGrammarException ("Rollback should fail" ))), Mono .empty ());
294
+ when (connectionMock .rollbackTransaction ()).thenReturn (Mono .defer (() ->
295
+ Mono .error (new R2dbcBadGrammarException ("Rollback should fail" ))), Mono .empty ());
302
296
when (connectionMock .setTransactionIsolationLevel (any ())).thenReturn (Mono .empty ());
303
297
304
298
TransactionalOperator operator = TransactionalOperator .create (tm );
@@ -319,6 +313,7 @@ void testConnectionReleasedWhenRollbackFails() {
319
313
320
314
@ Test
321
315
void testTransactionSetRollbackOnly () {
316
+ when (connectionMock .isAutoCommit ()).thenReturn (false );
322
317
when (connectionMock .rollbackTransaction ()).thenReturn (Mono .empty ());
323
318
TestTransactionSynchronization sync = new TestTransactionSynchronization (
324
319
TransactionSynchronization .STATUS_ROLLED_BACK );
@@ -334,6 +329,7 @@ void testTransactionSetRollbackOnly() {
334
329
}).then ();
335
330
}).as (StepVerifier ::create ).verifyComplete ();
336
331
332
+ verify (connectionMock ).isAutoCommit ();
337
333
verify (connectionMock ).beginTransaction (any (io .r2dbc .spi .TransactionDefinition .class ));
338
334
verify (connectionMock ).rollbackTransaction ();
339
335
verify (connectionMock ).close ();
0 commit comments