use of javax.transaction.Synchronization in project Payara by payara.
the class EJBTimerService method cancelTimerSynchronization.
protected void cancelTimerSynchronization(EJBContextImpl context_, TimerPrimaryKey timerId, long containerId, String ownerId, boolean persistent) throws Exception {
// is owned by the current server instance.
if (context_ == null || timerOwnedByThisServer(ownerId)) {
// Cancel existing timer task. Save time at which task would
// have executed in case cancellation is rolled back. The
// nextTimeout can be null if the timer is currently being
// delivered.
Date nextTimeout = cancelTask(timerId);
ContainerSynchronization containerSynch = getContainerSynch(context_, timerId.getTimerId(), persistent);
if (containerSynch == null) {
// null ContainerSynchronization for persistent timer would've
// caused exception in #getContainerSynch(). For non-persistent
// timer remove it right away
expungeTimer(timerId);
ejbContainerUtil.getContainer(containerId).incrementRemovedTimedObject();
} else {
Synchronization timerSynch = containerSynch.getTimerSynchronization(timerId);
if (timerSynch != null) {
// This timer was created and cancelled within the
// same transaction. No tx synchronization actions
// are needed, since whether tx commits or rolls back,
// timer will not exist.
containerSynch.removeTimerSynchronization(timerId);
expungeTimer(timerId);
} else {
// Set tx synchronization action to handle timer cancellation.
timerSynch = new TimerSynch(timerId, STATE_CANCELLED, nextTimeout, ejbContainerUtil.getContainer(containerId), this);
containerSynch.addTimerSynchronization(timerId, timerSynch);
}
}
}
}
use of javax.transaction.Synchronization in project Payara by payara.
the class ContainerSynchronization method beforeCompletion.
public void beforeCompletion() {
// first call beforeCompletion for each bean instance
for (int i = 0; i < beans.size(); i++) {
EJBContextImpl context = (EJBContextImpl) beans.get(i);
BaseContainer container = (BaseContainer) context.getContainer();
try {
if (container != null) {
boolean allowTxCompletion = true;
if (container.isUndeployed()) {
if (context instanceof SessionContextImpl) {
allowTxCompletion = ((SessionContextImpl) context).getInLifeCycleCallback();
} else {
allowTxCompletion = false;
_logger.log(Level.WARNING, "Marking Tx for rollback " + " because container for " + container + " is undeployed");
}
}
if (!allowTxCompletion) {
try {
tx.setRollbackOnly();
} catch (SystemException sysEx) {
_logger.log(Level.FINE, "Error while trying to " + "mark for rollback", sysEx);
}
} else {
container.beforeCompletion(context);
}
} else {
// Might be null if bean was removed. Just skip it.
_logger.log(Level.FINE, "context with empty container in " + " ContainerSynchronization.beforeCompletion");
}
} catch (RuntimeException ex) {
logAndRollbackTransaction(ex);
throw ex;
} catch (Exception ex) {
logAndRollbackTransaction(ex);
// no need to call remaining beforeCompletions
throw new EJBException("Error during beforeCompletion.", ex);
}
}
// now call beforeCompletion for all pmSyncs
for (int i = 0; i < pmSyncs.size(); i++) {
Synchronization sync = (Synchronization) pmSyncs.elementAt(i);
try {
sync.beforeCompletion();
} catch (RuntimeException ex) {
logAndRollbackTransaction(ex);
throw ex;
} catch (Exception ex) {
logAndRollbackTransaction(ex);
// no need to call remaining beforeCompletions
throw new EJBException("Error during beforeCompletion.", ex);
}
}
}
use of javax.transaction.Synchronization in project tomee by apache.
the class CmpContainer method ejbLoad.
private void ejbLoad(final EntityBean entityBean) {
if (entityBean == null) {
throw new NullPointerException("entityBean is null");
}
final ThreadContext callContext = createThreadContext(entityBean);
callContext.setCurrentOperation(Operation.LOAD);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
try {
entityBean.ejbLoad();
} catch (final RemoteException e) {
throw new EJBException(e);
} finally {
ThreadContext.exit(oldCallContext);
}
// if we call load we must call store
try {
// noinspection unchecked
Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE);
if (registeredEntities == null) {
registeredEntities = new LinkedHashSet<>();
synchronizationRegistry.putResource(ENTITIES_TO_STORE, registeredEntities);
synchronizationRegistry.registerInterposedSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// noinspection unchecked
final Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE);
if (registeredEntities == null) {
return;
}
for (final EntityBean entityBean : registeredEntities) {
ejbStore(entityBean);
}
}
@Override
public void afterCompletion(final int i) {
}
});
}
registeredEntities.add(entityBean);
} catch (final Exception e) {
// no-op
}
}
use of javax.transaction.Synchronization in project tomee by apache.
the class BaseEjbProxyHandler method getLiveHandleRegistry.
public ConcurrentMap getLiveHandleRegistry() {
final BeanContext beanContext = getBeanContext();
final ThreadContext tc = ThreadContext.getThreadContext();
if (tc != null && tc.getBeanContext() != beanContext && /* parent bean */
tc.getCurrentOperation() == Operation.BUSINESS) {
ProxyRegistry registry = tc.get(ProxyRegistry.class);
if (registry == null) {
registry = new ProxyRegistry();
tc.set(ProxyRegistry.class, registry);
}
return registry.liveHandleRegistry;
} else {
// use the tx if there
final SystemInstance systemInstance = SystemInstance.get();
final TransactionManager txMgr = systemInstance.getComponent(TransactionManager.class);
try {
final Transaction tx = txMgr.getTransaction();
if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
final TransactionSynchronizationRegistry registry = systemInstance.getComponent(TransactionSynchronizationRegistry.class);
final String resourceKey = ProxyRegistry.class.getName();
ConcurrentMap map = ConcurrentMap.class.cast(registry.getResource(resourceKey));
if (map == null) {
map = new ConcurrentHashMap();
registry.putResource(resourceKey, map);
try {
final ConcurrentMap tmp = map;
tx.registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// no-op
}
@Override
public void afterCompletion(final int status) {
tmp.clear();
}
});
} catch (final RollbackException e) {
// not really possible since we check the status
// let it go to default
}
}
return map;
}
} catch (final SystemException e) {
// let it go to default
}
// back to default but it doesnt release the memory
ProxyRegistry proxyRegistry = beanContext.get(ProxyRegistry.class);
if (proxyRegistry == null) {
proxyRegistry = new ProxyRegistry();
beanContext.set(ProxyRegistry.class, proxyRegistry);
}
return proxyRegistry.liveHandleRegistry;
}
}
use of javax.transaction.Synchronization in project tomee by apache.
the class TransactionalTest method classLevel.
@Test
public void classLevel() throws Exception {
for (int i = 0; i < 2; i++) {
final AtomicInteger status = new AtomicInteger();
try {
bean.classLevel(new Runnable() {
@Override
public void run() {
try {
OpenEJB.getTransactionManager().getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// no-op
}
@Override
public void afterCompletion(int state) {
status.set(state);
}
});
} catch (final RollbackException | SystemException e) {
fail();
}
}
});
fail();
} catch (final AnCheckedException e) {
// no-op
}
assertEquals(Status.STATUS_COMMITTED, status.get());
}
}
Aggregations