Indirect JDBC calls, wrapped

You can inspect the details of RemoteException to check whether it originates from a retryable exception. When a RemoteException is thrown to wrap another exception, the original exception is usually retained. All RemoteExceptions have an attribute detail, which is of type java.lang.Throwable. With this detail, we can trace back to the original exception to see whether its root cause is retryable. However, the detail is lost if a RemoteException flows from one JVM to another. A segment of code sample is shown in Example 12-5.

Example 12-5 Wrapping StaleConnectionException

public boolean retryableException(RemoteException rx) {
	RemoteException rex=rx;
	Throwable th=null;
	while (true) {
		th=rex.detail;
		try {
			rex= (RemoteExeption) th;
		} catch (ClassCastException cex) {
			return (
				th instanceof com.ibm.websphere.ce.cm.StaleConnectionException);
		}
	}
}

Then you can use the sample shown in Example 12-6.

Example 12-6 StaleConnectionException wrapped with indirect JDBC calls

public class WrappedRetrySessionEJBBean implements SessionBean {
	private javax.ejb.SessionContext mySessionCtx = null;
	private final static long serialVersionUID = 3206093459760846163L;

public void updateAccount() {
	javax.transaction.UserTransaction ut=
		getSessionContext().getUserTransaction();
	boolean retry=false;
	int maxRetries=5;
	int retryNum=0;
	do {
		try {
			ut.begin();
			aBean.update(id, deposit);
			//must catch SCE and rethrow it in remote exception
			ut.commit();
		} catch (RemoteException rex) {
			try {
				ut.rollback();
			} catch (Exception ex) {}
			retry=false;
			if (retryableException(rex) && retryNum<maxRetries) {
				retry=true;
				retryNum++;
			}
		} catch (Exception ex) {}
	} while (retry);
}

Example 12-7 RemoteException

...
boolean retry=false;
int retries=maxRetries; // set the maximum number of retries
do {
	try {
		aBean.update(xxx);
		break;
	} catch (RemoteException rex) {
		retry=false;
		if (retryableException(rex) && retries > 0) {
			retry=true;
			retries--;
		}
		try {
			Thread.sleep(1000); //sleep a while
		} catch (InterruptedException iex) {}
	}	
} while (retry);
...

  Prev | Home | Next

 

WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.

 

IBM is a trademark of the IBM Corporation in the United States, other countries, or both.