Class RollbackRuleAttribute

java.lang.Object
org.springframework.transaction.interceptor.RollbackRuleAttribute
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
NoRollbackRuleAttribute

public class RollbackRuleAttribute extends Object implements Serializable
Rule determining whether a given exception should cause a rollback.

Multiple such rules can be applied to determine whether a transaction should commit or rollback after an exception has been thrown.

Each rule is based on an exception type or exception pattern, supplied via RollbackRuleAttribute(Class) or RollbackRuleAttribute(String), respectively.

When a rollback rule is defined with an exception type, that type will be used to match against the type of a thrown exception and its super types, providing type safety and avoiding any unintentional matches that may occur when using a pattern. For example, a value of jakarta.servlet.ServletException.class will only match thrown exceptions of type jakarta.servlet.ServletException and its subclasses.

When a rollback rule is defined with an exception pattern, the pattern can be a fully qualified class name or a substring of a fully qualified class name for an exception type (which must be a subclass of Throwable), with no wildcard support at present. For example, a value of "jakarta.servlet.ServletException" or "ServletException" will match jakarta.servlet.ServletException and its subclasses.

See the javadocs for @Transactional for further details on rollback rule semantics, patterns, and warnings regarding possible unintentional matches with pattern-based rules.

Since:
09.04.2003
Author:
Rod Johnson, Sam Brannen
See Also:
  • Field Details

  • Constructor Details

    • RollbackRuleAttribute

      public RollbackRuleAttribute(Class<?> exceptionType)
      Create a new instance of the RollbackRuleAttribute class for the given exceptionType.

      This is the preferred way to construct a rollback rule that matches the supplied exception type and its subclasses with type safety.

      See the javadocs for @Transactional for further details on rollback rule semantics.

      Parameters:
      exceptionType - exception type; must be Throwable or a subclass of Throwable
      Throws:
      IllegalArgumentException - if the supplied exceptionType is not a Throwable type or is null
    • RollbackRuleAttribute

      public RollbackRuleAttribute(String exceptionPattern)
      Create a new instance of the RollbackRuleAttribute class for the given exceptionPattern.

      See the javadocs for @Transactional for further details on rollback rule semantics, patterns, and warnings regarding possible unintentional matches.

      For improved type safety and to avoid unintentional matches, use RollbackRuleAttribute(Class) instead.

      Parameters:
      exceptionPattern - the exception name pattern; can also be a fully package-qualified class name
      Throws:
      IllegalArgumentException - if the supplied exceptionPattern is null or empty
  • Method Details

    • getExceptionName

      public String getExceptionName()
      Get the configured exception name pattern that this rule uses for matching.
      See Also:
    • getDepth

      public int getDepth(Throwable exception)
      Return the depth of the superclass matching, with the following semantics.
      • -1 means this rule does not match the supplied exception.
      • 0 means this rule matches the supplied exception directly.
      • Any other positive value means this rule matches the supplied exception within the superclass hierarchy, where the value is the number of levels in the class hierarchy between the supplied exception and the exception against which this rule matches directly.

      When comparing roll back rules that match against a given exception, a rule with a lower matching depth wins. For example, a direct match (depth == 0) wins over a match in the superclass hierarchy (depth > 0).

      When constructed with an exception pattern via RollbackRuleAttribute(String), a match against a nested exception type or similarly named exception type will return a depth signifying a match at the corresponding level in the class hierarchy as if there had been a direct match.

    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object