001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.core.storage.sql.jdbc;
018
019import static javax.transaction.xa.XAException.XAER_INVAL;
020import static javax.transaction.xa.XAException.XAER_PROTO;
021import static javax.transaction.xa.XAException.XAER_RMERR;
022
023import java.sql.SQLException;
024
025import javax.transaction.xa.XAException;
026import javax.transaction.xa.XAResource;
027import javax.transaction.xa.Xid;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031
032/**
033 * Adapter for a simple JDBC Connection that gives it the XAResource interface, without actually implementing XA
034 * (prepare does nothing).
035 *
036 * @since 5.7
037 */
038public class XAResourceConnectionAdapter implements XAResource {
039
040    private static final Log log = LogFactory.getLog(XAResourceConnectionAdapter.class);
041
042    protected JDBCConnection owner;
043
044    protected Xid xid;
045
046    public XAResourceConnectionAdapter(JDBCConnection connection) {
047        owner = connection;
048    }
049
050    @Override
051    public void start(Xid xid, int flag) throws XAException {
052        if (flag == TMNOFLAGS) {
053            if (this.xid != null) {
054                throw newXAException(XAER_PROTO, "Already started");
055            }
056            this.xid = xid;
057            try {
058                owner.connection.setAutoCommit(false);
059            } catch (SQLException e) {
060                throw newXAException(XAER_RMERR, "Cannot set autoCommit=false");
061            }
062        } else {
063            // cannot support resume
064            throw newXAException(XAER_INVAL, "Invalid flag: " + flag);
065        }
066    }
067
068    @Override
069    public void end(Xid xid, int flag) throws XAException {
070        if (xid != this.xid) {
071            throw newXAException(XAER_INVAL, "Invalid Xid");
072        }
073        if (flag != TMSUCCESS && flag != TMFAIL) {
074            throw newXAException(XAER_INVAL, "Invalid flag: " + flag);
075        }
076    }
077
078    @Override
079    public int prepare(Xid xid) throws XAException {
080        return XA_OK;
081    }
082
083    @Override
084    public void commit(Xid xid, boolean flag) throws XAException {
085        if (this.xid == null || !this.xid.equals(xid)) {
086            throw newXAException(XAER_INVAL, "Invalid Xid");
087        }
088        this.xid = null;
089        try {
090            owner.connection.commit();
091        } catch (SQLException e) {
092            throw newXAException(XAER_RMERR, "Cannot commit", e);
093        } finally {
094            try {
095                owner.connection.setAutoCommit(true);
096            } catch (SQLException e) {
097                log.error("Cannot set autoCommit=true", e);
098            }
099        }
100    }
101
102    @Override
103    public void rollback(Xid xid) throws XAException {
104        if (this.xid == null || !this.xid.equals(xid)) {
105            throw newXAException(XAER_INVAL, "Invalid Xid");
106        }
107        this.xid = null;
108        try {
109            owner.connection.rollback();
110        } catch (SQLException e) {
111            throw newXAException(XAER_RMERR, "Cannot rollback", e);
112        } finally {
113            try {
114                owner.connection.setAutoCommit(true);
115            } catch (SQLException e) {
116                log.error("Cannot set autoCommit=true", e);
117            }
118        }
119    }
120
121    @Override
122    public void forget(Xid xid) throws XAException {
123        throw newXAException(XAER_PROTO, "Unsupported method");
124    }
125
126    @Override
127    public Xid[] recover(int n) throws XAException {
128        return new Xid[0];
129    }
130
131    @Override
132    public int getTransactionTimeout() throws XAException {
133        return 0;
134    }
135
136    @Override
137    public boolean setTransactionTimeout(int txTimeout) throws XAException {
138        return false;
139    }
140
141    @Override
142    public boolean isSameRM(XAResource xares) throws XAException {
143        return this == xares;
144    }
145
146    protected static XAException newXAException(int errorCode, String message, Exception cause) {
147        return (XAException) newXAException(errorCode, message).initCause(cause);
148    }
149
150    protected static XAException newXAException(int errorCode, String message) {
151        XAException e = new XAException(message);
152        e.errorCode = errorCode;
153        return e;
154    }
155
156}