/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.resource.adapter.jms.inflow;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ServerSession;
import javax.jms.Session;
import javax.jms.XAConnection;
import javax.jms.XASession;
import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkEvent;
import javax.resource.spi.work.WorkException;
import javax.resource.spi.work.WorkListener;
import javax.resource.spi.work.WorkManager;
import javax.transaction.Status;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;
import org.jboss.logging.Logger;
import org.jboss.resource.connectionmanager.xa.JcaXAResourceWrapperFactory;
/**
* A generic jms session pool.
*
* @author Adrian Brock
* @author 0)
{
log.trace("Setting transactionTimeout for JMSSessionPool to " + timeout);
tm.setTransactionTimeout(timeout);
}
tm.begin();
try
{
trans = tm.getTransaction();
if (trace)
log.trace(JmsServerSession.this + " using tx=" + trans);
if (xaSession != null)
{
XAResource res = JcaXAResourceWrapperFactory.getResourceWrapper(xaSession.getXAResource(), pool.getActivation().getActivationSpec().getIsSameRMOverrideValue());
if (!trans.enlistResource(res))
{
throw new JMSException("could not enlist resource");
}
if (trace)
log.trace(JmsServerSession.this + " XAResource '" + res + "' enlisted.");
}
}
catch (Throwable t)
{
try
{
tm.rollback();
}
catch (Throwable ignored)
{
log.trace(JmsServerSession.this + " ignored error rolling back after failed enlist", ignored);
}
throw t;
}
}
public void error()
{
// Mark for tollback TX via TM
try
{
if (trace)
{
log.trace(JmsServerSession.this + " using TM to mark TX for rollback tx=" + trans);
}
trans.setRollbackOnly();
}
catch (Throwable t)
{
log.error(JmsServerSession.this + " failed to set rollback only", t);
}
}
public void end()
{
try
{
// Use the TM to commit the Tx (assert the correct association)
Transaction currentTx = tm.getTransaction();
if (trans.equals(currentTx) == false)
throw new IllegalStateException("Wrong tx association: expected " + trans + " was " + currentTx);
// Marked rollback
if (trans.getStatus() == Status.STATUS_MARKED_ROLLBACK)
{
if (trace)
log.trace(JmsServerSession.this + " rolling back JMS transaction tx=" + trans);
// actually roll it back
tm.rollback();
// NO XASession? then manually rollback.
// This is not so good but
// it's the best we can do if we have no XASession.
if (xaSession == null && pool.getActivation().isDeliveryTransacted())
{
session.rollback();
}
}
else if (trans.getStatus() == Status.STATUS_ACTIVE)
{
// Commit tx
// This will happen if
// a) everything goes well
// b) app. exception was thrown
if (trace)
log.trace(JmsServerSession.this + " commiting the JMS transaction tx=" + trans);
tm.commit();
// NO XASession? then manually commit. This is not so good but
// it's the best we can do if we have no XASession.
if (xaSession == null && pool.getActivation().isDeliveryTransacted())
{
session.commit();
}
}
else
{
tm.suspend();
if (xaSession == null && pool.getActivation().isDeliveryTransacted())
{
session.rollback();
}
}
}
catch (Throwable t)
{
log.error(JmsServerSession.this + " failed to commit/rollback", t);
}
}
}
}