001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.transaction.interceptor;
018
019import org.springframework.transaction.support.DefaultTransactionDefinition;
020import org.springframework.util.StringUtils;
021
022/**
023 * Spring's common transaction attribute implementation.
024 * Rolls back on runtime, but not checked, exceptions by default.
025 *
026 * @author Rod Johnson
027 * @author Juergen Hoeller
028 * @since 16.03.2003
029 */
030@SuppressWarnings("serial")
031public class DefaultTransactionAttribute extends DefaultTransactionDefinition implements TransactionAttribute {
032
033        private String qualifier;
034
035        private String descriptor;
036
037
038        /**
039         * Create a new DefaultTransactionAttribute, with default settings.
040         * Can be modified through bean property setters.
041         * @see #setPropagationBehavior
042         * @see #setIsolationLevel
043         * @see #setTimeout
044         * @see #setReadOnly
045         * @see #setName
046         */
047        public DefaultTransactionAttribute() {
048                super();
049        }
050
051        /**
052         * Copy constructor. Definition can be modified through bean property setters.
053         * @see #setPropagationBehavior
054         * @see #setIsolationLevel
055         * @see #setTimeout
056         * @see #setReadOnly
057         * @see #setName
058         */
059        public DefaultTransactionAttribute(TransactionAttribute other) {
060                super(other);
061        }
062
063        /**
064         * Create a new DefaultTransactionAttribute with the given
065         * propagation behavior. Can be modified through bean property setters.
066         * @param propagationBehavior one of the propagation constants in the
067         * TransactionDefinition interface
068         * @see #setIsolationLevel
069         * @see #setTimeout
070         * @see #setReadOnly
071         */
072        public DefaultTransactionAttribute(int propagationBehavior) {
073                super(propagationBehavior);
074        }
075
076
077        /**
078         * Associate a qualifier value with this transaction attribute.
079         * <p>This may be used for choosing a corresponding transaction manager
080         * to process this specific transaction.
081         * @since 3.0
082         */
083        public void setQualifier(String qualifier) {
084                this.qualifier = qualifier;
085        }
086
087        /**
088         * Return a qualifier value associated with this transaction attribute.
089         * @since 3.0
090         */
091        @Override
092        public String getQualifier() {
093                return this.qualifier;
094        }
095
096        /**
097         * Set a descriptor for this transaction attribute,
098         * e.g. indicating where the attribute is applying.
099         * @since 4.3.4
100         */
101        public void setDescriptor(String descriptor) {
102                this.descriptor = descriptor;
103        }
104
105        /**
106         * Return a descriptor for this transaction attribute,
107         * or {@code null} if none.
108         * @since 4.3.4
109         */
110        public String getDescriptor() {
111                return this.descriptor;
112        }
113
114        /**
115         * The default behavior is as with EJB: rollback on unchecked exception
116         * ({@link RuntimeException}), assuming an unexpected outcome outside of any
117         * business rules. Additionally, we also attempt to rollback on {@link Error} which
118         * is clearly an unexpected outcome as well. By contrast, a checked exception is
119         * considered a business exception and therefore a regular expected outcome of the
120         * transactional business method, i.e. a kind of alternative return value which
121         * still allows for regular completion of resource operations.
122         * <p>This is largely consistent with TransactionTemplate's default behavior,
123         * except that TransactionTemplate also rolls back on undeclared checked exceptions
124         * (a corner case). For declarative transactions, we expect checked exceptions to be
125         * intentionally declared as business exceptions, leading to a commit by default.
126         * @see org.springframework.transaction.support.TransactionTemplate#execute
127         */
128        @Override
129        public boolean rollbackOn(Throwable ex) {
130                return (ex instanceof RuntimeException || ex instanceof Error);
131        }
132
133
134        /**
135         * Return an identifying description for this transaction attribute.
136         * <p>Available to subclasses, for inclusion in their {@code toString()} result.
137         */
138        protected final StringBuilder getAttributeDescription() {
139                StringBuilder result = getDefinitionDescription();
140                if (StringUtils.hasText(this.qualifier)) {
141                        result.append("; '").append(this.qualifier).append("'");
142                }
143                return result;
144        }
145
146}