Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple EJB question

 
Abhijit Das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit,

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.

Regards,
Reza
 
Abhijit Das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Reza,,.that is a good approach.
 
Tim Holloway
Bartender
Posts: 18419
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhijit Das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhijit Das
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!