Search in sources :

Example 1 with BeanStateSynchronization

use of com.sun.ejb.spi.container.BeanStateSynchronization in project Payara by payara.

the class ReadOnlyBeanContainer method callEJBLoad.

protected void callEJBLoad(EntityBean ejb, EntityContextImpl entityCtx, boolean activeTx) throws Exception {
    // ReadOnlyContextImpl should always be used in conjunction with ReadOnlyBeanContainer
    assert entityCtx instanceof ReadOnlyContextImpl;
    ReadOnlyContextImpl context = (ReadOnlyContextImpl) entityCtx;
    ReadOnlyBeanInfo robInfo = context.getReadOnlyBeanInfo();
    // Grab the pk-specific lock before doing the refresh comparisons.
    // In the common-case, the lock will only be held for a very short
    // amount of time.  In the case where a pk-level refresh is needed,
    // we want to ensure that no concurrent refreshes for the same
    // pk can occur.
    int pkLevelSequenceNum = 0;
    long pkLastRefreshedAt = 0;
    synchronized (robInfo) {
        int currentBeanLevelSequenceNum = beanLevelSequenceNum;
        if (robInfo.beanLevelSequenceNum != currentBeanLevelSequenceNum) {
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, "REFRESH DUE TO BEAN-LEVEL UPDATE:" + " Bean-level sequence num = " + beanLevelSequenceNum + robInfo + " current time is " + new Date());
            }
            robInfo.refreshNeeded = true;
        } else if (RELATIVE_TIME_CHECK_MODE && (refreshPeriodInMillis > 0)) {
            // 0 implies no time based refresh
            if ((currentTimeInMillis - robInfo.lastRefreshedAt) > refreshPeriodInMillis) {
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, "REFRESH DUE TO STALE PK:" + " robInfo.lastRefreshedAt: " + robInfo.lastRefreshedAt + "; current (approx) time is " + currentTimeInMillis);
                }
                robInfo.refreshNeeded = true;
            }
        }
        // occurred or programmatic refresh of this PK.
        if (robInfo.refreshNeeded) {
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, " PK-LEVEL REFRESH : " + robInfo + " current time is " + new Date());
            }
            try {
                if (isContainerManagedPers) {
                    BeanStateSynchronization beanStateSynch = (BeanStateSynchronization) ejb;
                    beanStateSynch.ejb__refresh(entityCtx.getPrimaryKey());
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.log(Level.FINE, " PK-LEVEL REFRESH DONE :" + robInfo + " current time is " + new Date());
                    }
                } else {
                    if (ejb instanceof BeanStateSynchronization) {
                        // For debugging purposes, call into ejb__refresh
                        // if it's present on a BMP bean class
                        BeanStateSynchronization beanStateSynch = (BeanStateSynchronization) ejb;
                        beanStateSynch.ejb__refresh(entityCtx.getPrimaryKey());
                    }
                }
            } finally {
                // Always set refreshNeeded to false
                robInfo.refreshNeeded = false;
            }
            // Rob info only updated if no errors so far.
            updateAfterRefresh(robInfo);
        }
        pkLevelSequenceNum = robInfo.pkLevelSequenceNum;
        pkLastRefreshedAt = robInfo.lastRefreshedAt;
    }
    if ((entityCtx.isNewlyActivated()) || (context.getPKLevelSequenceNum() != pkLevelSequenceNum)) {
        // Now do instance-level refresh check to see if
        // ejbLoad is warranted.
        callLoad(ejb, context, pkLevelSequenceNum, pkLastRefreshedAt, currentTimeInMillis);
    }
}
Also used : BeanStateSynchronization(com.sun.ejb.spi.container.BeanStateSynchronization) Date(java.util.Date)

Example 2 with BeanStateSynchronization

use of com.sun.ejb.spi.container.BeanStateSynchronization in project Payara by payara.

the class ActiveTxCache method doFlush.

@Override
protected void doFlush(EjbInvocation inv) {
    if (!inv.invocationInfo.flushEnabled || inv.exception != null) {
        return;
    }
    if (!isContainerManagedPers) {
        // NEED TO INTERNATIONALIZE THIS WARNING MESSAGE
        _logger.log(Level.WARNING, "Cannot turn on flush-enabled-at-end-of-method for a bean with Bean Managed Persistence");
        return;
    }
    InvocationInfo invInfo = inv.invocationInfo;
    EntityContextImpl context = (EntityContextImpl) inv.context;
    Transaction tx = context.getTransaction();
    // could be committed or rolledback. In that case there is no point to call flush
    if (tx == null) {
        return;
    }
    // return w/o doing anything if the transaction is marked for rollback
    try {
        if (context.getRollbackOnly()) {
            return;
        }
    } catch (Throwable ex) {
        _logger.log(Level.WARNING, "Exception when calling getRollbackOnly", ex);
        return;
    }
    if (invInfo.isBusinessMethod) {
        try {
            // Store the state of all the beans that are part of this transaction
            storeAllBeansInTx(tx);
        } catch (Throwable ex) {
            inv.exception = ex;
            return;
        }
    }
    try {
        BeanStateSynchronization pmcontract = (BeanStateSynchronization) inv.ejb;
        pmcontract.ejb__flush();
    } catch (Throwable ex) {
        // check the type of the method and create the corresponding exception
        if (invInfo.startsWithCreate) {
            CreateException ejbEx = new CreateException();
            ejbEx.initCause(ex);
            inv.exception = ejbEx;
        } else if (invInfo.startsWithRemove) {
            RemoveException ejbEx = new RemoveException();
            ejbEx.initCause(ex);
            inv.exception = ejbEx;
        } else {
            EJBException ejbEx = new EJBException();
            ejbEx.initCause(ex);
            inv.exception = ejbEx;
        }
        return;
    }
}
Also used : InvocationInfo(com.sun.ejb.InvocationInfo) JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) Transaction(javax.transaction.Transaction) BeanStateSynchronization(com.sun.ejb.spi.container.BeanStateSynchronization)

Aggregations

BeanStateSynchronization (com.sun.ejb.spi.container.BeanStateSynchronization)2 InvocationInfo (com.sun.ejb.InvocationInfo)1 JavaEETransaction (com.sun.enterprise.transaction.api.JavaEETransaction)1 Date (java.util.Date)1 Transaction (javax.transaction.Transaction)1