Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialxiangwu
6,954 PointsWhich layer to take care of the transaction management
I noticed that in the code example of the course using spring with hibernate, it’s the DAO layer that takes care of the transaction management. For example, we can see the method call of session.beginTransaction() in save method of CategoryDaoImpl class. So which layer is better for the transaction management, the service layer or the DAO layer? Also can we use @transactional annotation to do annotation based transaction management? If so, could you provide some code snippet for changing the existing code?
2 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Pointsit’s the DAO layer that takes care of the transaction management. For example, we can see the method call of session.beginTransaction() in save method of CategoryDaoImpl class. So which layer is better for the transaction management, the service layer or the DAO layer?
Transaction happen at DAO layer, as all database operations should be there.
But
@Transactional annotation is used in Service layer and it is not easy question to answer...
There are numerous of good answers about that on stack. Check here e.g.
http://stackoverflow.com/questions/3886909/where-should-transactional-be-place-service-layer-or-dao
One thing i needed for one of Techdegree projects, was implementations of GenericService
to reduce amount of boilerplate code
http://www.codesenior.com/en/tutorial/Spring-Generic-DAO-and-Generic-Service-Implementation
There we used @Transactional to make sure that operations will wrapped in transaction... well more I cannot say.
About using @Transactional in DAO... I haven't seen that, and judging by the Stack answer you should not too.
if you want to make your code better, get to latest Spring courses where Spring-Data is used. It is very contemporary, well-defined way to write less code in DAO layer, so I suggest you learn it, if that was purpose of your question....to write less code with more efficiency
I guess that is all I could say about @Transactional and DAO layer efficiency
xiangwu
6,954 PointsSorry about the late replay and thank you for sharing. Would you please share your configuration of the transaction managers? Thanks!
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsSorry, I could not understand you.
What do you mean by
configuration of the transaction managers
Here is yet another nice article about using @Transactional in Spring
http://blog.jhades.org/how-does-spring-transactional-really-work/
There you can find example of Transaction Manager Configuration, code from there:
@Configuration
@EnableTransactionManagement
public class TransactionManagersConfig {
@Autowired
EntityManagerFactory emf;
@Autowired
private DataSource dataSource;
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager() {
JpaTransactionManager tm =
new JpaTransactionManager();
tm.setEntityManagerFactory(emf);
tm.setDataSource(dataSource);
return tm;
}
}
But I've never done this myself, because we use Spring to avoid re-writing things over and over again...
I would rather suggest learning Spring-Data, before going into Transaction management, that does most of the har work for us.
If you give me concrete example, I can try to help you.
I'm sorry I can't speak about abstract solutions to abstract problems, I was never really good at it