As we all know, Spring DAO is based on template method pattern. The reason why Spring DAO adopts template method pattern is that there are some setted processes before and after accessing the database, such as start and commit the transaction, open and close the database connection. Therefore, when we use Spring DAO template, for example, HibernateTemplate and JdoTemplate, we don’t need to write code for transactions by ourselves, since the DAO template will does that job for us.
However, I has been thinking about a question , and I still not find the answer to it. Spring framework supports declaration transaction. In fact, in many projects, we use DAO template to access the database, at the same time, we also configure transaciton in spring configuration files. Therefore, is it necessary to declare transaions in spring configuration files when we already take the advantage of DAO template, which already helps us handle transactions? is it redundant for us to do that?
In order to find the answer, I have refered to some materies. There is an explansion in spring document. It is that “even if HibernateTransactionManager is used for transaction demarcation in higher-level services, all those services above the data access layer don’t need to be Hibernate-aware. Setting such a special PlatformTransactionManager is a configuration issue: For example, switching to JTA is just a matter of Spring configuration (use JtaTransactionManager instead) that does not affect application code.”
What is your opinion about this explanation. To be honest, It doesn’t make sense to me and it still not help me answer this question. I am sorry for my slow response. I hope you can help me solve this question! Thanks a lot!