Announcement Announcement Module
No announcement yet.
StaleConnectionException handling using Spring AOP Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • StaleConnectionException handling using Spring AOP


    I am trying to handle StaleConnectionException when using Spring DAO. I learnt that using Spring AOP, I can create a method interceptor and define retry logic in the interceptor. However, I also learned that after 'n' number of retries, if I throw the 'caught' exception in the interceptor, that exception is not caught by the calling program. To test out this scenario, I used a sample e.g (based on AOP examples on the Internet). Can someone provide some insight into this problem?

    ============ Interceptor code==========
    package interceptors;

    import java.util.Arrays;

    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;

    public class HijackMethod implements MethodInterceptor
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

    System.out.println("Method name : "
    + methodInvocation.getMethod().getName());
    System.out.println("Method arguments : "
    + Arrays.toString(methodInvocation.getArguments()));

    //same with MethodBeforeAdvice
    System.out.println("HijackMethod : Before method hijacked!");
    Object result = null;


    for (int i = 0; i < 3; i++) {
    try {
    result = methodInvocation.proceed();

    catch (IllegalArgumentException e) {
    // Escape out of # of retries has been reached
    System.out.println(" Got exception " + e.toString());
    if (i >= 3)
    throw e; //This is not caught by the invoker in
    //proceed to original method call
    //Object result = methodInvocation.proceed();

    //same with AfterReturningAdvice
    System.out.println("HijackMethod : After method hijacked!");

    return result;

    }catch(IllegalArgumentException e){
    //same with ThrowsAdvice
    System.out.println("HijackAroundMethod : Throw exception hijacked!");
    throw e;
    The bean which has the business logic and I will be intercepting printName method..

    package customers;

    public class CustomerService {
    private String name;
    private String url;

    public void setName(String name) { = name;

    public void setUrl(String url) {
    this.url = url;

    public void printName(){
    System.out.println("Customer name : " +;
    throw new IllegalArgumentException("Test"); //to simulate an exception thrown

    public void printURL(){
    System.out.println("Customer website : " + this.url);

    public void printThrowException(){
    throw new IllegalArgumentException("Test");


    Invoking program:

    package common;

    import org.springframework.context.ApplicationContext;
    import plicationContext;

    import customers.CustomerService;

    public class App {
    public static void main( String[] args )
    ApplicationContext appContext =
    new ClassPathXmlApplicationContext(new String[] {"classpath:/common/Spring-Customer.xml"});

    CustomerService cust =
    (CustomerService)appContext.getBean("customerServi ceProxy");


    }catch(Exception e){
    //Code never reaches here??
    System.out.println("printName: Caught exception" + e.getMessage());

    //}catch(Exception e){
    //System.out.println("In main Caught exception" + e.getMessage());

    Bean configuration file:
    - <beans xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    - <bean id="customerService" class="customers.CustomerService">
    <property name="name" value="Shashikant Rao" />
    <property name="url" value="" />
    <bean id="hijackAroundMethodBeanAdvice" class="interceptors.HijackMethod" />
    - <bean id="customerAdvisor" class=" thodPointcutAdvisor">
    <property name="mappedName" value="printName" />
    <property name="advice" ref="hijackAroundMethodBeanAdvice" />
    - <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="target" ref="customerService" />
    - <property name="interceptorNames">
    - <list>