1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.transaction.interceptor;
  17. import org.springframework.transaction.support.DefaultTransactionDefinition;
  18. /**
  19. * Transaction attribute that takes the EJB approach to rolling
  20. * back on runtime, but not checked, exceptions.
  21. * @author Rod Johnson
  22. * @since 16-Mar-2003
  23. * @version $Id: DefaultTransactionAttribute.java,v 1.4 2004/03/18 02:46:05 trisberg Exp $
  24. */
  25. public class DefaultTransactionAttribute extends DefaultTransactionDefinition implements TransactionAttribute {
  26. /** Prefix for rollback-on-exception rules in description strings */
  27. public static final String ROLLBACK_RULE_PREFIX = "-";
  28. /** Prefix for commit-on-exception rules in description strings */
  29. public static final String COMMIT_RULE_PREFIX = "+";
  30. public DefaultTransactionAttribute() {
  31. }
  32. public DefaultTransactionAttribute(int propagationBehavior) {
  33. super(propagationBehavior);
  34. }
  35. /**
  36. * Default behavior is as with EJB: rollback on unchecked exception.
  37. * Additionally attempt to rollback on Error.
  38. * Consistent with TransactionTemplate's behavior.
  39. */
  40. public boolean rollbackOn(Throwable ex) {
  41. return (ex instanceof RuntimeException || ex instanceof Error);
  42. }
  43. /**
  44. * Return a description of this transaction attribute.
  45. * The format matches the one used by TransactionAttributeEditor,
  46. * to be able to feed toString results into TransactionAttribute properties.
  47. * @see org.springframework.transaction.interceptor.TransactionAttributeEditor
  48. */
  49. public String toString() {
  50. StringBuffer result = getDefinitionDescription();
  51. result.append(',');
  52. result.append(ROLLBACK_RULE_PREFIX + "RuntimeException");
  53. return result.toString();
  54. }
  55. }