Handling exceptions in EJB - ejb

I've tried to handle exceptions in EJB tier with a CMT. I have some crud operations that can have constraint rules violated. When some violation ocurrs, the transaction is rolled back but I can't catch the exception (I understood that the commit happens at the end of the method. Am I right ?). The question is: Should I think the EJB tier as a black box and handle the exceptions in the EJB client ?

Related

Tracking Event Processor - Retry Execption originating from Event Handlers

I am trying to configure an ErrorHandler for a TrackingEventProcessor so that any exception in my #EventHandler annotated method will be retried.
Currently using Axon Framework 4 and looking how to achieve this.
I'd recommend to set the PropagatingErrorHandler as the ListenerInvocationErrorHandler in this case.
Know that there are two error handling levels within a given Event Processor, namely:
The ListenerInvocationErrorHandler, catching the exceptions thrown from within your #EventHandler annotated methods. This defaults to a LoggingErrorHandler instance, which logs the exception.
The ErrorHandler, catching the transaction exceptions of the given EventProcessor. This defaults to a PropagatingErrorHandler, which rethrows the exceptions.
The TrackingEventProcessor (TEP) EventProcessor implementation is the one which will retry events with a certain (unconfigurable) back-off, if the configured ErrorHandler throws an exception.
If you want to retry on every exception (which might be a debatable approach anyhow), you'd thus want to enter the retry scheme of the TEP. To achieve this, you simply should configure the ListenerInvocationErrorHandler to also be a PropagatingErrorHandler. I'd recommending reading the Reference Guide on the matter too, to get a better idea of how to configure this.

Ejb injection in web service when no ejb instance is available in the pool

I want to know what will happen when there are no ejbs available in the pool and a client is trying to access a web service which uses the ejb.
We are receiving a NPE at the line where the ejb instance is used to call a method. Unfortunately I do not have access to the logs right now and I am trying to figure out what is wrong. So I am thinking in all possible ways and this question spawned in my head.
Can anyone please tell me?
What I think is, the web service will not be initialized until an ejb instance is available in the pool. So In this case the request will be queued and after sometime the client will receive a timeout error or appropriate message. Am I right?
P.S
BTW, if it makes any difference, I am injecting the ejb using #EJB annotation.
If you use a reference to a SLSB the initialization is just a proxy, no instance is needed.
At runtime the invocation try to get an instance from the pool, if there are all instances busy it will be blocked for a while (5sec by default) and throw an Exception in case of timeout, otherwise just continue.
If you get a NPE this seems to me a different issue where you can't get a reference.
A stateful bean is different, but I think you don't use that.
I think it should be the same no matter which container you use.

what happens if System Exception occurs from a method in a session bean in EJB 3.0?

What happens if a system exception occurs from a method in a session bean that has a transaction attribute REQUIRED_NEW and this method is called by some other method which is running in other transaction (i.e REQUIRED).
Will the suspended transcation(i.e Required) rollback or not ??
what happens if System Exception occurs from a method in a session bean in EJB 3.0?
This is what the ejb3 specification says:
14.2.2 System Exceptions
... The container catches a non-application exception; logs it; and, unless the bean is a message-driven bean, throws the javax.ejb.EJBException.
... The transaction in which the bean method participated will be rolled back....
and the bean instance will be discared.
Will the suspended transcation(i.e Required) rollback or not ??
In your case the Client method runs in a different transactional context, therefore, if you catch the
javax.ejb.EJBException that was originated in the target bean, the transaction will commit. Otherwise, the transaction will be rolled back.
If you don't handle exception inside method A, exception will throw higher and rollback bought transactions.

How to control transaction of CMT EJB from client

We are trying to build an application, which talks to the remote EJB services and local database. EJB methods are CMT with TransactionAttributeType.REQUIRES_NEW.
My question is: how can we control EJB transactions from client?
You cannot control EJB transactions from the client. If you're using Container Managed Transactions than you've decided that the container should manage them.
The only "control" over transaction the client has is to re-invoke a method after EJBException or the implicit rollback exception.
The client invokes a method with TransactionAttributeType.REQUIRES_NEW and the rest lies in the hand of the EJB Container.

Best Way to handle Exception in multi-tier

I got a question about exception handling.
I guess it may be quite easy for you but for me I just don't know how to handle exception in multi-tier projects.
Let's say in my solution, I have several project.
I have (lower) DataAccess, BizComponent, WCF, Proxy and Presentation (upper).
I try to "try catch" in DataAccess and throw the exception to BizComponent and in BizComponent I try again "try catch" and log the error and throw the exception again to WCF.
In WCF and Proxy layers, I do the same thing. In presentation layer, I show come custom message to end user.
My senior told me that I only need to start doing "try catch" in WCF and upper layers.
And I don't need to do in DataAccess and BizComponent because it will be caught in
WCF.
Should I try to catch the exception in DataAccess,BizComponent and
throw exception or should I just try to catch only in WCF and starts
throwing to upper layers?
Which one is better practice ?
If you are not clear about my question, please let me know.
This is my first multi-tier projects so it makes me confused.
Thanks in advance.
As a rule of thumb only handle (catch) exceptions if you are going to do something with them.
For example, if you could catch a database exception in the data layer if you wanted to provide additional information in the "upper" layers.
An approach I use personally is to catch and log exceptions in the business layer and then rethrow the same exception or a wrapping exception that provides more friendly information for layers higher in your stack. This provides a consistent logging process and does not require boiler plate logging code in the application.
If you reuse the code in multiple applications and need different log stores this can be handled using dependancy injection.

Resources