use of javax.transaction.SystemException in project tomee by apache.
the class EjbTimerServiceImpl method ejbTimeout.
/**
* This method calls the ejbTimeout method and starts a transaction if the timeout is transacted.
* <p/>
* This method will retry failed ejbTimeout calls until retryAttempts is exceeded.
*
* @param timerData the timer to call.
*/
@SuppressWarnings("ReturnInsideFinallyBlock")
public void ejbTimeout(final TimerData timerData) {
final Thread thread = Thread.currentThread();
// container loader
final ClassLoader loader = thread.getContextClassLoader();
try {
Timer timer = getTimer(timerData.getId());
// quartz can be backed by some advanced config (jdbc for instance)
if (timer == null && timerStore instanceof MemoryTimerStore && timerData.getTimer() != null) {
try {
timerStore.addTimerData(timerData);
// TODO: replace memoryjobstore by the db one?
timer = timerData.getTimer();
} catch (final TimerStoreException e) {
// shouldn't occur
}
// return;
}
for (int tries = 0; tries < 1 + retryAttempts; tries++) {
boolean retry = false;
// if transacted, begin the transaction
if (transacted) {
try {
transactionManager.begin();
} catch (final Exception e) {
log.warning("Exception occured while starting container transaction", e);
return;
}
}
// call the timeout method
try {
final RpcContainer container = (RpcContainer) deployment.getContainer();
if (container == null) {
return;
}
final Method ejbTimeout = timerData.getTimeoutMethod();
if (ejbTimeout == null) {
return;
}
// if app registered Synchronization we need it for commit()/rollback()
// so forcing it and not relying on container for it
thread.setContextClassLoader(deployment.getClassLoader() != null ? deployment.getClassLoader() : loader);
SetAccessible.on(ejbTimeout);
container.invoke(deployment.getDeploymentID(), InterfaceType.TIMEOUT, ejbTimeout.getDeclaringClass(), ejbTimeout, new Object[] { timer }, timerData.getPrimaryKey());
} catch (final RuntimeException e) {
retry = true;
// exception from a timer does not necessairly mean failure
log.warning("RuntimeException from ejbTimeout on " + deployment.getDeploymentID(), e);
try {
transactionManager.setRollbackOnly();
} catch (final SystemException e1) {
log.warning("Exception occured while setting RollbackOnly for container transaction", e1);
}
} catch (final OpenEJBException e) {
retry = true;
if (ApplicationException.class.isInstance(e)) {
// we don't want to pollute logs
log.debug("Exception from ejbTimeout on " + deployment.getDeploymentID(), e);
} else {
log.warning("Exception from ejbTimeout on " + deployment.getDeploymentID(), e);
}
if (transacted) {
try {
transactionManager.setRollbackOnly();
} catch (final SystemException e1) {
log.warning("Exception occured while setting RollbackOnly for container transaction", e1);
}
}
} finally {
try {
if (!transacted) {
if (!retry) {
return;
}
} else if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
transactionManager.commit();
return;
} else {
// tx was marked rollback, so roll it back and retry.
transactionManager.rollback();
}
} catch (final Exception e) {
log.warning("Exception occured while completing container transaction", e);
}
}
}
log.warning("Failed to execute ejbTimeout on " + timerData.getDeploymentId() + " successfully within " + retryAttempts + " attempts");
} catch (final RuntimeException e) {
log.warning("RuntimeException occured while calling ejbTimeout", e);
throw e;
} catch (final Error e) {
log.warning("Error occured while calling ejbTimeout", e);
throw e;
} finally {
thread.setContextClassLoader(loader);
//TODO shall we do all this via Quartz listener ???
if (timerData.getType() == TimerType.SingleAction) {
timerStore.removeTimer(timerData.getId());
timerData.setExpired(true);
} else if (timerData.getType() == TimerType.Calendar && timerData.getNextTimeout() == null) {
timerStore.removeTimer(timerData.getId());
timerData.setExpired(true);
} else {
timerStore.updateIntervalTimer(timerData);
}
}
}
use of javax.transaction.SystemException in project hibernate-orm by hibernate.
the class SchemaToolTransactionHandlingTest method testDropCreateDropInExistingJtaTransaction.
// for each case we want to run these tool delegates in a matrix of:
// 1) JTA versus JDBC transaction handling
// 2) existing transaction versus not
//
// cases:
// 1) create-drop
// 2) update
// 3) validate
//
// so:
// 1) create-drop
// 1.1) JTA transaction handling
// 1.1.1) inside an existing transaction
// 1.1.2) outside any transaction
// 1.1) JDBC transaction handling
// - there really cannot be an "existing transaction" case...
@Test
public void testDropCreateDropInExistingJtaTransaction() {
// start a JTA transaction...
try {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
} catch (Exception e) {
throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
}
// hold reference to Transaction object
final Transaction jtaTransaction;
try {
jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
} catch (SystemException e) {
throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
}
final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
// perform the test...
try {
final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
final SchemaDropper schemaDropper = smt.getSchemaDropper(Collections.emptyMap());
final SchemaCreator schemaCreator = smt.getSchemaCreator(Collections.emptyMap());
final Metadata mappings = buildMappings(registry);
try {
try {
schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (CommandAcceptanceException e) {
//ignore may happen if sql drop does not support if exist
}
schemaCreator.doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} finally {
try {
schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (Exception ignore) {
// ignore
}
}
} finally {
try {
jtaTransaction.commit();
((StandardServiceRegistryImpl) registry).destroy();
} catch (Exception e) {
// not much we can do...
}
}
}
use of javax.transaction.SystemException in project hibernate-orm by hibernate.
the class SchemaToolTransactionHandlingTest method testValidateInExistingJtaTransaction.
@Test
public void testValidateInExistingJtaTransaction() {
// start a JTA transaction...
try {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
} catch (Exception e) {
throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
}
// hold reference to Transaction object
final Transaction jtaTransaction;
try {
jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
} catch (SystemException e) {
throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
}
final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
// perform the test...
try {
final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
final Metadata mappings = buildMappings(registry);
// first make the schema exist...
try {
smt.getSchemaCreator(Collections.emptyMap()).doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (Exception e) {
throw new RuntimeException("Unable to create schema to validation tests", e);
}
try {
smt.getSchemaValidator(Collections.emptyMap()).doValidation(mappings, ExecutionOptionsTestImpl.INSTANCE);
} finally {
try {
smt.getSchemaDropper(Collections.emptyMap()).doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (Exception ignore) {
// ignore
}
}
} finally {
try {
jtaTransaction.commit();
((StandardServiceRegistryImpl) registry).destroy();
} catch (Exception e) {
// not much we can do...
}
}
}
use of javax.transaction.SystemException in project hibernate-orm by hibernate.
the class XaTransactionImpl method rollback.
public void rollback() throws IllegalStateException, SystemException {
status = Status.STATUS_ROLLING_BACK;
runXaResourceRollback();
status = Status.STATUS_ROLLEDBACK;
if (connection != null) {
try {
connection.rollback();
connection.close();
} catch (SQLException sqle) {
status = Status.STATUS_UNKNOWN;
throw new SystemException();
}
}
if (synchronizations != null) {
for (int i = 0; i < synchronizations.size(); i++) {
Synchronization s = (Synchronization) synchronizations.get(i);
if (s != null)
s.afterCompletion(status);
}
}
// status = Status.STATUS_NO_TRANSACTION;
jtaTransactionManager.endCurrent(this);
}
use of javax.transaction.SystemException in project hibernate-orm by hibernate.
the class TxUtil method markRollbackOnly.
public static void markRollbackOnly(boolean useJta, Session s) {
if (useJta) {
JtaPlatform jtaPlatform = s.getSessionFactory().getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class);
TransactionManager tm = jtaPlatform.retrieveTransactionManager();
try {
tm.setRollbackOnly();
} catch (SystemException e) {
throw new RuntimeException(e);
}
} else {
s.getTransaction().markRollbackOnly();
}
}
Aggregations