Skip to content

Commit df80614

Browse files
committed
TransactionTemplate equality for same transaction manager only
Issue: SPR-16572
1 parent cf74b1b commit df80614

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

spring-tx/src/main/java/org/springframework/transaction/support/DefaultTransactionDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public final String getName() {
259259
*/
260260
@Override
261261
public boolean equals(Object other) {
262-
return (other instanceof TransactionDefinition && toString().equals(other.toString()));
262+
return (this == other || (other instanceof TransactionDefinition && toString().equals(other.toString())));
263263
}
264264

265265
/**

spring-tx/src/main/java/org/springframework/transaction/support/TransactionTemplate.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -178,4 +178,11 @@ private void rollbackOnException(TransactionStatus status, Throwable ex) throws
178178
}
179179
}
180180

181+
182+
@Override
183+
public boolean equals(Object other) {
184+
return (this == other || (super.equals(other) && (!(other instanceof TransactionTemplate) ||
185+
getTransactionManager() == ((TransactionTemplate) other).getTransactionManager())));
186+
}
187+
181188
}

spring-tx/src/test/java/org/springframework/transaction/TransactionSupportTests.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,6 +83,7 @@ public void commitWithoutExistingTransaction() {
8383
TestTransactionManager tm = new TestTransactionManager(false, true);
8484
TransactionStatus status = tm.getTransaction(null);
8585
tm.commit(status);
86+
8687
assertTrue("triggered begin", tm.begin);
8788
assertTrue("triggered commit", tm.commit);
8889
assertTrue("no rollback", !tm.rollback);
@@ -94,6 +95,7 @@ public void rollbackWithoutExistingTransaction() {
9495
TestTransactionManager tm = new TestTransactionManager(false, true);
9596
TransactionStatus status = tm.getTransaction(null);
9697
tm.rollback(status);
98+
9799
assertTrue("triggered begin", tm.begin);
98100
assertTrue("no commit", !tm.commit);
99101
assertTrue("triggered rollback", tm.rollback);
@@ -106,6 +108,7 @@ public void rollbackOnlyWithoutExistingTransaction() {
106108
TransactionStatus status = tm.getTransaction(null);
107109
status.setRollbackOnly();
108110
tm.commit(status);
111+
109112
assertTrue("triggered begin", tm.begin);
110113
assertTrue("no commit", !tm.commit);
111114
assertTrue("triggered rollback", tm.rollback);
@@ -117,6 +120,7 @@ public void commitWithExistingTransaction() {
117120
TestTransactionManager tm = new TestTransactionManager(true, true);
118121
TransactionStatus status = tm.getTransaction(null);
119122
tm.commit(status);
123+
120124
assertTrue("no begin", !tm.begin);
121125
assertTrue("no commit", !tm.commit);
122126
assertTrue("no rollback", !tm.rollback);
@@ -128,6 +132,7 @@ public void rollbackWithExistingTransaction() {
128132
TestTransactionManager tm = new TestTransactionManager(true, true);
129133
TransactionStatus status = tm.getTransaction(null);
130134
tm.rollback(status);
135+
131136
assertTrue("no begin", !tm.begin);
132137
assertTrue("no commit", !tm.commit);
133138
assertTrue("no rollback", !tm.rollback);
@@ -140,6 +145,7 @@ public void rollbackOnlyWithExistingTransaction() {
140145
TransactionStatus status = tm.getTransaction(null);
141146
status.setRollbackOnly();
142147
tm.commit(status);
148+
143149
assertTrue("no begin", !tm.begin);
144150
assertTrue("no commit", !tm.commit);
145151
assertTrue("no rollback", !tm.rollback);
@@ -155,6 +161,7 @@ public void transactionTemplate() {
155161
protected void doInTransactionWithoutResult(TransactionStatus status) {
156162
}
157163
});
164+
158165
assertTrue("triggered begin", tm.begin);
159166
assertTrue("triggered commit", tm.commit);
160167
assertTrue("no rollback", !tm.rollback);
@@ -170,6 +177,7 @@ public void transactionTemplateWithCallbackPreference() {
170177
protected void doInTransactionWithoutResult(TransactionStatus status) {
171178
}
172179
});
180+
173181
assertSame(template, ptm.getDefinition());
174182
assertFalse(ptm.getStatus().isRollbackOnly());
175183
}
@@ -300,9 +308,22 @@ public void transactionTemplateInitialization() {
300308
assertTrue("Correct isolation level set", template.getIsolationLevel() == TransactionDefinition.ISOLATION_REPEATABLE_READ);
301309
}
302310

311+
@Test
312+
public void transactionTemplateEquality() {
313+
TestTransactionManager tm1 = new TestTransactionManager(false, true);
314+
TestTransactionManager tm2 = new TestTransactionManager(false, true);
315+
TransactionTemplate template1 = new TransactionTemplate(tm1);
316+
TransactionTemplate template2 = new TransactionTemplate(tm2);
317+
TransactionTemplate template3 = new TransactionTemplate(tm2);
318+
319+
assertNotEquals(template1, template2);
320+
assertNotEquals(template1, template3);
321+
assertEquals(template2, template3);
322+
}
323+
303324

304325
@After
305-
public void tearDown() {
326+
public void clear() {
306327
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
307328
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
308329
}

0 commit comments

Comments
 (0)