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