📄 transactionutil.java
字号:
/*
* $Id: TransactionUtil.java,v 1.4 2003/12/04 21:54:20 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.entity.transaction;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.sql.XAConnection;
import javax.transaction.*;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import javax.transaction.xa.XAResource;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
/**
* <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 $Revision: 1.4 $
* @since 2.0
*/
public class TransactionUtil implements Status {
// Debug module name
public static final String module = TransactionUtil.class.getName();
/** 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 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) {
Debug.logVerbose("[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun", module);
throw new GenericTransactionException("The current transaction is marked for rollback, should stop immediately.");
//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 transaction stamps, just in case...
clearTransactionStamps();
// initialize the start stamp
getTransactionStartStamp();
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;
}
}
/** 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();
Debug.logVerbose("[TransactionUtil.commit] transaction committed", module);
// clear out the stamps to keep it clean
clearTransactionStamps();
} 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);
}
}
/** 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) throws GenericTransactionException {
if (beganTransaction) {
TransactionUtil.rollback();
} else {
TransactionUtil.setRollbackOnly();
}
}
/** Rolls back transaction in the current thread IF transactions are available */
public static void rollback() throws GenericTransactionException {
UserTransaction ut = TransactionFactory.getUserTransaction();
if (ut != null) {
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -