• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System exception captured by an Interceptor

 
Leo Marzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The EJB 3.1 Spec (12.3.1) states that
If a system exception escapes the interceptor chain the bean instance and any associated interceptor instances are discarded.


Does it mean that if, for example, an stateless bean's method throws a RuntimeException, and it is caught by an @AroundInvoke method, the stateless bean instance is not discarded?

In code:




Thanks,
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean that if, for example, an stateless bean's method throws a RuntimeException, and it is caught by an @AroundInvoke method, the stateless bean instance is not discarded?

Yes it won't be discarded. The RuntimeException is suppressed and that is allowed.
 
Leo Marzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits!

I read a bit more the Interceptors 1.1 spec, and i found this:
"Around-invoke methods are allowed to catch and suppress exceptions and recover by calling proceed()."

which confirms what I thought
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a good exercise question.
According to JSR 318, p.386, table 15, it says if the bean throws a system exception in CMT with Required attribute, the stateless bean will be discarded.
Only singleton bean won't be discarded when a system exception is thrown from it.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used Ivan's example to demo this concept. And the stateless bean is not discard as it is not null.




[output]
LogInterceptor1 - entering method: method1
Info: LogInterceptor1 - throw a runtime exception and captured.
Info: LogInterceptor1- exiting method: method1
Info: ***StatlessBeanA is not discard.
Info: ***Exiting SingletonClientServlet
[/output]
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the information, if the exception thrown from the interceptor and it is captured by the interceptor, the stateless bean is not discarded. Assume the servlet is the same as above.



LogInterceptor1 - entering method: method1
Info: This is a demo, it does nothing.
Info: LogInterceptor1 - throw a runtime exception and captured.
Info: LogInterceptor1- exiting method: method1
Info: ***StatlessBeanA is not discard.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the information,
If the interceptor throws a RuntimeException that cannot be recovered, an EJB exception is thrown and I assume the stateless bean is discarded as the JSR 318 says so.



LogInterceptor1 - entering method: method1
Info: This is a demo, it does nothing.
Warning: A system exception occurred during an invocation on EJB StatelessSessionBeanA method public void com.ivan.scbcd6.StatelessSessionBeanA.method1()
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5194)
...
Caused by: java.lang.RuntimeException
at com.ivan.scbcd6.LogInterceptor1.log(LogInterceptor1.java:17)
...
Warning: StandardWrapperValve[SingletonClientServlet]: PWC1406: Servlet.service() for servlet SingletonClientServlet threw exception
javax.ejb.EJBException
...
Caused by: java.lang.RuntimeException
at com.ivan.scbcd6.LogInterceptor1.log(LogInterceptor1.java:17)
....

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Info: ***StatlessBeanA is not discard.

Be careful here: you are checking the reference


You should invoke a method to see if the Stateless Session Bean has been discarded.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Frits' suggestion.

I guess checks if the proxy of the stateless bean is null or not. But this does not check if the actual stateless bean has been discarded or not.


In the servlet client, I used after the interceptor throws the runtime exception.
The output is an EJBException as the statelessBeanA has been discarded by the container after the exception is thrown from the interceptor.


For the information:





LogInterceptor1 - entering method: method1
Info: This is a demo, it does nothing.
Warning: A system exception occurred during an invocation on EJB StatelessSessionBeanA method public void com.ivan.scbcd6.StatelessSessionBeanA.method1()
javax.ejb.EJBException
....
at com.ivan.scbcd6.__EJB31_Generated__StatelessSessionBeanA__Intf____Bean__.method1(Unknown Source)
at com.ivan.scbcd6.client.SingletonClientServlet.doGet(SingletonClientServlet.java:39)
...
Caused by: java.lang.RuntimeException
at com.ivan.scbcd6.LogInterceptor1.log(LogInterceptor1.java:17)
...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic