use of org.apache.openejb.SystemException in project tomee by apache.
the class JtaTransactionPolicy method beginTransaction.
protected Transaction beginTransaction() throws SystemException {
final Transaction transaction;
try {
transactionManager.begin();
transaction = transactionManager.getTransaction();
} catch (final Exception e) {
txLogger.error("The Transaction Manager has encountered an unexpected error condition while attempting to begin a new transaction: {0}", e.getMessage());
throw new SystemException(e);
}
if (transaction == null) {
throw new SystemException("Failed to begin a new transaction");
}
txLogger.debug("TX {0}: Started transaction {1}", transactionType, transaction);
return transaction;
}
use of org.apache.openejb.SystemException in project tomee by apache.
the class RAFPassivater method passivate.
@Override
public synchronized void passivate(final Map stateTable) throws SystemException {
try (final RandomAccessFile ras = new RandomAccessFile(JavaSecurityManagers.getSystemProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation" + fileID + ".ser", "rw")) {
fileID++;
final Iterator iterator = stateTable.keySet().iterator();
Pointer lastPointer = null;
while (iterator.hasNext()) {
final Object id = iterator.next();
final Object obj = stateTable.get(id);
final byte[] bytes = Serializer.serialize(obj);
final long filepointer = ras.getFilePointer();
if (lastPointer == null) {
lastPointer = new Pointer(fileID, filepointer, (int) filepointer);
} else {
lastPointer = new Pointer(fileID, filepointer, (int) (filepointer - lastPointer.filepointer));
}
masterTable.put(id, lastPointer);
ras.write(bytes);
}
} catch (final Exception e) {
throw new SystemException(e);
}
}
use of org.apache.openejb.SystemException in project tomee by apache.
the class SimplePassivater method activate.
@Override
public Object activate(final Object primaryKey) throws SystemException {
try {
final String filename = primaryKey.toString().replace(':', '=');
final File sessionFile = new File(sessionDirectory, filename);
if (sessionFile.exists()) {
logger.info("Activating from file " + sessionFile);
try (final InputStream source = IO.read(sessionFile);
final ObjectInputStream ois = new EjbObjectInputStream(source)) {
return ois.readObject();
} finally {
if (!sessionFile.delete()) {
sessionFile.deleteOnExit();
}
}
} else {
logger.info("Activation failed: file not found " + sessionFile);
return null;
}
} catch (final Exception t) {
logger.info("Activation failed ", t);
throw new SystemException(t);
}
}
use of org.apache.openejb.SystemException in project tomee by apache.
the class EntityInstanceManager method getPooledInstance.
protected EntityBean getPooledInstance(final ThreadContext callContext) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final Stack methodReadyPool = poolMap.get(beanContext.getDeploymentID());
if (methodReadyPool == null) {
throw new SystemException("Invalid deployment id " + beanContext.getDeploymentID() + " for this container");
}
EntityBean bean = (EntityBean) methodReadyPool.pop();
if (bean == null) {
try {
bean = (EntityBean) beanContext.getBeanClass().newInstance();
} catch (final Exception e) {
logger.error("Bean instantiation failed for class " + beanContext.getBeanClass(), e);
throw new SystemException(e);
}
final Operation currentOp = callContext.getCurrentOperation();
callContext.setCurrentOperation(Operation.SET_CONTEXT);
try {
/*
* setEntityContext executes in an unspecified transactional context. In this case we choose to
* allow it to have what every transaction context is current. Better then suspending it
* unnecessarily.
*
* We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
* logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
* we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
* to do.
*/
bean.setEntityContext(createEntityContext());
} catch (final Exception e) {
/*
* The EJB 1.1 specification does not specify how exceptions thrown by setEntityContext impact the
* transaction, if there is one. In this case we choose the least disruptive operation, throwing an
* application exception and NOT automatically marking the transaciton for rollback.
*/
logger.error("Bean callback method failed ", e);
throw new ApplicationException(e);
} finally {
callContext.setCurrentOperation(currentOp);
}
} else {
reusingBean(bean, callContext);
}
if (callContext.getCurrentOperation() == Operation.BUSINESS || callContext.getCurrentOperation() == Operation.REMOVE) {
/*
* When a bean is retrieved from the bean pool to service a client's business method request it must be
* notified that its about to enter service by invoking its ejbActivate( ) method. A bean instance
* does not have its ejbActivate() invoked when:
* 1. Its being retreived to service an ejbCreate()/ejbPostCreate().
* 2. Its being retrieved to service an ejbFind method.
* 3. Its being retrieved to service an ejbRemove() method.
* See section 9.1.4 of the EJB 1.1 specification.
*/
final Operation currentOp = callContext.getCurrentOperation();
callContext.setCurrentOperation(Operation.ACTIVATE);
try {
/*
In the event of an exception, OpenEJB is required to log the exception, evict the instance,
and mark the transaction for rollback. If there is a transaction to rollback, then the a
javax.transaction.TransactionRolledbackException must be throw to the client.
See EJB 1.1 specification, section 12.3.2
*/
bean.ejbActivate();
} catch (final Throwable e) {
logger.error("Encountered exception during call to ejbActivate()", e);
final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
if (txPolicy != null && txPolicy.isTransactionActive()) {
txPolicy.setRollbackOnly(e);
throw new ApplicationException(new TransactionRolledbackException("Reflection exception thrown while attempting to call ejbActivate() on the instance", e));
}
throw new ApplicationException(new RemoteException("Exception thrown while attempting to call ejbActivate() on the instance. Exception message = " + e.getMessage(), e));
} finally {
callContext.setCurrentOperation(currentOp);
}
}
return bean;
}
use of org.apache.openejb.SystemException in project tomee by apache.
the class ManagedContainer method obtainInstance.
private Instance obtainInstance(final Object primaryKey, final ThreadContext callContext) throws OpenEJBException {
if (primaryKey == null) {
throw new SystemException(new NullPointerException("Cannot obtain an instance of the stateful session bean with a null session id"));
}
final Transaction currentTransaction = getTransaction(callContext);
// Find the instance
Instance instance = checkedOutInstances.get(primaryKey);
if (instance == null) {
try {
instance = cache.checkOut(primaryKey);
} catch (final OpenEJBException e) {
throw e;
} catch (final Exception e) {
throw new SystemException("Unexpected load exception", e);
}
// Did we find the instance?
if (instance == null) {
throw new InvalidateReferenceException(new NoSuchObjectException("Not Found"));
}
// remember instance until it is returned to the cache
checkedOutInstances.put(primaryKey, instance);
}
synchronized (this) {
if (instance.isInUse()) {
// the bean is already being invoked; the only reentrant/concurrent operations allowed are Session synchronization callbacks
final Operation currentOperation = callContext.getCurrentOperation();
if (currentOperation != Operation.AFTER_COMPLETION && currentOperation != Operation.BEFORE_COMPLETION) {
throw new ApplicationException(new RemoteException("Concurrent calls not allowed."));
}
}
if (instance.getTransaction() != null) {
if (!instance.getTransaction().equals(currentTransaction) && !instance.getLock().tryLock()) {
throw new ApplicationException(new RemoteException("Instance is in a transaction and cannot be invoked outside that transaction. See EJB 3.0 Section 4.4.4"));
}
} else {
instance.setTransaction(currentTransaction);
}
// Mark the instance in use so we can detect reentrant calls
instance.setInUse(true);
return instance;
}
}
Aggregations