001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.sql.xa;
019
020import javax.transaction.xa.XAException;
021
022import net.sf.hajdbc.AbstractExceptionFactory;
023import net.sf.hajdbc.ExceptionType;
024import net.sf.hajdbc.dialect.Dialect;
025import net.sf.hajdbc.durability.Durability.Phase;
026
027/**
028 * @author Paul Ferraro
029 */
030public class XAExceptionFactory extends AbstractExceptionFactory<XAException>
031{
032        private static final long serialVersionUID = -8802252233361030433L;
033
034        public XAExceptionFactory()
035        {
036                super(XAException.class);
037        }
038
039        /**
040         * {@inheritDoc}
041         * @see net.sf.hajdbc.ExceptionFactory#createException(java.lang.String)
042         */
043        @Override
044        public XAException createException(String message)
045        {
046                return new XAException(message);
047        }
048
049        /**
050         * {@inheritDoc}
051         * @see net.sf.hajdbc.ExceptionFactory#equals(java.lang.Exception, java.lang.Exception)
052         */
053        @Override
054        public boolean equals(XAException exception1, XAException exception2)
055        {
056                // Match by error code, if defined
057                if ((exception1.errorCode != 0) || (exception2.errorCode != 0))
058                {
059                        return exception1.errorCode == exception2.errorCode;
060                }
061
062                return super.equals(exception1, exception2);
063        }
064
065        /**
066         * {@inheritDoc}
067         * @see net.sf.hajdbc.ExceptionFactory#indicatesFailure(java.lang.Exception, net.sf.hajdbc.dialect.Dialect)
068         */
069        @Override
070        public boolean indicatesFailure(XAException exception, Dialect dialect)
071        {
072                return dialect.indicatesFailure(exception);
073        }
074
075        /**
076         * {@inheritDoc}
077         * @see net.sf.hajdbc.ExceptionFactory#getType()
078         */
079        @Override
080        public ExceptionType getType()
081        {
082                return ExceptionType.XA;
083        }
084
085        /**
086         * {@inheritDoc}
087         * @see net.sf.hajdbc.ExceptionFactory#correctHeuristic(java.lang.Exception, net.sf.hajdbc.durability.Durability.Phase)
088         */
089        @Override
090        public boolean correctHeuristic(XAException exception, Phase phase)
091        {
092                switch (phase)
093                {
094                        case COMMIT:
095                        {
096                                return exception.errorCode == XAException.XA_HEURCOM;
097                        }
098                        case ROLLBACK:
099                        {
100                                return exception.errorCode == XAException.XA_HEURRB;
101                        }
102                        default:
103                        {
104                                return false;
105                        }
106                }
107        }
108}