Avatar of Squadless
Squadless

asked on 

Spring transactional layer setup

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
JavaJava EEWeb Frameworks

Avatar of undefined
Last Comment
mccarl

8/22/2022 - Mon