001/*
002 * Copyright 2002-2012 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;
018
019/**
020 * Exception that represents a transaction failure caused by a heuristic
021 * decision on the side of the transaction coordinator.
022 *
023 * @author Rod Johnson
024 * @author Juergen Hoeller
025 * @since 17.03.2003
026 */
027@SuppressWarnings("serial")
028public class HeuristicCompletionException extends TransactionException {
029
030        /**
031         * Values for the outcome state of a heuristically completed transaction.
032         */
033        public static final int STATE_UNKNOWN = 0;
034        public static final int STATE_COMMITTED = 1;
035        public static final int STATE_ROLLED_BACK = 2;
036        public static final int STATE_MIXED = 3;
037
038
039        public static String getStateString(int state) {
040                switch (state) {
041                        case STATE_COMMITTED:
042                                return "committed";
043                        case STATE_ROLLED_BACK:
044                                return "rolled back";
045                        case STATE_MIXED:
046                                return "mixed";
047                        default:
048                                return "unknown";
049                }
050        }
051
052
053        /**
054         * The outcome state of the transaction: have some or all resources been committed?
055         */
056        private int outcomeState = STATE_UNKNOWN;
057
058
059        /**
060         * Constructor for HeuristicCompletionException.
061         * @param outcomeState the outcome state of the transaction
062         * @param cause the root cause from the transaction API in use
063         */
064        public HeuristicCompletionException(int outcomeState, Throwable cause) {
065                super("Heuristic completion: outcome state is " + getStateString(outcomeState), cause);
066                this.outcomeState = outcomeState;
067        }
068
069        /**
070         * Return the outcome state of the transaction state,
071         * as one of the constants in this class.
072         * @see #STATE_UNKNOWN
073         * @see #STATE_COMMITTED
074         * @see #STATE_ROLLED_BACK
075         * @see #STATE_MIXED
076         */
077        public int getOutcomeState() {
078                return outcomeState;
079        }
080
081}