Spring @Transaction method call by the method within the same class, does not work?

前端 未结 8 2189
谎友^
谎友^ 2020-11-22 05:07

I am new to Spring Transaction. Something that I found really odd, probably I did understand this properly.

I wanted to have a transactional around method level and

8条回答
  •  囚心锁ツ
    2020-11-22 05:38

    Here is what I do for small projects with only marginal usage of method calls within the same class. In-code documentation is strongly advised, as it may look strange to colleagues. But it works with singletons, is easy to test, simple, quick to achieve and spares me the full blown AspectJ instrumentation. However, for more heavy usage I'd advice the AspectJ solution as described in Espens answer.

    @Service
    @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
    class PersonDao {
    
        private final PersonDao _personDao;
    
        @Autowired
        public PersonDao(PersonDao personDao) {
            _personDao = personDao;
        }
    
        @Transactional
        public void addUser(String username, String password) {
            // call database layer
        }
    
        public void addUsers(List users) {
            for (User user : users) {
                _personDao.addUser(user.getUserName, user.getPassword);
            }
        }
    }
    

提交回复
热议问题