1
1
/*
2
- * Copyright 2010-2023 the original author or authors.
2
+ * Copyright 2010-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -47,7 +47,7 @@ class MyBatisSpringTest extends AbstractMyBatisSpringTest {
47
47
@ AfterEach
48
48
void validateSessionClose () {
49
49
// assume if the Executor is closed, the Session is too
50
- if (( session != null ) && !executorInterceptor .isExecutorClosed ()) {
50
+ if (session != null && !executorInterceptor .isExecutorClosed ()) {
51
51
session = null ;
52
52
fail ("SqlSession is not closed" );
53
53
} else {
@@ -140,8 +140,8 @@ void testSpringAPIWithMyBatisClose() {
140
140
// Spring API should work with a MyBatis TransactionFactories
141
141
@ Test
142
142
void testWithNonSpringTransactionFactory () {
143
- Environment original = sqlSessionFactory .getConfiguration ().getEnvironment ();
144
- Environment nonSpring = new Environment ("non-spring" , new JdbcTransactionFactory (), dataSource );
143
+ var original = sqlSessionFactory .getConfiguration ().getEnvironment ();
144
+ var nonSpring = new Environment ("non-spring" , new JdbcTransactionFactory (), dataSource );
145
145
sqlSessionFactory .getConfiguration ().setEnvironment (nonSpring );
146
146
147
147
try {
@@ -162,8 +162,8 @@ void testWithNonSpringTransactionFactory() {
162
162
// this should not work since the DS will be out of sync with MyBatis
163
163
@ Test
164
164
void testNonSpringTxFactoryWithTx () throws Exception {
165
- Environment original = sqlSessionFactory .getConfiguration ().getEnvironment ();
166
- Environment nonSpring = new Environment ("non-spring" , new JdbcTransactionFactory (), dataSource );
165
+ var original = sqlSessionFactory .getConfiguration ().getEnvironment ();
166
+ var nonSpring = new Environment ("non-spring" , new JdbcTransactionFactory (), dataSource );
167
167
sqlSessionFactory .getConfiguration ().setEnvironment (nonSpring );
168
168
169
169
TransactionStatus status = null ;
@@ -186,12 +186,12 @@ void testNonSpringTxFactoryWithTx() throws Exception {
186
186
// this should work since the DS is managed MyBatis
187
187
@ Test
188
188
void testNonSpringTxFactoryNonSpringDSWithTx () throws java .sql .SQLException {
189
- Environment original = sqlSessionFactory .getConfiguration ().getEnvironment ();
189
+ var original = sqlSessionFactory .getConfiguration ().getEnvironment ();
190
190
191
- MockDataSource mockDataSource = new MockDataSource ();
191
+ var mockDataSource = new MockDataSource ();
192
192
mockDataSource .setupConnection (createMockConnection ());
193
193
194
- Environment nonSpring = new Environment ("non-spring" , new JdbcTransactionFactory (), mockDataSource );
194
+ var nonSpring = new Environment ("non-spring" , new JdbcTransactionFactory (), mockDataSource );
195
195
sqlSessionFactory .getConfiguration ().setEnvironment (nonSpring );
196
196
197
197
TransactionStatus status ;
@@ -211,7 +211,7 @@ void testNonSpringTxFactoryNonSpringDSWithTx() throws java.sql.SQLException {
211
211
212
212
// SqlSession uses its own connection
213
213
// that connection will not have committed since no SQL was executed by the session
214
- MockConnection mockConnection = (MockConnection ) mockDataSource .getConnection ();
214
+ var mockConnection = (MockConnection ) mockDataSource .getConnection ();
215
215
assertThat (mockConnection .getNumberCommits ()).as ("should call commit on Connection" ).isEqualTo (0 );
216
216
assertThat (mockConnection .getNumberRollbacks ()).as ("should not call rollback on Connection" ).isEqualTo (0 );
217
217
assertCommitSession ();
@@ -248,16 +248,16 @@ void testChangeExecutorTypeInTxRequiresNew() throws Exception {
248
248
249
249
try {
250
250
txManager .setDataSource (dataSource );
251
- TransactionStatus status = txManager .getTransaction (new DefaultTransactionDefinition ());
251
+ var status = txManager .getTransaction (new DefaultTransactionDefinition ());
252
252
253
253
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
254
254
255
255
// start a new tx while the other is in progress
256
- DefaultTransactionDefinition txRequiresNew = new DefaultTransactionDefinition ();
256
+ var txRequiresNew = new DefaultTransactionDefinition ();
257
257
txRequiresNew .setPropagationBehaviorName ("PROPAGATION_REQUIRES_NEW" );
258
- TransactionStatus status2 = txManager .getTransaction (txRequiresNew );
258
+ var status2 = txManager .getTransaction (txRequiresNew );
259
259
260
- SqlSession session2 = SqlSessionUtils .getSqlSession (sqlSessionFactory , ExecutorType .BATCH , exceptionTranslator );
260
+ var session2 = SqlSessionUtils .getSqlSession (sqlSessionFactory , ExecutorType .BATCH , exceptionTranslator );
261
261
262
262
SqlSessionUtils .closeSqlSession (session2 , sqlSessionFactory );
263
263
txManager .rollback (status2 );
@@ -277,12 +277,12 @@ void testChangeExecutorTypeInTxRequiresNew() throws Exception {
277
277
278
278
@ Test
279
279
void testWithJtaTxManager () {
280
- JtaTransactionManager jtaManager = new JtaTransactionManager (Mockito .mock (UserTransaction .class ));
280
+ var jtaManager = new JtaTransactionManager (Mockito .mock (UserTransaction .class ));
281
281
282
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
282
+ var txDef = new DefaultTransactionDefinition ();
283
283
txDef .setPropagationBehaviorName ("PROPAGATION_REQUIRED" );
284
284
285
- TransactionStatus status = jtaManager .getTransaction (txDef );
285
+ var status = jtaManager .getTransaction (txDef );
286
286
287
287
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
288
288
session .getMapper (TestMapper .class ).findTest ();
@@ -298,20 +298,20 @@ void testWithJtaTxManager() {
298
298
299
299
@ Test
300
300
void testWithJtaTxManagerAndNonSpringTxManager () throws java .sql .SQLException {
301
- Environment original = sqlSessionFactory .getConfiguration ().getEnvironment ();
301
+ var original = sqlSessionFactory .getConfiguration ().getEnvironment ();
302
302
303
- MockDataSource mockDataSource = new MockDataSource ();
303
+ var mockDataSource = new MockDataSource ();
304
304
mockDataSource .setupConnection (createMockConnection ());
305
305
306
- Environment nonSpring = new Environment ("non-spring" , new ManagedTransactionFactory (), mockDataSource );
306
+ var nonSpring = new Environment ("non-spring" , new ManagedTransactionFactory (), mockDataSource );
307
307
sqlSessionFactory .getConfiguration ().setEnvironment (nonSpring );
308
308
309
- JtaTransactionManager jtaManager = new JtaTransactionManager (Mockito .mock (UserTransaction .class ));
309
+ var jtaManager = new JtaTransactionManager (Mockito .mock (UserTransaction .class ));
310
310
311
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
311
+ var txDef = new DefaultTransactionDefinition ();
312
312
txDef .setPropagationBehaviorName ("PROPAGATION_REQUIRED" );
313
313
314
- TransactionStatus status = jtaManager .getTransaction (txDef );
314
+ var status = jtaManager .getTransaction (txDef );
315
315
316
316
try {
317
317
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
@@ -326,7 +326,7 @@ void testWithJtaTxManagerAndNonSpringTxManager() throws java.sql.SQLException {
326
326
assertNoCommitJdbc ();
327
327
assertCommitSession ();
328
328
329
- MockConnection mockConnection = (MockConnection ) mockDataSource .getConnection ();
329
+ var mockConnection = (MockConnection ) mockDataSource .getConnection ();
330
330
assertThat (mockConnection .getNumberCommits ()).as ("should call commit on Connection" ).isEqualTo (0 );
331
331
assertThat (mockConnection .getNumberRollbacks ()).as ("should not call rollback on Connection" ).isEqualTo (0 );
332
332
@@ -345,10 +345,10 @@ void testWithJtaTxManagerAndNonSpringTxManager() throws java.sql.SQLException {
345
345
346
346
@ Test
347
347
void testWithTxSupports () {
348
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
348
+ var txDef = new DefaultTransactionDefinition ();
349
349
txDef .setPropagationBehaviorName ("PROPAGATION_SUPPORTS" );
350
350
351
- TransactionStatus status = txManager .getTransaction (txDef );
351
+ var status = txManager .getTransaction (txDef );
352
352
353
353
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
354
354
session .getMapper (TestMapper .class ).findTest ();
@@ -363,10 +363,10 @@ void testWithTxSupports() {
363
363
364
364
@ Test
365
365
void testRollbackWithTxSupports () {
366
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
366
+ var txDef = new DefaultTransactionDefinition ();
367
367
txDef .setPropagationBehaviorName ("PROPAGATION_SUPPORTS" );
368
368
369
- TransactionStatus status = txManager .getTransaction (txDef );
369
+ var status = txManager .getTransaction (txDef );
370
370
371
371
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
372
372
session .getMapper (TestMapper .class ).findTest ();
@@ -381,10 +381,10 @@ void testRollbackWithTxSupports() {
381
381
382
382
@ Test
383
383
void testWithTxRequired () {
384
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
384
+ var txDef = new DefaultTransactionDefinition ();
385
385
txDef .setPropagationBehaviorName ("PROPAGATION_REQUIRED" );
386
386
387
- TransactionStatus status = txManager .getTransaction (txDef );
387
+ var status = txManager .getTransaction (txDef );
388
388
389
389
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
390
390
session .getMapper (TestMapper .class ).findTest ();
@@ -399,10 +399,10 @@ void testWithTxRequired() {
399
399
400
400
@ Test
401
401
void testSqlSessionCommitWithTx () {
402
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
402
+ var txDef = new DefaultTransactionDefinition ();
403
403
txDef .setPropagationBehaviorName ("PROPAGATION_REQUIRED" );
404
404
405
- TransactionStatus status = txManager .getTransaction (txDef );
405
+ var status = txManager .getTransaction (txDef );
406
406
407
407
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
408
408
session .getMapper (TestMapper .class ).findTest ();
@@ -429,7 +429,7 @@ void testWithInterleavedTx() {
429
429
session .getMapper (TestMapper .class ).findTest ();
430
430
431
431
// this transaction should use another Connection
432
- TransactionStatus status = txManager .getTransaction (new DefaultTransactionDefinition ());
432
+ var status = txManager .getTransaction (new DefaultTransactionDefinition ());
433
433
434
434
// session continues using original connection
435
435
session .getMapper (TestMapper .class ).insertTest ("test2" );
@@ -463,16 +463,16 @@ void testSuspendAndResume() {
463
463
464
464
try {
465
465
txManager .setDataSource (dataSource );
466
- TransactionStatus status = txManager .getTransaction (new DefaultTransactionDefinition ());
466
+ var status = txManager .getTransaction (new DefaultTransactionDefinition ());
467
467
468
468
session = SqlSessionUtils .getSqlSession (sqlSessionFactory );
469
469
470
470
// start a new tx while the other is in progress
471
- DefaultTransactionDefinition txRequiresNew = new DefaultTransactionDefinition ();
471
+ var txRequiresNew = new DefaultTransactionDefinition ();
472
472
txRequiresNew .setPropagationBehaviorName ("PROPAGATION_REQUIRES_NEW" );
473
- TransactionStatus status2 = txManager .getTransaction (txRequiresNew );
473
+ var status2 = txManager .getTransaction (txRequiresNew );
474
474
475
- SqlSession session2 = SqlSessionUtils .getSqlSession (sqlSessionFactory );
475
+ var session2 = SqlSessionUtils .getSqlSession (sqlSessionFactory );
476
476
477
477
assertThat (session ).as ("getSqlSession() should not return suspended SqlSession" ).isNotSameAs (session2 );
478
478
@@ -535,10 +535,10 @@ void testBatch() {
535
535
void testBatchInTx () {
536
536
setupBatchStatements ();
537
537
538
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
538
+ var txDef = new DefaultTransactionDefinition ();
539
539
txDef .setPropagationBehaviorName ("PROPAGATION_REQUIRED" );
540
540
541
- TransactionStatus status = txManager .getTransaction (txDef );
541
+ var status = txManager .getTransaction (txDef );
542
542
543
543
session = SqlSessionUtils .getSqlSession (sqlSessionFactory , ExecutorType .BATCH , exceptionTranslator );
544
544
@@ -577,10 +577,10 @@ void testBatchWithError() {
577
577
void testBatchInTxWithError () {
578
578
setupBatchStatements ();
579
579
580
- DefaultTransactionDefinition txDef = new DefaultTransactionDefinition ();
580
+ var txDef = new DefaultTransactionDefinition ();
581
581
txDef .setPropagationBehaviorName ("PROPAGATION_REQUIRED" );
582
582
583
- TransactionStatus status = txManager .getTransaction (txDef );
583
+ var status = txManager .getTransaction (txDef );
584
584
585
585
session = SqlSessionUtils .getSqlSession (sqlSessionFactory , ExecutorType .BATCH , exceptionTranslator );
586
586
0 commit comments