Solved

Java Message handling in Service Layer

Posted on 2016-10-13
3
58 Views
Last Modified: 2016-10-18
I´m designing a service layer in a multilayerd architecture (Service, Business, Dao). Suposse I need a method like getUserAccounts

This method just return a list of Accounts, but what if my dao layer can´t connect to database, or if I have some error when reading the recorset, a parse problem for example. Or maybe another error in the business layer.

Do I have to catch the exception in the service layer, and map to a error code?
Do I have to catch the exception in the business layer, and the buisness layer return a bean with the error code? Avoiding to have any kind of logic in the service layer, more than exposing the Rest API
0
Comment
Question by:anonymous
3 Comments
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 500 total points
ID: 41842543
Throw an exception and handle it in your rest layer with a exception handler.
You don't need to map all the error codes as you described. A generic error message is sufficient.
No user of your application would be interested to know the update failed because it violated referential integrity.
1
 

Author Comment

by:anonymous
ID: 41842563
Great! If the exception is in the DAO Layer should I catch it in the Business layer also? or only in the service layer?
0
 
LVL 26

Expert Comment

by:dpearson
ID: 41842935
You should always catch Exceptions at the level of the system that knows how to correctly handle that exception.

In other words, "it depends".  If you know what to do at the business layer to handle the exception (e.g. database offline, let's check the cache) then handle it there.

But if you need to return to the user "it didn't work" you should let it bubble up to the service layer and send the error out to the user.

Doug
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now