0

目前我使用MVC架构开发Spring 3,Mybatis和Struts2集成应用程序。但是我在我的应用程序中处理事务时遇到了一些困难。我在我的服务层使用Spring Transaction,喜欢这个

服务层

         @Service("MyService")
         @Transactional
         public class MyServiceImpl implements IMyService {
              @Transactional(readOnly=false)
              public void myMethod() {

              }
         }

我的问题是“我应该在数据访问层而不是服务层中使用 Spring Transaction 吗?” 喜欢这个

数据访问层

        @Repository("MyDAO")
        public class MyDAO implements IMyDAO {
             @Transactional(readOnly=false)
             public void myMethod() {

             } 
        }
4

1 回答 1

2

If your calling each DAO method through the Service layer than make the service layer transactional. If you call some DAO methods independent of the Service layer than the DAO methods will need to be transactional. You could also make both transactional since Spring will propogate the transactions in both layers, meaning if you call a Service method that is transactional which calls a transactional DAO method, they will share the same transaction.

于 2013-10-10T08:58:40.430 回答