Direct JDBC calls, global transaction

This, also, can group several statements in a unit of work, as shown in Example 12-3. A javax.transaction.UserTransaction object can be retrieved from JNDI naming or from an EJBContext object (for example, getSessionContext().getUserTransaction() in session beans).

Example 12-3 StaleConnectionException with direct JDBC calls and global transaction

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

public void updateAccount(String id, float amount) {
	UserTransaction ut=getSessionContext().getUserTransaction();
	Statement stmt=null;
	ResultSet rs=null;
	Connection conn=null;
	int maxRetries=5;
	int retryNum=0;
	boolean retry=false;
	do {
		try {
			ut.begin();
			conn=ds.getConnection();
			stmt=conn.createStatement();
			stmt.executeUpdate(sqlstring1);
			stmt.executeUpdate(sqlstring2);
			ut.commit();
		} catch (com.ibm.websphere.ce.cm.StaleConnectionException scex){
			ut.rollback();
			if (retryNum < maxRetries) {
				retry=true;
				retryNum++;
			}
			retry=false;
		} catch (SQLException sqlex) {
		} catch (Exception ex) {}
	} 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.