1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 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.
@@ -72,7 +72,7 @@ public class DataSourceTransactionManagerTests {
72
72
73
73
protected DataSource ds = mock ();
74
74
75
- protected Connection con = mock ();
75
+ protected ConnectionProxy con = mock ();
76
76
77
77
protected DataSourceTransactionManager tm ;
78
78
@@ -81,6 +81,7 @@ public class DataSourceTransactionManagerTests {
81
81
void setup () throws Exception {
82
82
tm = createTransactionManager (ds );
83
83
given (ds .getConnection ()).willReturn (con );
84
+ given (con .getTargetConnection ()).willThrow (new UnsupportedOperationException ());
84
85
}
85
86
86
87
protected DataSourceTransactionManager createTransactionManager (DataSource ds ) {
@@ -1073,9 +1074,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
1073
1074
Connection tCon = dsProxy .getConnection ();
1074
1075
tCon .getWarnings ();
1075
1076
tCon .clearWarnings ();
1076
- assertThat (((ConnectionProxy ) dsProxy . getConnection () ).getTargetConnection ()).isEqualTo (con );
1077
+ assertThat (((ConnectionProxy ) tCon ).getTargetConnection ()).isEqualTo (con );
1077
1078
// should be ignored
1078
- dsProxy . getConnection () .close ();
1079
+ tCon .close ();
1079
1080
}
1080
1081
catch (SQLException ex ) {
1081
1082
throw new UncategorizedSQLException ("" , "" , ex );
@@ -1109,9 +1110,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
1109
1110
Connection tCon = dsProxy .getConnection ();
1110
1111
assertThatExceptionOfType (SQLException .class ).isThrownBy (tCon ::getWarnings );
1111
1112
tCon .clearWarnings ();
1112
- assertThat (((ConnectionProxy ) dsProxy . getConnection () ).getTargetConnection ()).isEqualTo (con );
1113
+ assertThat (((ConnectionProxy ) tCon ).getTargetConnection ()).isEqualTo (con );
1113
1114
// should be ignored
1114
- dsProxy . getConnection () .close ();
1115
+ tCon .close ();
1115
1116
}
1116
1117
catch (SQLException ex ) {
1117
1118
throw new UncategorizedSQLException ("" , "" , ex );
@@ -1127,6 +1128,42 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
1127
1128
verify (con ).close ();
1128
1129
}
1129
1130
1131
+ @ Test
1132
+ void testTransactionAwareDataSourceProxyWithEarlyConnection () throws Exception {
1133
+ given (ds .getConnection ()).willReturn (mock (Connection .class ), con );
1134
+ given (con .getAutoCommit ()).willReturn (true );
1135
+ given (con .getWarnings ()).willThrow (new SQLException ());
1136
+
1137
+ TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy (ds );
1138
+ dsProxy .setLazyTransactionalConnections (false );
1139
+ Connection tCon = dsProxy .getConnection ();
1140
+
1141
+ TransactionTemplate tt = new TransactionTemplate (tm );
1142
+ assertThat (TransactionSynchronizationManager .hasResource (ds )).isFalse ();
1143
+ tt .execute (new TransactionCallbackWithoutResult () {
1144
+ @ Override
1145
+ protected void doInTransactionWithoutResult (TransactionStatus status ) {
1146
+ // something transactional
1147
+ assertThat (DataSourceUtils .getConnection (ds )).isEqualTo (con );
1148
+ try {
1149
+ // should close the early Connection obtained before the transaction
1150
+ tCon .close ();
1151
+ }
1152
+ catch (SQLException ex ) {
1153
+ throw new UncategorizedSQLException ("" , "" , ex );
1154
+ }
1155
+ }
1156
+ });
1157
+
1158
+ assertThat (TransactionSynchronizationManager .hasResource (ds )).isFalse ();
1159
+
1160
+ InOrder ordered = inOrder (con );
1161
+ ordered .verify (con ).setAutoCommit (false );
1162
+ ordered .verify (con ).commit ();
1163
+ ordered .verify (con ).setAutoCommit (true );
1164
+ verify (con ).close ();
1165
+ }
1166
+
1130
1167
@ Test
1131
1168
void testTransactionAwareDataSourceProxyWithSuspension () throws Exception {
1132
1169
given (con .getAutoCommit ()).willReturn (true );
0 commit comments