Examples of OwnableReentrantLock


Examples of org.jboss.as.ejb3.tx.OwnableReentrantLock

    @Override
    public Object processInvocation(final InterceptorContext context) throws Exception {
        final StatefulSessionComponent component = getComponent(context, StatefulSessionComponent.class);
        final StatefulSessionComponentInstance instance = getComponentInstance(context);

        final OwnableReentrantLock lock = instance.getLock();
        final Object threadLock = instance.getThreadLock();

        final TransactionSynchronizationRegistry transactionSynchronizationRegistry = component.getTransactionSynchronizationRegistry();
        final Object lockOwner = getLockOwner(transactionSynchronizationRegistry);
        lock.pushOwner(lockOwner);
        try {
            final AccessTimeoutDetails timeout = component.getAccessTimeout(context.getMethod());
            if (ROOT_LOGGER.isTraceEnabled()) {
                ROOT_LOGGER.trace("Trying to acquire lock: " + lock + " for stateful component instance: " + instance + " during invocation: " + context);
            }
            // we obtain a lock in this synchronization interceptor because the lock needs to be tied to the synchronization
            // so that it can released on the tx synchronization callbacks
            boolean acquired = lock.tryLock(timeout.getValue(), timeout.getTimeUnit());
            if (!acquired) {
                throw EjbLogger.ROOT_LOGGER.failToObtainLock(context, timeout.getValue(), timeout.getTimeUnit());
            }
            synchronized (threadLock) {
                if (ROOT_LOGGER.isTraceEnabled()) {
                    ROOT_LOGGER.trace("Acquired lock: " + lock + " for stateful component instance: " + instance + " during invocation: " + context);
                }

                Object currentTransactionKey = null;
                boolean wasTxSyncRegistered = false;
                try {
                    //we never register a sync for bean managed transactions
                    //the inner BMT interceptor is going to setup the correct transaction anyway
                    //so enrolling in an existing transaction is not correct
                    if(containerManagedTransactions) {
                        if (!instance.isSynchronizationRegistered()) {
                            // get the key to current transaction associated with this thread
                            currentTransactionKey = transactionSynchronizationRegistry.getTransactionKey();
                            final int status = transactionSynchronizationRegistry.getTransactionStatus();
                            // if this SFSB instance is already associated with a different transaction, then it's an error
                            // if the thread is currently associated with a tx, then register a tx synchronization
                            if (currentTransactionKey != null && status != Status.STATUS_COMMITTED && status != Status.STATUS_ROLLEDBACK) {
                                // register a tx synchronization for this SFSB instance
                                final Synchronization statefulSessionSync = new StatefulSessionSynchronization(instance, lockOwner);
                                transactionSynchronizationRegistry.registerInterposedSynchronization(statefulSessionSync);
                                wasTxSyncRegistered = true;
                                if (ROOT_LOGGER.isTraceEnabled()) {
                                    ROOT_LOGGER.trace("Registered tx synchronization: " + statefulSessionSync + " for tx: " + currentTransactionKey +
                                            " associated with stateful component instance: " + instance);
                                }
                                // invoke the afterBegin callback on the SFSB
                                instance.afterBegin();
                                instance.setSynchronizationRegistered(true);
                                context.putPrivateData(StatefulTransactionMarker.class, StatefulTransactionMarker.of(true));
                            }
                        } else {
                            context.putPrivateData(StatefulTransactionMarker.class, StatefulTransactionMarker.of(false));
                        }
                    }
                    // proceed with the invocation
                    return context.proceed();

                } finally {
                    // if the current call did *not* register a tx SessionSynchronization, then we have to explicitly mark the
                    // SFSB instance as "no longer in use". If it registered a tx SessionSynchronization, then releasing the lock is
                    // taken care off by a tx synchronization callbacks.
                    if (!wasTxSyncRegistered && !instance.isSynchronizationRegistered()) {
                        releaseInstance(instance);
                    } else if (!wasTxSyncRegistered) {
                        //if we don't release the lock here then it will be acquired multiple times
                        //and only released once
                        releaseLock(instance);
                        //we also call the cache release to decrease the usage count
                        if (!instance.isDiscarded()) {
                            instance.getComponent().getCache().release(instance);
                        }
                    }
                }
            }
        } finally {
            lock.popOwner();
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.