Im new to both spring and JSF and I'm developing a JSF app and I'm using Spring to maintain my Services and DAO's. The DAO's use Springs unchecked DataAccessException hierarchy so that no exceptiondeclarations affect the signature of the methods of the DAO interfaces. I have a ServiceLocator maintained by JSF in the faces-config.xml looking up my Spring maintained services. So far so good.
The thing I'm don't know how to solve in the cleanest possible way is how to deal with the unchecked exceptions. I understand the positives about using unchecked exceptions, that they don't affect method signatures, that I don't have to propagate exceptions up the chain since it can probably only be dealt with in the presentationlayer etc. But the downside is that the programmer using the services has to know about what exceptions that might be thrown by the service. This obviously only has to be done in the layer closest to the presentation (i.e. in the JSF controller). I want to catch any DataAccessExceptions in the controller so that I can present an error to the user that's more descriptive than e.g. "Login failed". If the database is down there's no point for the user to retry logging in etc.
One way is obviously to have code in each actionmethod in the controller to catch the exceptions (which will clutter up the controllers a bit). Also, the programmer creating the controller has to know about what servicemethods that throws exceptions as well as maybe know what exceptions to catch (maybe not an issue, could maybe be 100% assumed that servicemethods throws exceptions!?) I also thought about using Spring AoP to setup this exception catching. That would be a clean way, but the problem is that the JSF controller isn't managed by Spring and therefor cannot be referred to in the beandefinition.
Another thing I'm not entirely clear of is if I should let my DAO's throw DataAccessExceptions? It affects the method signature and it doesn't have to be catched, but at least it gives a hint to the user of the interface that such an exception might be thrown!? Is that completely useless?
As I said, I'm new to both JSF and Spring, I might be missing something here. Any ideas or hints on a nice, clean solution for this would be greatly appreciated!
first off, I wouldn't throw it because it's unchecked for a reason, meaning either the database is down, or someone screwed up the configuration and it cannot access the database. Either way it would be VERY annoying if we had to catch that exception for each call (then every controller would be fat with catch statements).
I've never used JSF, but an example of how I would handle this in struts is this the following: A better way with struts is by using the exception handlers, but for your question I think this applies. Remember I've never used JSF, but is there an interface or a class that the controller must extend, then same concept. Hope this helps
in this case all my actions will now extend MyBaseAbstractClass instead of Action, a simple wrapper to handle common/global problems.
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?