Skip to main content

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
*/
@Service
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
    */
   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public void updateUsers(List<User> users){
       for ( User user: users ) {
           userDao.merge(user);
       }
   }
   
   
   @Autowired
   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.

Comments

Javin Paul said…
I didn't get the point completely , to me most of the transaction completes when data successfully entered into database, so either you could have synchronous database insertion or asynchronous database insertion , in case of asynchronous database insertion there is chance of data loss if data stays in temp memory and application dies. What's your opinion ?

Javin
Why String is immutable in Java
Don Stevo said…
I think the point stated here is:

If your service method calls several DAO methods, when you put your transaction management in the DAO layer each previous DAO method will be comitted, even if a subsequent DAO method throws an exception. Possibly erronous data will remain in the database this way.

If your transaction management is around your Service method, when one of the DAO methods throws an Exception your entire Service call will be rollbacked and no modified data will remain in the database.
jbaris said…
This comment has been removed by the author.
jbaris said…
You are forgetting a BusinessObject layer between the Services layer and the DAO layer. All the business logic must be inside this layer, and its methods can be transactional.
The adventage of having a BO layer is that other technologies than WS (eg JMS) can access to the BO logic, and you can make transactional multiple DAO calls.
In brief, having a BO layer have to adventages:
1) solves your problem of transactinality
2) makes your app more extensible (for other communication technologies)

Best regards

Popular posts from this blog

JPA 2 new feature @ElementCollection explained

@ElementCollection is new annotation introduced in JPA 2.0, This will help us get rid of One-Many and Many-One shitty syntax.

Example 1: Stores list of Strings in an Entity

@Entity
public class Users implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
@ElementCollection
    private List<String> certifications = new ArrayList<String>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<String> getCertifications() {
        return certifications;
    }

    public void setCertifications(List<String> certifications) {
        this.certifications = certifications;
    }
..
}

        Users u = new Users();
        u.getCertifications().add("Sun Certified Java Programmer");
        em.persist(u);

Generated Tables

   Users
Column --> ID
    Row             1

Users_CERTIFICATIONS

Validating CSV Files

What is CsvValidator ?
  A Java framework which validates any CSV files something similar to XML validation using XSD.

Why should I use this ?
  You don't have to use this and in fact its easy to write something your own and also checkout its source code for reference.

Why did I write this ?
  Some of our projects integrate with third party application which exchanges information in CSV files so I thought of writing a generic validator which can be hooked in multiple projects or can be used by QA for integration testing.

What is the license clause ?
GNU GPL v2

Are there any JUnit test cases for me checkout ?
 Yes, source

How to integrate in my existing project ?

Just add the Jar which can be downloaded from here CsvValidator.jar and you are good.

Instantiate CsvValidator constructor which takes these 3 arguements

         // filename is the the file to be validated and here is a sample         // list - defines all the fields in the above csv file ( a field has index, type, isOptional, rege…

ArrayList vs LinkedList vs HashSet Performance Comparision

ConclusionsInserting & Reading sequentially from Collection prefer LinkedList/ArrayListInserting & Reading/Deleting by Search/equals from Collection prefer HashSetInserting, ArrayList & LinkedList performs best while HashSet takes double the timeReading, HashSet performs best while ArrayList & LinkedList are marginally lessDeleting, HashSet performs 10 times better than ArrayList & ArrayList performs 4 times better than LinkedList. LinkedList is slow because of sequencial search Bottom line : unless you are not going to iterate using for(Integer i : list ) then prefer HashSet
Inserting/Reading/Deleting integer's from zero till countJDK7Collectionactioncounttime msArrayListInsert1000/1LinkedListInsert1000/1HashSetInsert1000/1ArrayListInsert100005LinkedListInsert100004HashSetInsert100007ArrayListInsert10000011LinkedListInsert10000011HashSetInsert10000021ArrayListGet/Read1000LinkedListGet/Read1000HashSetGet/Read1000ArrayListGet/Read100004LinkedListGet/Read100003Has…