use of javax.transaction.TransactionalException in project Payara by payara.
the class TransactionalInterceptorRequired method transactional.
@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
_logger.log(FINE, CDI_JTA_REQUIRED);
if (isLifeCycleMethod(ctx)) {
return proceed(ctx);
}
setTransactionalTransactionOperationsManger(false);
try {
boolean isTransactionStarted = false;
if (getTransactionManager().getTransaction() == null) {
_logger.log(FINE, CDI_JTA_MBREQUIRED);
try {
getTransactionManager().begin();
TransactionManager transactionManager = getTransactionManager();
if (transactionManager instanceof TransactionManagerHelper) {
((TransactionManagerHelper) transactionManager).preInvokeTx(true);
}
} catch (Exception exception) {
_logger.log(FINE, CDI_JTA_MBREQUIREDBT, exception);
throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during begin " + exception, exception);
}
isTransactionStarted = true;
}
Object proceed = null;
try {
proceed = proceed(ctx);
} finally {
if (isTransactionStarted) {
try {
TransactionManager transactionManager = getTransactionManager();
if (transactionManager instanceof TransactionManagerHelper) {
((TransactionManagerHelper) transactionManager).postInvokeTx(false, true);
}
// Exception handling for proceed method call above can set TM/TRX as setRollbackOnly
if (getTransactionManager().getTransaction().getStatus() == STATUS_MARKED_ROLLBACK) {
getTransactionManager().rollback();
} else {
getTransactionManager().commit();
}
} catch (Exception exception) {
_logger.log(FINE, CDI_JTA_MBREQUIREDCT, exception);
throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during commit " + exception, exception);
}
}
}
return proceed;
} finally {
resetTransactionOperationsManager();
}
}
use of javax.transaction.TransactionalException in project tomee by apache.
the class InterceptorBase method intercept.
protected Object intercept(final InvocationContext ic) throws Exception {
TransactionPolicy policy = null;
final boolean forbidsUt = doesForbidUtUsage();
final RuntimeException oldEx;
final IllegalStateException illegalStateException;
if (forbidsUt) {
illegalStateException = ILLEGAL_STATE_EXCEPTION;
oldEx = CoreUserTransaction.error(illegalStateException);
} else {
illegalStateException = null;
oldEx = null;
}
try {
policy = getPolicy();
final Object proceed = ic.proceed();
// force commit there to ensure we can catch synchro exceptions
policy.commit();
return proceed;
} catch (final Exception e) {
if (illegalStateException == e) {
throw e;
}
Exception error = unwrap(e);
if (error != null && (!HANDLE_EXCEPTION_ONLY_FOR_CLIENT || policy.isNewTransaction())) {
final Method method = ic.getMethod();
if (rollback == null) {
synchronized (this) {
if (rollback == null) {
rollback = new ConcurrentHashMap<>();
}
}
}
Boolean doRollback = rollback.get(method);
if (doRollback != null) {
if (doRollback && policy != null && policy.isTransactionActive()) {
policy.setRollbackOnly();
}
} else {
// computed lazily but we could cache it later for sure if that's really a normal case
final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(method.getDeclaringClass());
Transactional tx = null;
for (final AnnotatedMethod<?> m : annotatedType.getMethods()) {
if (method.equals(m.getJavaMember())) {
tx = m.getAnnotation(Transactional.class);
break;
}
}
if (tx == null) {
tx = annotatedType.getAnnotation(Transactional.class);
}
if (tx != null) {
doRollback = new ExceptionPriotiryRules(tx.rollbackOn(), tx.dontRollbackOn()).accept(error, method.getExceptionTypes());
rollback.putIfAbsent(method, doRollback);
if (doRollback && policy != null && policy.isTransactionActive()) {
policy.setRollbackOnly();
}
}
}
}
if (policy != null) {
try {
policy.commit();
} catch (final Exception ex) {
// no-op: swallow to keep the right exception
final Logger logger = Logger.getLogger(getClass().getName());
if (logger.isLoggable(Level.FINE)) {
logger.fine("Swallowing: " + ex.getMessage());
}
}
}
if (error == null || TransactionRequiredException.class.isInstance(error)) {
throw new TransactionalException(e.getMessage(), error);
}
throw error;
} finally {
if (forbidsUt) {
CoreUserTransaction.resetError(oldEx);
}
}
}
use of javax.transaction.TransactionalException in project Payara by payara.
the class TransactionalInterceptorNever method transactional.
@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
_logger.log(FINE, CDI_JTA_NEVER);
if (isLifeCycleMethod(ctx)) {
return proceed(ctx);
}
setTransactionalTransactionOperationsManger(true);
try {
if (getTransactionManager().getTransaction() != null) {
throw new TransactionalException("InvalidTransactionException thrown from TxType.NEVER transactional interceptor.", new InvalidTransactionException("Managed bean with Transactional annotation and TxType of NEVER " + "called inside a transaction context"));
}
return proceed(ctx);
} finally {
resetTransactionOperationsManager();
}
}
use of javax.transaction.TransactionalException in project Payara by payara.
the class TransactionalInterceptorRequiresNew method transactional.
@AroundInvoke
public Object transactional(InvocationContext ctx) throws Exception {
_logger.log(FINE, CDI_JTA_REQNEW);
if (isLifeCycleMethod(ctx)) {
return proceed(ctx);
}
setTransactionalTransactionOperationsManger(false);
try {
Transaction suspendedTransaction = null;
if (getTransactionManager().getTransaction() != null) {
_logger.log(FINE, CDI_JTA_MBREQNEW);
suspendedTransaction = getTransactionManager().suspend();
// todo catch, wrap in new transactional exception and throw
}
try {
getTransactionManager().begin();
TransactionManager tm = getTransactionManager();
if (tm instanceof TransactionManagerHelper) {
((TransactionManagerHelper) tm).preInvokeTx(true);
}
} catch (Exception exception) {
_logger.log(FINE, CDI_JTA_MBREQNEWBT, exception);
throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRES_NEW " + "encountered exception during begin " + exception, exception);
}
Object proceed = null;
try {
proceed = proceed(ctx);
} finally {
try {
TransactionManager tm = getTransactionManager();
if (tm instanceof TransactionManagerHelper) {
((TransactionManagerHelper) tm).postInvokeTx(false, true);
}
// Exception handling for proceed method call above can set TM/TRX as setRollbackOnly
if (getTransactionManager().getTransaction().getStatus() == STATUS_MARKED_ROLLBACK) {
getTransactionManager().rollback();
} else {
getTransactionManager().commit();
}
} catch (Exception exception) {
_logger.log(FINE, CDI_JTA_MBREQNEWCT, exception);
throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRES_NEW " + "encountered exception during commit " + exception, exception);
}
if (suspendedTransaction != null) {
try {
getTransactionManager().resume(suspendedTransaction);
} catch (Exception exception) {
throw new TransactionalException("Managed bean with Transactional annotation and TxType of REQUIRED " + "encountered exception during resume " + exception, exception);
}
}
}
return proceed;
} finally {
resetTransactionOperationsManager();
}
}
Aggregations