public void enlist(Transaction transaction, LocalTransaction localTransaction) { if (!localTransaction.isEnlisted()) { SynchronizationAdapter sync = new SynchronizationAdapter( localTransaction, txCoordinator, commandsFactory, rpcManager, this, clusteringLogic, configuration); if (transactionSynchronizationRegistry != null) { try { transactionSynchronizationRegistry.registerInterposedSynchronization(sync); } catch (Exception e) { log.failedSynchronizationRegistration(e); throw new CacheException(e); } } else { try { transaction.registerSynchronization(sync); } catch (Exception e) { log.failedSynchronizationRegistration(e); throw new CacheException(e); } } ((SyncLocalTransaction) localTransaction).setEnlisted(true); } }
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<EntityBean>(); 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 } }
/** * Looks for a current Spring managed transaction and wraps/returns that as a Ebean transaction. * * <p>Returns null if there is no current spring transaction (lazy loading outside a spring txn * etc). */ public Object getCurrentTransaction() { TransactionSynchronizationRegistry syncRegistry = getSyncRegistry(); SpiTransaction t = (SpiTransaction) syncRegistry.getResource(EBEAN_TXN_RESOURCE); if (t != null) { // we have already seen this transaction return t; } // check current Ebean transaction SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName); if (currentEbeanTransaction != null) { // NOT expecting this so log WARNING String msg = "JTA Transaction - no current txn BUT using current Ebean one " + currentEbeanTransaction.getId(); logger.warn(msg); return currentEbeanTransaction; } UserTransaction ut = getUserTransaction(); if (ut == null) { // no current JTA transaction if (logger.isDebugEnabled()) { logger.debug("JTA Transaction - no current txn"); } return null; } // This is a transaction that Ebean has not seen before. // "wrap" it in a Ebean specific JtaTransaction String txnId = String.valueOf(System.currentTimeMillis()); JtaTransaction newTrans = new JtaTransaction(txnId, true, ut, dataSource, transactionManager); // create and register transaction listener JtaTxnListener txnListener = createJtaTxnListener(newTrans); syncRegistry.putResource(EBEAN_TXN_RESOURCE, newTrans); syncRegistry.registerInterposedSynchronization(txnListener); // also put in Ebean ThreadLocal DefaultTransactionThreadLocal.set(serverName, newTrans); return newTrans; }
private Map<Contextual<?>, BeanInstanceBag<?>> findMap() { final Object resource; try { // we can't call registry.getResource(KEY) in afterCompletion resource = context.geronimoTxMgr ? TransactionImpl.class .cast(context.transactionManager.getTransaction()) .getResource(KEY) : registry.getResource(KEY); } catch (final SystemException e) { throw new IllegalStateException(e); } if (resource == null) { final Map<Contextual<?>, BeanInstanceBag<?>> map = new HashMap<>(); registry.putResource(KEY, map); registry.registerInterposedSynchronization(context); return map; } return Map.class.cast(resource); }