use of javax.transaction.Transaction in project tomee by apache.
the class ManagedConnection method invoke.
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
// first some Object method management
final String mtdName = method.getName();
if ("toString".equals(mtdName)) {
return "ManagedConnection{" + delegate + "}";
}
if ("hashCode".equals(mtdName)) {
return hashCode();
}
if ("equals".equals(mtdName)) {
InvocationHandler handler;
return args[0] == this || ((handler = unwrapHandler(args[0])) == this) || (delegate != null && delegate.equals(unwrapDelegate(args[0], handler)));
}
// allow to get delegate if needed by the underlying program
if (Wrapper.class == method.getDeclaringClass() && args.length == 1 && Connection.class == args[0]) {
if ("isWrapperFor".equals(mtdName)) {
return true;
}
if ("unwrap".equals(mtdName)) {
return delegate;
}
}
// here the real logic starts
try {
final Transaction transaction = transactionManager.getTransaction();
// shouldn't be used without a transaction but if so just delegate to the actual connection
if (transaction == null) {
if ("close".equals(mtdName)) {
if (delegate == null) {
// no need to get a connection
return close();
}
closeConnection(true);
return null;
}
if ("isClosed".equals(mtdName) && closed) {
return true;
}
if (delegate == null) {
newConnection();
}
return invoke(method, delegate, args);
}
// if we have a tx check it is the same this connection is linked to
if (currentTransaction != null && isUnderTransaction(currentTransaction.getStatus())) {
if (!currentTransaction.equals(transaction)) {
throw new SQLException("Connection can not be used while enlisted in another transaction");
}
return invokeUnderTransaction(method, args);
}
// get the already bound connection to the current transaction or enlist this one in the tx
final int transactionStatus = transaction.getStatus();
if (isUnderTransaction(transactionStatus)) {
Connection connection = Connection.class.cast(registry.getResource(key));
if (connection == null && delegate == null) {
newConnection();
currentTransaction = transaction;
try {
if (!transaction.enlistResource(getXAResource())) {
closeConnection(true);
throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'.");
}
} catch (final RollbackException ignored) {
// no-op
} catch (final SystemException e) {
throw new SQLException("Unable to enlist connection the transaction", e);
}
registry.putResource(key, delegate);
transaction.registerSynchronization(new ClosingSynchronization());
if (xaConnection == null) {
try {
setAutoCommit(false);
} catch (final SQLException xae) {
// we are alreay in a transaction so this can't be called from a user perspective - some XA DataSource prevents it in their code
final String message = "Can't set auto commit to false cause the XA datasource doesn't support it, this is likely an issue";
final Logger logger = Logger.getInstance(LogCategory.OPENEJB_RESOURCE_JDBC, ManagedConnection.class);
if (logger.isDebugEnabled()) {
// we don't want to print the exception by default
logger.warning(message, xae);
} else {
logger.warning(message);
}
}
}
} else if (delegate == null) {
// shouldn't happen
delegate = connection;
}
return invokeUnderTransaction(method, args);
}
if ("isClosed".equals(mtdName) && closed) {
return true;
}
if ("close".equals(mtdName)) {
// let it be handled by the ClosingSynchronisation since we have a tx there
return close();
}
// we shouldn't come here, tempted to just throw an exception
if (delegate == null) {
newConnection();
}
return invoke(method, delegate, args);
} catch (final InvocationTargetException ite) {
throw ite.getTargetException();
}
}
use of javax.transaction.Transaction in project tomee by apache.
the class SimpleTransactionSynchronizationRegistry method getActiveTransaction.
private Transaction getActiveTransaction() {
try {
final Transaction transaction = transactionManager.getTransaction();
if (transaction == null) {
throw new IllegalStateException("No transaction active");
}
final int status = transaction.getStatus();
if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) {
throw new IllegalStateException("No transaction active");
}
return transaction;
} catch (final SystemException e) {
throw new IllegalStateException("No transaction active", e);
}
}
use of javax.transaction.Transaction in project tomee by apache.
the class TxBeanManaged method commit.
public void commit() throws ApplicationException, SystemException {
try {
// The Container must detect the case in which a transaction was started, but
// not completed, in the business method, and handle it as follows:
final Transaction currentTx = getTransaction();
if (currentTx != null) {
final String message = "The EJB started a transaction but did not complete it.";
/* [1] Log this as an application error ********/
logger.error(message);
/* [2] Roll back the started transaction *******/
try {
rollbackTransaction(currentTx);
} catch (final Throwable t) {
// no-op
}
/* [3] Throw the RemoteException to the client */
throw new ApplicationException(new RemoteException(message));
}
fireNonTransactionalCompletion();
} finally {
resumeTransaction(clientTx);
}
}
use of javax.transaction.Transaction in project tomee by apache.
the class JtaTransactionPolicy method beginTransaction.
protected Transaction beginTransaction() throws SystemException {
final Transaction transaction;
try {
transactionManager.begin();
transaction = transactionManager.getTransaction();
} catch (final Exception e) {
txLogger.error("The Transaction Manager has encountered an unexpected error condition while attempting to begin a new transaction: {0}", e.getMessage());
throw new SystemException(e);
}
if (transaction == null) {
throw new SystemException("Failed to begin a new transaction");
}
txLogger.debug("TX {0}: Started transaction {1}", transactionType, transaction);
return transaction;
}
use of javax.transaction.Transaction in project tomee by apache.
the class TimerData method registerTimerDataSynchronization.
private void registerTimerDataSynchronization() throws TimerStoreException {
if (synchronizationRegistered) {
return;
}
try {
final Transaction transaction = timerService.getTransactionManager().getTransaction();
final int status = transaction == null ? Status.STATUS_NO_TRANSACTION : transaction.getStatus();
if (transaction != null && status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK) {
transaction.registerSynchronization(new TimerDataSynchronization());
synchronizationRegistered = true;
return;
}
} catch (final Exception e) {
log.warning("Unable to register timer data transaction synchronization", e);
}
// there either wasn't a transaction or registration failed... call transactionComplete directly
transactionComplete(true);
}
Aggregations