File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Review of my first Session EJB bean for best practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Review of my first Session EJB bean for best practice" Watch "Review of my first Session EJB bean for best practice" New topic
Author

Review of my first Session EJB bean for best practice

thomas jacob
Ranch Hand

Joined: May 19, 2005
Posts: 91
Hi All,
Please review the EJB 3.1 Session bean for best practice and taking advantage of interceptors. This session bean will be called by the JSF managed bean. I am having the following questions
1) Where do I place the dmd object so that it can be used by both getTables() and getColumns() business methods?
2) How can I use the interceptors for cross cutting code like logging?
3) What other parts of the code can be improved like exception handling, lifecycle callback methods etc?



Thanks in advance
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

thomas jacob wrote:Hi All,
Please review the EJB 3.1 Session bean for best practice and taking advantage of interceptors. This session bean will be called by the JSF managed bean. I am having the following questions
1) Where do I place the dmd object so that it can be used by both getTables() and getColumns() business methods?




I would remove that Connection instance variable and instead create and close a connection wherever required, like in the getTables method.

As for where to store the "dmd" (DatabaseMetaData), it's really upto your requirements. Do you want to query the DatabaseMetaData on every call to the getTables() or do you want the getTables() to return a cached instance? If you want to return a cached instance, then I guess you can make that MetaDataBean a @Singleton and then store the DatabaseMetaData after the first call to the getTables().

thomas jacob wrote:
2) How can I use the interceptors for cross cutting code like logging?


Well, you can use interceptors. But again it depends on the kind of log messages you want to output. Each bean and each business method in that bean will practically have to write out various (specific) log messages and not necessarily just at entry and/or exit. In such cases, having an interceptor wouldn't help much.

As for exception handling - just printing out the exception stacktrace isn't really a good idea, in a practical application. Most of the times, you have to throw back an ApplicationException or a system exception so that the client and the container know what to do with it.

[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Review of my first Session EJB bean for best practice