Creating a post commit when using transaction in Spring

后端 未结 4 957
余生分开走
余生分开走 2020-12-13 02:47

Due to certain reasons i have manually performed transaction commit and roll back using Spring PlatformTransactionManager, what i need to do is setup a hook so that a post c

4条回答
  •  余生分开走
    2020-12-13 03:00

    In one of my projects because of certain reasons I also had to use PlatformTransactionManager. So I forced to use org.springframework.transaction.support.TransactionTemplate.

    http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/transaction/support/TransactionTemplate.html

    The main benefit is that if you have implemented PlatformTransactionManager correctly, you don't need to bother with manual commit/rollback. At least source code of TransactionTemplate may help you if you need more specific thing.

    It's pretty simply to use:

    config.xml

    
        
    
    

    MyServiceImpl.java

    
    @Service
    public class MyServiceImpl implements MyService {
    
        @Autowired
        private TransactionTemplate transactionTemplate;
    
        public Entity getSomethingWithTx(final long id) {
    
            return transactionTemplate.execute(new TransactionCallback() {
                @Override
                public Entity doInTransaction(TransactionStatus status) {
                    //TODO implement
                }
            });
        }
    
    

提交回复
热议问题