Suppose i write an EJB method in the bean class which is exposed through ejb remote object. the method does the login validation and return a boolean. if the username/password is incorrect it will return false. but i want to send the message as well when the login is unsuccessful (the message for invalid login eg. "invalid username or password or the username does not exist" etc.). my question is ... what is the best practice to return both boolean value and the message from a single exposed method in ejb layer?
There is no such "best practice" specific to EJB (especially as of EJB 3), other than the basic practices of writing good Java code. In case of what you are describing, returning boolean seems incorrect. you should probably return a higher level "result" object that returns both the boolean and the message.
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Actually, in cases like this, it's worthwhile considering throwing an Exception on failure. Preferably a user-defined class named something like LoginFailException.
Java Exceptions carry a message payload as part of the base Exception class definition. Plus, if you need additional context, you can also add that to your exception.
An IDE is no substitute for an Intelligent Developer.
posted 9 years ago
But Exception is a heavy object. While login failure is so common and frequently occurs (due to wrong username / password). It is probably not a good solution to throw user defined exception object. performance gets affected.. please correct me if I am wrong!!!
Originally posted by Abhijit Das: But Exception is a heavy object. While login failure is so common and frequently occurs (due to wrong username / password). It is probably not a good solution to throw user defined exception object. performance gets affected.. please correct me if I am wrong!!!
They are something related to a business, I mean the LoginFailException. What else could you do when your Business rule fails to validate?? You could throw a Business Exception with a message of why it failed. So I'd say, it is ideal to throw a user defined exception like the LoginFailException to the client which should contain everything of why it occurred.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
posted 9 years ago
but in my opinion it is better to return a bean object which would contain the message as well as the result. if the result shows failure, you have the message to display for the client. in the case of business rule failure you have the only opinion of providing the message rt? so why do we need to design a business exception in that case.