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

📄 transactionutil.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * $Id: TransactionUtil.java 6778 2006-02-20 05:13:55Z jonesde $ * * Copyright 2001-2006 The Apache Software Foundation *  * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *  * http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */package org.ofbiz.entity.transaction;import java.sql.Connection;import java.sql.SQLException;import java.sql.Timestamp;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import javax.sql.XAConnection;import javax.transaction.*;import javax.transaction.xa.XAException;import javax.transaction.xa.XAResource;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilValidate;/** * <p>Transaction Utility to help with some common transaction tasks * <p>Provides a wrapper around the transaction objects to allow for changes in underlying implementations in the future. * * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version    $Rev: 6778 $ * @since      2.0 */public class TransactionUtil implements Status {    // Debug module name    public static final String module = TransactionUtil.class.getName();    public static Map debugResMap = new HashMap();    public static boolean debugResources = true;    /** Begins a transaction in the current thread IF transactions are available; only     * tries if the current transaction status is ACTIVE, if not active it returns false.     * If and on only if it begins a transaction it will return true. In other words, if     * a transaction is already in place it will return false and do nothing.     */    public static boolean begin() throws GenericTransactionException {        return begin(0);    }    /** Begins a transaction in the current thread IF transactions are available; only     * tries if the current transaction status is ACTIVE, if not active it returns false.     * If and on only if it begins a transaction it will return true. In other words, if     * a transaction is already in place it will return false and do nothing.     */    public static synchronized boolean begin(int timeout) throws GenericTransactionException {        UserTransaction ut = TransactionFactory.getUserTransaction();        if (ut != null) {            try {                int currentStatus = ut.getStatus();                Debug.logVerbose("[TransactionUtil.begin] current status : " + getTransactionStateString(currentStatus), module);                if (currentStatus == Status.STATUS_ACTIVE) {                    Debug.logVerbose("[TransactionUtil.begin] active transaction in place, so no transaction begun", module);                    return false;                } else if (currentStatus == Status.STATUS_MARKED_ROLLBACK) {                    Exception e = getTransactionBeginStack();                    if (e != null) {                        Debug.logWarning(e, "[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun; this stack trace shows when the exception began: ", module);                    } else {                        Debug.logWarning("[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun", module);                    }                    RollbackOnlyCause roc = getSetRollbackOnlyCause();                    // do we have a cause? if so, throw special exception                    if (roc != null && !roc.isEmpty()) {                        throw new GenericTransactionException("The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: " + roc.getCauseMessage(), roc.getCauseThrowable());                    } else {                        return false;                    }                }                // set the timeout for THIS transaction                if (timeout > 0) {                    ut.setTransactionTimeout(timeout);                    Debug.logVerbose("[TransactionUtil.begin] set transaction timeout to : " + timeout + " seconds", module);                }                // begin the transaction                ut.begin();                Debug.logVerbose("[TransactionUtil.begin] transaction begun", module);                // reset the timeout to the default                if (timeout > 0) {                    ut.setTransactionTimeout(0);                }                // reset the transaction stamps, just in case...                clearTransactionStamps();                // initialize the start stamp                getTransactionStartStamp();                // set the tx begin stack placeholder                setTransactionBeginStack();                // initialize the debug resource                if (debugResources) {                    DebugXaResource dxa = new DebugXaResource();                    try {                        dxa.enlist();                    } catch (XAException e) {                        Debug.logError(e, module);                    }                }                return true;            } catch (NotSupportedException e) {                //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();                throw new GenericTransactionException("Not Supported error, could not begin transaction (probably a nesting problem)", e);            } catch (SystemException e) {                //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();                throw new GenericTransactionException("System error, could not begin transaction", e);            }        } else {            Debug.logInfo("[TransactionUtil.begin] no user transaction, so no transaction begun", module);            return false;        }    }    /** Gets the status of the transaction in the current thread IF     * transactions are available, otherwise returns STATUS_NO_TRANSACTION */    public static int getStatus() throws GenericTransactionException {        UserTransaction ut = TransactionFactory.getUserTransaction();        if (ut != null) {            try {                return ut.getStatus();            } catch (SystemException e) {                throw new GenericTransactionException("System error, could not get status", e);            }        } else {            return STATUS_NO_TRANSACTION;        }    }    public static boolean isTransactionInPlace() throws GenericTransactionException {        int status = getStatus();        if (status == STATUS_NO_TRANSACTION) {            return false;        } else {            return true;        }    }    /** Commits the transaction in the current thread IF transactions are available     *  AND if beganTransaction is true     */    public static void commit(boolean beganTransaction) throws GenericTransactionException {        if (beganTransaction) {            TransactionUtil.commit();        }    }    /** Commits the transaction in the current thread IF transactions are available */    public static void commit() throws GenericTransactionException {        UserTransaction ut = TransactionFactory.getUserTransaction();        if (ut != null) {            try {                int status = ut.getStatus();                Debug.logVerbose("[TransactionUtil.commit] current status : " + getTransactionStateString(status), module);                if (status != STATUS_NO_TRANSACTION) {                    ut.commit();                    // clear out the stamps to keep it clean                    clearTransactionStamps();                    // clear out the stack too                    clearTransactionBeginStack();                    clearSetRollbackOnlyCause();                    Debug.logVerbose("[TransactionUtil.commit] transaction committed", module);                } else {                    Debug.logInfo("[TransactionUtil.commit] Not committing transaction, status is STATUS_NO_TRANSACTION", module);                }            } catch (RollbackException e) {                if (Debug.infoOn()) Thread.dumpStack();                //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();                throw new GenericTransactionException("Roll back error, could not commit transaction, was rolled back instead", e);            } catch (HeuristicMixedException e) {                //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();                throw new GenericTransactionException("Could not commit transaction, HeuristicMixed exception", e);            } catch (HeuristicRollbackException e) {                //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();                throw new GenericTransactionException("Could not commit transaction, HeuristicRollback exception", e);            } catch (SystemException e) {                //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();                throw new GenericTransactionException("System error, could not commit transaction", e);            }        } else {            Debug.logInfo("[TransactionUtil.commit] UserTransaction is null, not commiting", module);        }    }    /** @deprecated */    public static void rollback(boolean beganTransaction) throws GenericTransactionException {        Debug.logWarning("WARNING: called rollback without debug/error info; it is recommended to always pass this to make otherwise tricky bugs much easier to track down.", module);        rollback(beganTransaction, null, null);    }        /** Rolls back transaction in the current thread IF transactions are available     *  AND if beganTransaction is true; if beganTransaction is not true,     *  setRollbackOnly is called to insure that the transaction will be rolled back     */    public static void rollback(boolean beganTransaction, String causeMessage, Throwable causeThrowable) throws GenericTransactionException {        if (beganTransaction) {            TransactionUtil.rollback();        } else {

⌨️ 快捷键说明

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