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

  • StaleConnectionException handling using Spring AOP

    Hi,

    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
    {
    @Override
    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;

    try{

    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 App.java??
    //Thread.currentThread().sleep(5);
    }
    }
    //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) {
    this.name = name;
    }

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

    public void printName(){
    System.out.println("Customer name : " + this.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 org.springframework.context.support.ClassPathXmlAp 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");

    System.out.println("*************************");

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


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

    ====================
    Bean configuration file:
    - <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    - <bean id="customerService" class="customers.CustomerService">
    <property name="name" value="Shashikant Rao" />
    <property name="url" value="http://www.google.com" />
    </bean>
    <bean id="hijackAroundMethodBeanAdvice" class="interceptors.HijackMethod" />
    - <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMe thodPointcutAdvisor">
    <property name="mappedName" value="printName" />
    <property name="advice" ref="hijackAroundMethodBeanAdvice" />
    </bean>
    - <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="target" ref="customerService" />
    - <property name="interceptorNames">
    - <list>
    <value>customerAdvisor</value>
    </list>
    </property>
    </bean>
    </beans>
    ========================
Working...
X