1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2022 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.
22
22
import org .springframework .util .Assert ;
23
23
24
24
/**
25
- * Rule determining whether or not a given exception (and any subclasses)
26
- * should cause a rollback.
25
+ * Rule determining whether or not a given exception should cause a rollback.
27
26
*
28
27
* <p>Multiple such rules can be applied to determine whether a transaction
29
28
* should commit or rollback after an exception has been thrown.
30
29
*
31
30
* @author Rod Johnson
31
+ * @author Sam Brannen
32
32
* @since 09.04.2003
33
33
* @see NoRollbackRuleAttribute
34
34
*/
35
35
@ SuppressWarnings ("serial" )
36
36
public class RollbackRuleAttribute implements Serializable {
37
37
38
38
/**
39
- * The {@link RollbackRuleAttribute rollback rule} for
39
+ * The {@linkplain RollbackRuleAttribute rollback rule} for
40
40
* {@link RuntimeException RuntimeExceptions}.
41
41
*/
42
42
public static final RollbackRuleAttribute ROLLBACK_ON_RUNTIME_EXCEPTIONS =
@@ -48,71 +48,81 @@ public class RollbackRuleAttribute implements Serializable{
48
48
* This way does multiple string comparisons, but how often do we decide
49
49
* whether to roll back a transaction following an exception?
50
50
*/
51
- private final String exceptionName ;
51
+ private final String exceptionPattern ;
52
52
53
53
54
54
/**
55
- * Create a new instance of the {@code RollbackRuleAttribute} class.
55
+ * Create a new instance of the {@code RollbackRuleAttribute} class
56
+ * for the given {@code exceptionType}.
56
57
* <p>This is the preferred way to construct a rollback rule that matches
57
- * the supplied {@link Exception} class , its subclasses, and its nested classes.
58
- * @param clazz throwable class ; must be {@link Throwable} or a subclass
58
+ * the supplied exception type , its subclasses, and its nested classes.
59
+ * @param exceptionType exception type ; must be {@link Throwable} or a subclass
59
60
* of {@code Throwable}
60
- * @throws IllegalArgumentException if the supplied {@code clazz } is
61
+ * @throws IllegalArgumentException if the supplied {@code exceptionType } is
61
62
* not a {@code Throwable} type or is {@code null}
62
63
*/
63
- public RollbackRuleAttribute (Class <?> clazz ) {
64
- Assert .notNull (clazz , "'clazz ' cannot be null" );
65
- if (!Throwable .class .isAssignableFrom (clazz )) {
64
+ public RollbackRuleAttribute (Class <?> exceptionType ) {
65
+ Assert .notNull (exceptionType , "'exceptionType ' cannot be null" );
66
+ if (!Throwable .class .isAssignableFrom (exceptionType )) {
66
67
throw new IllegalArgumentException (
67
- "Cannot construct rollback rule from [" + clazz .getName () + "]: it's not a Throwable" );
68
+ "Cannot construct rollback rule from [" + exceptionType .getName () + "]: it's not a Throwable" );
68
69
}
69
- this .exceptionName = clazz .getName ();
70
+ this .exceptionPattern = exceptionType .getName ();
70
71
}
71
72
72
73
/**
73
74
* Create a new instance of the {@code RollbackRuleAttribute} class
74
- * for the given {@code exceptionName }.
75
+ * for the given {@code exceptionPattern }.
75
76
* <p>This can be a substring, with no wildcard support at present. A value
76
77
* of "ServletException" would match
77
78
* {@code javax.servlet.ServletException} and subclasses, for example.
78
79
* <p><b>NB:</b> Consider carefully how specific the pattern is, and
79
80
* whether to include package information (which is not mandatory). For
80
81
* example, "Exception" will match nearly anything, and will probably hide
81
82
* other rules. "java.lang.Exception" would be correct if "Exception" was
82
- * meant to define a rule for all checked exceptions. With more unusual
83
+ * meant to define a rule for all checked exceptions. With more unique
83
84
* exception names such as "BaseBusinessException" there's no need to use a
84
85
* fully package-qualified name.
85
- * @param exceptionName the exception name pattern; can also be a fully
86
+ * @param exceptionPattern the exception name pattern; can also be a fully
86
87
* package-qualified class name
87
- * @throws IllegalArgumentException if the supplied
88
- * {@code exceptionName} is {@code null} or empty
88
+ * @throws IllegalArgumentException if the supplied {@code exceptionPattern}
89
+ * is {@code null} or empty
89
90
*/
90
- public RollbackRuleAttribute (String exceptionName ) {
91
- Assert .hasText (exceptionName , "'exceptionName ' cannot be null or empty" );
92
- this .exceptionName = exceptionName ;
91
+ public RollbackRuleAttribute (String exceptionPattern ) {
92
+ Assert .hasText (exceptionPattern , "'exceptionPattern ' cannot be null or empty" );
93
+ this .exceptionPattern = exceptionPattern ;
93
94
}
94
95
95
96
96
97
/**
97
- * Return the pattern for the exception name.
98
+ * Get the configured exception name pattern that this rule uses for matching.
99
+ * @see #getDepth(Throwable)
98
100
*/
99
101
public String getExceptionName () {
100
- return this .exceptionName ;
102
+ return this .exceptionPattern ;
101
103
}
102
104
103
105
/**
104
- * Return the depth of the superclass matching.
105
- * <p>{@code 0} means {@code ex} matches exactly. Returns
106
- * {@code -1} if there is no match. Otherwise, returns depth with the
107
- * lowest depth winning.
106
+ * Return the depth of the superclass matching, with the following semantics.
107
+ * <ul>
108
+ * <li>{@code -1} means this rule does not match the supplied {@code exception}.</li>
109
+ * <li>{@code 0} means this rule matches the supplied {@code exception} exactly.</li>
110
+ * <li>Any other positive value means this rule matches the supplied {@code exception}
111
+ * within the superclass hierarchy, where the value is the number of levels in the
112
+ * class hierarchy between the supplied {@code exception} and the exception against
113
+ * which this rule matches directly.</li>
114
+ * </ul>
115
+ * <p>When comparing roll back rules that match against a given exception, a rule
116
+ * with a lower matching depth wins. For example, a direct match ({@code depth == 0})
117
+ * wins over a match in the superclass hierarchy ({@code depth > 0}).
108
118
*/
109
- public int getDepth (Throwable ex ) {
110
- return getDepth (ex .getClass (), 0 );
119
+ public int getDepth (Throwable exception ) {
120
+ return getDepth (exception .getClass (), 0 );
111
121
}
112
122
113
123
114
124
private int getDepth (Class <?> exceptionClass , int depth ) {
115
- if (exceptionClass .getName ().contains (this .exceptionName )) {
125
+ if (exceptionClass .getName ().contains (this .exceptionPattern )) {
116
126
// Found it!
117
127
return depth ;
118
128
}
@@ -133,17 +143,17 @@ public boolean equals(@Nullable Object other) {
133
143
return false ;
134
144
}
135
145
RollbackRuleAttribute rhs = (RollbackRuleAttribute ) other ;
136
- return this .exceptionName .equals (rhs .exceptionName );
146
+ return this .exceptionPattern .equals (rhs .exceptionPattern );
137
147
}
138
148
139
149
@ Override
140
150
public int hashCode () {
141
- return this .exceptionName .hashCode ();
151
+ return this .exceptionPattern .hashCode ();
142
152
}
143
153
144
154
@ Override
145
155
public String toString () {
146
- return "RollbackRuleAttribute with pattern [" + this .exceptionName + "]" ;
156
+ return "RollbackRuleAttribute with pattern [" + this .exceptionPattern + "]" ;
147
157
}
148
158
149
159
}
0 commit comments