Search This Blog

Google Analytics

Thursday, April 12, 2007

J2EE Practices

Taken from the book: More Java Pitfalls

Client Tier

  • Use the new Preference API to store preferences instead of Properties.
  • Take advantage of interfaces when hiding implementation detail.
  • Be wary of not handling exceptions properly. Instead of returning a value from a method (such as false or null), thinks about the cause of your returning these values. If there could be multiple causes, throw an exception.
  • Instead of taking some information from one exception and placing it in a new exception, take advantage of the new JDK 1.4 changes to Throwable and add the original exception to your new exception.
  • Make use of the Java Assertion Facility instead of putting a lot of print statement.
  • When using assertion, follows the following rules:
    • Do use assertions to test postconditions on methods.
    • Do use assertions to test places where you believe control flow should not execute.
    • Do not use assertions to test preconditions on public methods.
    • Do use assertions to test preconditions on helper methods.
    • Do not use assertions that affect the normal operation of the code.
  • Use Xpath expression to search a DOM.
  • Use Apache Ant to facilitate application builds and deployments.
  • Other opensource tools:
    • Use Junit for unit test.
    • Use BugRat to track issues.
    • Use CVS for code repositories.
    • Use Jdepend to generate metric measurement to determine the software quality.
    • Use JavaNCSS to detect noncommented code.
    • Use checkstyle to ensure that previously established coding rules are incorporated into each individual source code components.
    • Use CruiseControl to automate the build process.
  • When deciding whether to create a Singleton class, first ask yourself the following questions:
    • Does there needs to be one global entry point to this class?
    • Should there be only one instance to this class in the VM?

If answer is yes, then use Singleton. If no, don’t use.

  • When using Singleton, make sure to:
    • Use private constructor.
    • Synchronize methods that need to be synchronized.
  • Don’t use Singleton as global variables and never let Singletons to become non-Singleton.

Web Tier

  • It is a good practice to make use of a Web tier framework to implements Web application. While it is possible to implements application using independent servlet and jsp, it will be a maintenance nightmare.
  • Be careful about your use of HttpSession in servlets. Be aware of the collision problem that could await you in regard to naming data objects with putVaue (). Avoid using common names to storing types in HttpSession with the putValue method.
  • Use removeValue instead of invalidate () when removing object specific to the application from HttpSession.
  • When building distributed business systems, keep in mind that transactional data should be handled in a database instead of LDAP which don’t have transactional protections. LDAP should be used to organize and search for relatively static information in an efficient manner, while database systems can accommodate frequent fluctuations in data.
  • Make use of the Java Regular Expressions introduced in Jdk 1.4 for form field validation.
  • Don’t declare instance variables in Servlet as it may cause concurrent issues.

Enterprise Tier

  • A J2EE application does not need to have EJB tier in it. The EJB tier is necessary if:
    • You need to deal with distributed or complex transaction scenarios (multiple systems, phased transactions).
    • Reliability (how well the system responds to something going wrong) is a major concern.
    • When your applications need to be spread over several containers in a transparent way to support things like clustering, load-balancing, and failover.
  • When design network applications:
    • Abstract details from the client.
    • Abstract things in a multi-tired solution.
    • When the network has the potential to be a bottleneck, use the “plan and execute” strategy - it is often better to send one large message with lots of information than to send smaller messages over the network.
  • Use local interface to improve performance of the EJB tier. Use local interface if your Web container and EJB container are in the same process.
  • Be aware of using more than one ResultSet at once. While many JDBC drivers will support the use of multiple concurrent ResultSet objects many will not. Many databases will exceed the number of maximum open cursors. To achieve maximum portability, rethink your design so that you only have one open at a time.
  • Don’t use Entity bean because you need a object persistence mechanism. Consider JDO as it is lightweight, minimizes database transaction implementation, and it supports reuse, particularly with Java components.
  • Don’t spawn thread in EJB.
  • Avoid using file operation in EJB.

No comments:

Post a Comment

Do provide your constructive comment. I appreciate that.

Popular Posts