下面列出了怎么用javax.jms.TransactionInProgressException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Commit
*
* @throws JMSException Failed to close session.
*/
@Override
public void commit() throws JMSException {
if (cri.getType() == ActiveMQRAConnectionFactory.XA_CONNECTION || cri.getType() == ActiveMQRAConnectionFactory.XA_QUEUE_CONNECTION ||
cri.getType() == ActiveMQRAConnectionFactory.XA_TOPIC_CONNECTION) {
throw new TransactionInProgressException("XA connection");
}
lock();
try {
Session session = getSessionInternal();
if (cri.isTransacted() == false) {
throw new IllegalStateException("Session is not transacted");
}
if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
ActiveMQRALogger.LOGGER.trace("Commit session " + this);
}
session.commit();
} finally {
unlock();
}
}
/**
* Rollback
*
* @throws JMSException Failed to close session.
*/
@Override
public void rollback() throws JMSException {
if (cri.getType() == ActiveMQRAConnectionFactory.XA_CONNECTION || cri.getType() == ActiveMQRAConnectionFactory.XA_QUEUE_CONNECTION ||
cri.getType() == ActiveMQRAConnectionFactory.XA_TOPIC_CONNECTION) {
throw new TransactionInProgressException("XA connection");
}
lock();
try {
Session session = getSessionInternal();
if (cri.isTransacted() == false) {
throw new IllegalStateException("Session is not transacted");
}
if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
ActiveMQRALogger.LOGGER.trace("Rollback session " + this);
}
session.rollback();
} finally {
unlock();
}
}
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on SessionProxy interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("commit")) {
throw new TransactionInProgressException("Commit call not allowed within a managed transaction");
}
else if (method.getName().equals("rollback")) {
throw new TransactionInProgressException("Rollback call not allowed within a managed transaction");
}
else if (method.getName().equals("close")) {
// Handle close method: not to be closed within a transaction.
return null;
}
else if (method.getName().equals("getTargetSession")) {
// Handle getTargetSession method: return underlying Session.
return this.target;
}
// Invoke method on target Session.
try {
return method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on SessionProxy interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("commit")) {
throw new TransactionInProgressException("Commit call not allowed within a managed transaction");
}
else if (method.getName().equals("rollback")) {
throw new TransactionInProgressException("Rollback call not allowed within a managed transaction");
}
else if (method.getName().equals("close")) {
// Handle close method: not to be closed within a transaction.
return null;
}
else if (method.getName().equals("getTargetSession")) {
// Handle getTargetSession method: return underlying Session.
return this.target;
}
// Invoke method on target Session.
try {
return method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on SessionProxy interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of Connection proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("commit")) {
throw new TransactionInProgressException("Commit call not allowed within a managed transaction");
}
else if (method.getName().equals("rollback")) {
throw new TransactionInProgressException("Rollback call not allowed within a managed transaction");
}
else if (method.getName().equals("close")) {
// Handle close method: not to be closed within a transaction.
return null;
}
else if (method.getName().equals("getTargetSession")) {
// Handle getTargetSession method: return underlying Session.
return this.target;
}
// Invoke method on target Session.
try {
return method.invoke(this.target, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
/**
* Converts instances of sub-classes of {@link JMSException} into the corresponding sub-class of
* {@link JMSRuntimeException}.
*
* @param e
* @return
*/
public static JMSRuntimeException convertToRuntimeException(JMSException e) {
if (e instanceof javax.jms.IllegalStateException) {
return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidClientIDException) {
return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidDestinationException) {
return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidSelectorException) {
return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof JMSSecurityException) {
return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageFormatException) {
return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageNotWriteableException) {
return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof ResourceAllocationException) {
return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionInProgressException) {
return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionRolledBackException) {
return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
@Override
public void commit() throws JMSException {
if (!transacted) {
throw new IllegalStateException("Cannot commit a non-transacted session");
}
if (xa) {
throw new TransactionInProgressException("Cannot call commit on an XA session");
}
try {
session.commit();
} catch (ActiveMQException e) {
throw JMSExceptionHelper.convertFromActiveMQException(e);
}
}
@Override
public void rollback() throws JMSException {
if (!transacted) {
throw new IllegalStateException("Cannot rollback a non-transacted session");
}
if (xa) {
throw new TransactionInProgressException("Cannot call rollback on an XA session");
}
try {
session.rollback();
} catch (ActiveMQException e) {
throw JMSExceptionHelper.convertFromActiveMQException(e);
}
}
public static JMSRuntimeException toRuntimeException(final JMSException e) {
if (e instanceof javax.jms.IllegalStateException) {
return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidClientIDException) {
return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidDestinationException) {
return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidSelectorException) {
return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof JMSSecurityException) {
return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageFormatException) {
return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageNotWriteableException) {
return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof ResourceAllocationException) {
return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionInProgressException) {
return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionRolledBackException) {
return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
@Test(expected = TransactionInProgressRuntimeException.class)
public void testConvertsTransactionInProgressExceptionToTransactionInProgressRuntimeException() {
throw JMSExceptionSupport.createRuntimeException(new TransactionInProgressException("error"));
}
@Test(expected = TransactionInProgressRuntimeException.class)
public void testConvertsTransactionInProgressExceptionToTransactionInProgressRuntimeException() {
throw JmsExceptionSupport.createRuntimeException(new TransactionInProgressException("error"));
}