Wednesday, April 27, 2011

Enterprise Application's Logical Layers and where to manage Transactions

Recently I came across an application which manage transaction's at Dao layer that means there is no proper transaction management and if an exception occurs during runtime will leave inappropriate data in database.

Consider this simple Architectural layers

Brief description of each block starting from the bottom.

DB1 .. DBn → Databases, an application sometimes refers to multiple databases and simple application has one.

Dao Layer →  Encapsulates logic to add, update, delete and read data from Databases using JPA, Hibernate or JDBC.

Entity/Dto → Simple Pojo classes which encapsulate database entities

WebServices Client → Another Integration Layers like Dao which communicates with other systems via SOAP, JMS etc.

Service Layer → This layer contains business logic which encapsulate the data (Dao & Webservices client) they operate on and provide hardened interfaces as the only way to access their functionality. This approach reduces side effects and allows services to evolve at their own pace without impacting the other components of the overall system.

UI/Controller → If you use any MVC framework then this layer contains Controller or Action classes which directly serve browser requests and delegates all operations to Service Layers Classes i.e. they don't have any business logic in them.

Web Services → This layer contains Web Services or External facing API's if you plan to expose some functionality for other systems to integrate via SOAP, REST, RMI etc. Also this should just delegate all functionality to Service Layers classes, Main reason we have this extra layer to expose some functionality is to have its own space to evolve with different revisions without impacting the overall system, this separation is very important if you system needs to involve over time and external changes.

Coming back to the original topic as which layer to manage transactions, also you can clearly see in the Architecture diagram above that Service Layer lies at the heart of the system exposing business logic to UI and other API's and hence that's where Transactions should be manage.

Checkout this sample transaction management code

* @author intesar
public class UserServiceImpl implements UserService {

    * Dummy Use Case # 33
    * Admin can select multiple users on UI page and update the active status and then saves
    * updates multiple users in a single transactions
    * @param users
   @Transactional(propagation = Propagation.REQUIRED)
   public void updateUsers(List<User> users){
       for ( User user: users ) {
   protected UserDao userDao;
   protected static final Log log = LogFactory.getLog(UserServiceImpl.class);

As you can see in the above code snippet updateUsers(..) method part of Service Layer class,  updates multiple users in a single operation and if any user update fails then entire operation rollsback. However this not possible if we are managing transactions at Dao layer.

I am using Spring framework to manage my transactions though you can use JEE or any other framework.

Tuesday, April 19, 2011

Reverse Engineer UML from Maven project

I am a Netbeans user and after they discontinued UML plugin I started looking at other options which will work with my Maven projects and found this open source tool UMLGraph which does a good job but only lacks in proper documentation.

Good thing about this tool is, It works as a Maven plugin so all my UML gets automatically updated with any changes to code.

Setup is very straight forward and takes less then a minute.

Step 1
Install Graphviz 
   Is an open source graph visualization software.
   Note : Only install in a non-space directory (i.e "Program Files" won't work)
   Website :
   Download link : Windows download

Step 2 Is the Maven plugin which does the job
   Setup UMLGraph plugin to your pom.xml

Step 3
  command to generate UML : mvn javadoc:javadoc
   UML is created inside target/site directory of your project

  Sample Image

Thursday, April 14, 2011

Simple Reverse Lookup Enum in Java

Documentation inline with the code.

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

 * @author Intesar Mohammed
 *  Reverse Enum Lookup example using Map and EnumSet
public enum Status {


    *   code or your required variable can be of any type
    private int code;

    private Status(int code) {
        this.code = code;
    private static final Map<Integer, Status> lookup = 
                  new HashMap<Integer, Status>();
     * Stores all Enum's into lookup
    static {
        for (Status s : EnumSet.allOf(Status.class)) {
            lookup.put(s.code, s);

     * toString() is the only api to print Enum value
     * All Enum classes can consistently override toString() and provide a simple api
     * across different Enums
     * @return
    public String toString() {
        return String.valueOf(code);

     * Sample lookup code
     * Status status = Status.get(0);
     * Status status = Status.get(5);
     * Status status = Status.get(10); // this will return null
     * Also you can rename get() to reverseLookup() or lookup()
     * for readability convenience
     * @param code
     * @return
    public static Status get(int code) {
        return lookup.get(code);


Friday, April 1, 2011

Why Gmail Motion and why not Gmail Speech ?

Why the heck I will use my body to interact with the Gmail rather I prefer speech to do basic operations like.
  1. Speech command which triggers Gmail to Read a particular email.
  2. Gmail  converting my speech to text and sending as email
  3. Search using speech commands etc
I can hook a simple bluetooth headset and be connected to my Gmail. Also this will be handy on my touch pads and phones etc.

And I don't think people will be using this regularly rather they will use it for fun few times and a total waste of time. Learning body language for one particular Application doesn't work out for me.

 Incase if you don't know what is Gmail Motion

Gmail Motion