Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Catching Spring DAO exceptions

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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!
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I have the same question. Can someone help with an answer?

Thanks,
Devi
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic