Search code examples
javaspringhibernatetransactionsdata-access-layer

Transaction over Data abstraction layer(DAL: Data Access Layer)


Currently I am working on a DAL design, I have concern regarding managing transaction in Business Layer(BL) while keeping it unaware of DB technologies.

For ex: I have two DAOs

SalesOrderDAO, SalesOrderItemDAO

At some point in BL I would like to call update/create method on both in a transaction:

// some where in BL
  SalesOrderDAO soObj = new SalesOrderDAO();
  SalesOrderItemDAO soiObj = new SalesOrderItemDAO();

  //transaction start
  soiObj.create(); // it could be update as well
  soObj.update();  
  //on some condition transaction roll back

  //transaction end

How to acheive it?

Constraint:

  • I am using Hibernate in DAL.
  • I don't want my BL to be aware of Hibernate.
  • Outside the DAL no one knows or should know what technology is being used to access the DB

(Am I missing something?)


Solution

  • I did some research(after i received comment on question) I found spring have all I needed.

    Say, in BL we have method: SomeBLMethod(), which does db operation using SalesOrderDAO, SalesOrderItemDAO and SomeBLMethod should be under transaction. We need to do following to make Transaction work:

    • Get the object of SalesOrderDAO, SalesOrderItemDAO implementation using spring @Autowired attribute and perform db opration using these attributes.
    • Make SomeBLMethod transactional using sprint @Transactional attribute.
    • The implementation of SalesOrderDAO, SalesOrderItemDAO should use @Autowired SessionFactory object
    • We also need to do some spring configuration.(which you can find in following url)

    Reference:

    PS: I am adding this answer to help others.