use of javax.jms.XASession in project narayana by jbosstm.
the class ConnectionProxy method createAndRegisterSession.
/**
* Create a proxied XA session and enlist its XA resource to the transaction.
* <p>
* If session's XA resource cannot be enlisted to the transaction, session is closed.
*
* @return XA session wrapped with {@link SessionProxy}.
* @throws JMSException if failure occurred creating XA session or registering its XA resource.
*/
private Session createAndRegisterSession() throws JMSException {
XASession xaSession = xaConnection.createXASession();
Session session = new SessionProxy(xaSession, transactionHelper);
try {
transactionHelper.registerXAResource(xaSession.getXAResource());
} catch (JMSException e) {
xaSession.close();
throw e;
}
if (jtaLogger.logger.isTraceEnabled()) {
jtaLogger.logger.trace("Created new proxied session: " + session);
}
return session;
}
use of javax.jms.XASession in project quickstart by wildfly.
the class XAService method notifyUpdate.
private void notifyUpdate(Queue queue, String msg) throws Exception {
XAConnection connection = null;
try {
connection = xaConnectionFactory.createXAConnection();
XASession session = connection.createXASession();
MessageProducer messageProducer = session.createProducer(queue);
connection.start();
TextMessage message = session.createTextMessage();
message.setText(msg);
messageProducer.send(message);
messageProducer.close();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
LOGGER.info("Error closing JMS connection: " + e.getMessage());
}
}
}
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class ActiveMQXAConnectionFactoryTest method testIsSameRM.
public void testIsSameRM() throws URISyntaxException, JMSException, XAException {
XAConnection connection1 = null;
XAConnection connection2 = null;
try {
ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
connection1 = (XAConnection) cf1.createConnection();
XASession session1 = connection1.createXASession();
XAResource resource1 = session1.getXAResource();
ActiveMQXAConnectionFactory cf2 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
connection2 = (XAConnection) cf2.createConnection();
XASession session2 = connection2.createXASession();
XAResource resource2 = session2.getXAResource();
assertTrue(resource1.isSameRM(resource2));
session1.close();
session2.close();
} finally {
if (connection1 != null) {
try {
connection1.close();
} catch (Exception e) {
// ignore
}
}
if (connection2 != null) {
try {
connection2.close();
} catch (Exception e) {
// ignore
}
}
}
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class ActiveMQXAConnectionFactoryTest method testRollbackXaErrorCode.
public void testRollbackXaErrorCode() throws Exception {
String brokerName = "rollbackErrorCode";
BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:0)/" + brokerName));
broker.start();
broker.waitUntilStarted();
ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory(broker.getTransportConnectors().get(0).getConnectUri());
XAConnection connection = (XAConnection) cf.createConnection();
connection.start();
XASession session = connection.createXASession();
XAResource resource = session.getXAResource();
Xid tid = createXid();
try {
resource.rollback(tid);
fail("Expected xa exception on no tx");
} catch (XAException expected) {
LOG.info("got expected xa", expected);
assertEquals("no tx", XAException.XAER_NOTA, expected.errorCode);
}
connection.close();
broker.stop();
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class ActiveMQXAConnectionFactoryTest method testConsumerCloseTransactionalSendReceive.
public void testConsumerCloseTransactionalSendReceive() throws Exception {
ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
XAConnection connection1 = (XAConnection) cf1.createConnection();
connection1.start();
XASession session = connection1.createXASession();
XAResource resource = session.getXAResource();
Destination dest = new ActiveMQQueue(getName());
// publish a message
Xid tid = createXid();
resource.start(tid, XAResource.TMNOFLAGS);
MessageProducer producer = session.createProducer(dest);
ActiveMQTextMessage message = new ActiveMQTextMessage();
message.setText(getName());
producer.send(message);
producer.close();
resource.end(tid, XAResource.TMSUCCESS);
resource.commit(tid, true);
session.close();
session = connection1.createXASession();
MessageConsumer consumer = session.createConsumer(dest);
tid = createXid();
resource = session.getXAResource();
resource.start(tid, XAResource.TMNOFLAGS);
TextMessage receivedMessage = (TextMessage) consumer.receive(1000);
consumer.close();
assertNotNull(receivedMessage);
assertEquals(getName(), receivedMessage.getText());
resource.end(tid, XAResource.TMSUCCESS);
resource.commit(tid, true);
session = connection1.createXASession();
consumer = session.createConsumer(dest);
tid = createXid();
resource = session.getXAResource();
resource.start(tid, XAResource.TMNOFLAGS);
assertNull(consumer.receive(1000));
resource.end(tid, XAResource.TMSUCCESS);
resource.commit(tid, true);
}
Aggregations