We help IT Professionals succeed at work.
Get Started

Spring transactional layer setup

Squadless
Squadless asked
on
278 Views
Last Modified: 2016-07-26
Hi, I'm designing the backend of an app where i've a few scenarios and i'd like to get the base right in terms of how to properly design my daos and daoimpls and service layers.  Whats important is that i have situations where if a request comes in to add an entity, lets say, multiple tables will be populated, so for example entity table and generic details table.  I would like to create an ecosystem where my transactions are properly defined and are correct meaning that
if(add){
 -row inserted into table1
 -20 rows inserted into table2
 -21st row fails to be inserted into table2 due to a .. lets say primary key violation..
 - ROLLBACK ENTIRE transaction.
}
My current design is
Entity - pojo
EntityDao - interface that has methods like boolean addEntity(Entity entity);
EntityDaoImpl - extends EntityDao, the actual implementation where the addEntity method does a insert into the database.

Ive ran and tested and all is fine except transactional aspect is not working.  
My QUESTION is.. do i need a service layer somewhere in between if i want to handle transactions or marking the methods inside my dao impl class @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) is enough?

If i implement EntityServiceImpl where i have a method like addEntity that calls entityDaoImpl.addEntity() and wrap the EntityServiceImpl method @Transactional, is that better then having just a DaoImpl be transactional? i guess it is right so that i can commit or rollback and etc..

Thoughts?
THx
Comment
Watch Question
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE