⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xasupport.java

📁 jtds的源码 是你学习java的好东西
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                con.getXaState() != XA_PREPARE) {
                // Connection not ended or prepared
                raiseXAException(XAException.XAER_PROTO);
            }
            JtdsXid tran = (JtdsXid)con.getXid();
            if (tran == null || !tran.equals(lxid)) {
                raiseXAException(XAException.XAER_NOTA);
            }
            con.setXid(null);
            try {
                con.commit();
            } catch (SQLException e) {
                raiseXAException(e);
            } finally {
                try {
                    con.setAutoCommit(true);
                } catch(SQLException e) {
                    Logger.println("xa_close: setAutoCommit() returned " + e);
                }
                con.setXaState(XA_OPEN);
            }
            return;
        }
        //
        // Execute xa_commit via MSDTC
        //
        int args[] = new int[5];
        args[1] = XA_COMMIT;
        args[2] = xaConId;
        args[3] = XA_RMID;
        args[4] = (onePhase) ? XAResource.TMONEPHASE : XAResource.TMNOFLAGS;
        try {
            ((ConnectionJDBC2) connection).sendXaPacket(args, toBytesXid(xid));
        } catch (SQLException e) {
            raiseXAException(e);
        }
        if (args[0] != XAResource.XA_OK) {
            raiseXAException(args[0]);
        }
    }

    /**
     * Invoke the xa_rollback routine on the SQL Server.
     *
     * @param connection JDBC Connection enlisted in the transaction
     * @param xaConId    the connection ID allocated by the server
     * @param xid        the XA Transaction ID object
     * @exception javax.transaction.xa.XAException
     *             if an error condition occurs
     */
    public static void xa_rollback(Connection connection, int xaConId, Xid xid)
            throws XAException {

        ConnectionJDBC2 con = (ConnectionJDBC2)connection;
        if (con.isXaEmulation()) {
            //
            // Emulate xa_rollback method
            //
            JtdsXid lxid = new JtdsXid(xid);
            if (con.getXaState()!= XA_END && con.getXaState() != XA_PREPARE) {
                // Connection not ended
                raiseXAException(XAException.XAER_PROTO);
            }
            JtdsXid tran = (JtdsXid)con.getXid();
            if (tran == null || !tran.equals(lxid)) {
                raiseXAException(XAException.XAER_NOTA);
            }
            con.setXid(null);
            try {
                con.rollback();
            } catch (SQLException e) {
                raiseXAException(e);
            } finally {
                try {
                    con.setAutoCommit(true);
                } catch(SQLException e) {
                    Logger.println("xa_close: setAutoCommit() returned " + e);
                }
                con.setXaState(XA_OPEN);
            }
            return;
        }
        //
        // Execute xa_rollback via MSDTC
        //
        int args[] = new int[5];
        args[1] = XA_ROLLBACK;
        args[2] = xaConId;
        args[3] = XA_RMID;
        args[4] = XAResource.TMNOFLAGS;
        try {
            ((ConnectionJDBC2) connection).sendXaPacket(args, toBytesXid(xid));
        } catch (SQLException e) {
            raiseXAException(e);
        }
        if (args[0] != XAResource.XA_OK) {
            raiseXAException(args[0]);
        }
    }

    /**
     * Invoke the xa_recover routine on the SQL Server.
     * <p/>
     * This version of xa_recover will return all XIDs on the first call.
     *
     * @param connection JDBC Connection enlisted in the transaction
     * @param xaConId    the connection ID allocated by the server
     * @param flags      XA Flags for start command
     * @return transactions to recover as a <code>Xid[]</code>
     * @exception javax.transaction.xa.XAException
     *             if an error condition occurs
     */
    public static Xid[] xa_recover(Connection connection, int xaConId, int flags)
            throws XAException {

        ConnectionJDBC2 con = (ConnectionJDBC2)connection;
        if (con.isXaEmulation()) {
            //
            // Emulate xa_recover method
            //
            // There is no state available all uncommited transactions
            // will have been rolled back by the server.
            if (flags != XAResource.TMSTARTRSCAN &&
                flags != XAResource.TMENDRSCAN &&
                flags != XAResource.TMNOFLAGS) {
                raiseXAException(XAException.XAER_INVAL);
            }
            return new JtdsXid[0];
        }
        //
        // Execute xa_recover via MSDTC
        //
        int args[] = new int[5];
        args[1] = XA_RECOVER;
        args[2] = xaConId;
        args[3] = XA_RMID;
        args[4] = XAResource.TMNOFLAGS;
        Xid[] list = null;

        if (flags != XAResource.TMSTARTRSCAN) {
            return new JtdsXid[0];
        }

        try {
            byte[][] buffer = ((ConnectionJDBC2) connection).sendXaPacket(args, null);
            if (args[0] >= 0) {
                int n = buffer.length;
                list = new JtdsXid[n];
                for (int i = 0; i < n; i++) {
                    list[i] = new JtdsXid(buffer[i], 0);
                }
            }
        } catch (SQLException e) {
            raiseXAException(e);
        }
        if (args[0] < 0) {
            raiseXAException(args[0]);
        }
        if (list == null) {
            list = new JtdsXid[0];
        }
        return list;
    }

    /**
     * Invoke the xa_forget routine on the SQL Server.
     *
     * @param connection JDBC Connection enlisted in the transaction
     * @param xaConId    the connection ID allocated by the server
     * @param xid        the XA Transaction ID object
     * @exception javax.transaction.xa.XAException
     *             if an error condition occurs
     */
    public static void xa_forget(Connection connection, int xaConId, Xid xid)
            throws XAException {

        ConnectionJDBC2 con = (ConnectionJDBC2)connection;
        if (con.isXaEmulation()) {
            //
            // Emulate xa_forget method
            //
            JtdsXid lxid = new JtdsXid(xid);
            JtdsXid tran = (JtdsXid)con.getXid();
            if (tran == null || !tran.equals(lxid)) {
                raiseXAException(XAException.XAER_NOTA);
            }
            if (con.getXaState() != XA_END && con.getXaState() != XA_PREPARE) {
               // Connection not ended
               raiseXAException(XAException.XAER_PROTO);
            }
            con.setXid(null);
            try {
                 con.rollback();
            } catch (SQLException e) {
                raiseXAException(e);
            } finally {
                try {
                    con.setAutoCommit(true);
                } catch(SQLException e) {
                    Logger.println("xa_close: setAutoCommit() returned " + e);
                }
                con.setXaState(XA_OPEN);
            }
            return;
        }
        //
        // Execute xa_forget via MSDTC
        //
        int args[] = new int[5];
        args[1] = XA_FORGET;
        args[2] = xaConId;
        args[3] = XA_RMID;
        args[4] = XAResource.TMNOFLAGS;
        try {
            ((ConnectionJDBC2) connection).sendXaPacket(args, toBytesXid(xid));
        } catch (SQLException e) {
            raiseXAException(e);
        }
        if (args[0] != XAResource.XA_OK) {
            raiseXAException(args[0]);
        }
    }

    /**
     * Construct and throw an <code>XAException</code> with an explanatory message derived from the
     * <code>SQLException</code> and the XA error code set to <code>XAER_RMFAIL</code>.
     *
     * @param sqle The SQLException.
     * @exception javax.transaction.xa.XAException
     *             exception derived from the code>SQLException</code>
     */
    public static void raiseXAException(SQLException sqle)
            throws XAException {
        XAException e = new XAException(sqle.getMessage());
        e.errorCode = XAException.XAER_RMFAIL;
        Logger.println("XAException: " + e.getMessage());
        throw e;
    }

    /**
     * Construct and throw an <code>XAException</code> with an explanatory message and the XA error code set.
     *
     * @param errorCode the XA Error code
     * @exception javax.transaction.xa.XAException
     *             the constructed exception
     */
    public static void raiseXAException(int errorCode)
            throws XAException {
        String err = "xaerunknown";
        switch (errorCode) {
            case XAException.XA_RBROLLBACK:
                err = "xarbrollback";
                break;
            case XAException.XA_RBCOMMFAIL:
                err = "xarbcommfail";
                break;
            case XAException.XA_RBDEADLOCK:
                err = "xarbdeadlock";
                break;
            case XAException.XA_RBINTEGRITY:
                err = "xarbintegrity";
                break;
            case XAException.XA_RBOTHER:
                err = "xarbother";
                break;
            case XAException.XA_RBPROTO:
                err = "xarbproto";
                break;
            case XAException.XA_RBTIMEOUT:
                err = "xarbtimeout";
                break;
            case XAException.XA_RBTRANSIENT:
                err = "xarbtransient";
                break;
            case XAException.XA_NOMIGRATE:
                err = "xanomigrate";
                break;
            case XAException.XA_HEURHAZ:
                err = "xaheurhaz";
                break;
            case XAException.XA_HEURCOM:
                err = "xaheurcom";
                break;
            case XAException.XA_HEURRB:
                err = "xaheurrb";
                break;
            case XAException.XA_HEURMIX:
                err = "xaheurmix";
                break;
            case XAException.XA_RETRY:
                err = "xaretry";
                break;
            case XAException.XA_RDONLY:
                err = "xardonly";
                break;
            case XAException.XAER_ASYNC:
                err = "xaerasync";
                break;
            case XAException.XAER_NOTA:
                err = "xaernota";
                break;
            case XAException.XAER_INVAL:
                err = "xaerinval";
                break;
            case XAException.XAER_PROTO:
                err = "xaerproto";
                break;
            case XAException.XAER_RMERR:
                err = "xaerrmerr";
                break;
            case XAException.XAER_RMFAIL:
                err = "xaerrmfail";
                break;
            case XAException.XAER_DUPID:
                err = "xaerdupid";
                break;
            case XAException.XAER_OUTSIDE:
                err = "xaeroutside";
                break;
        }
        XAException e = new XAException(Messages.get("error.xaexception." + err));
        e.errorCode = errorCode;
        Logger.println("XAException: " + e.getMessage());
        throw e;
    }

    // ------------- Private methods  ---------

    /**
     * Format an XA transaction ID into a 140 byte array.
     *
     * @param xid the XA transaction ID
     * @return the formatted ID as a <code>byte[]</code>
     */
    private static byte[] toBytesXid(Xid xid) {
        byte[] buffer = new byte[12 +
                xid.getGlobalTransactionId().length +
                xid.getBranchQualifier().length];
        int fmt = xid.getFormatId();
        buffer[0] = (byte) fmt;
        buffer[1] = (byte) (fmt >> 8);
        buffer[2] = (byte) (fmt >> 16);
        buffer[3] = (byte) (fmt >> 24);
        buffer[4] = (byte) xid.getGlobalTransactionId().length;
        buffer[8] = (byte) xid.getBranchQualifier().length;
        System.arraycopy(xid.getGlobalTransactionId(), 0, buffer, 12, buffer[4]);
        System.arraycopy(xid.getBranchQualifier(), 0, buffer, 12 + buffer[4], buffer[8]);
        return buffer;
    }

    private XASupport() {
        // Prevent an instance of this class being created.
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -