Spring Boot:CrudRepository方法是@Transactional,我的服务不能

时间:2017-05-02 17:45:54

标签: java spring transactions spring-data-jpa

我有一个Spring Boot应用程序,我面临一个奇怪的问题。

我尝试将其中一种服务方法用于事务处理,但这并不起作用。

我的服务如下:

@Service("ppService")
public class PlanProgresServiceImpl implements PlanProgresService
{
    @Resource
    private DeploiementDAO deploiementDao;

    ...

    @Override
    @Transactional(rollbackFor = TechnicalErrorException.class)
    public Set<String> deployer(PlanProgresType planProgresType, List<Integer> listIdUo, boolean sharepoint, Campagne campagne)
            throws TechnicalErrorException

        ...

        createDeploiement(deploiement);

        throw new TechnicalErrorException("temporary exception to test the transactional behavior");
    }


    @Override
    public void createDeploiement(Deploiement deploiement)
    {
        deploiementDao.save(deploiement);
    }
}

这是我的DAO:

public interface DeploiementDAO extends CrudRepository<Deploiement, Integer>
{
    List<Deploiement> findByUniteOperationnelle(UniteOperationnelle uo);
}

问题是我的Deploiement对象总是插入数据库(PostgreSQL对象)。

我在application.yml logging.level.org.springframework.transaction.interceptor: TRACE

中添加了以下行

它在控制台中给了我这个:

2017-05-02 19:38:24.297 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Getting transaction for 
[org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne]
2017-05-02 19:38:24.310 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne]
2017-05-02 19:38:24.312 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne]
2017-05-02 19:38:24.323 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne]
2017-05-02 19:38:24.324 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Don't need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findByUniteOperationnelle]: This method isn't transactional.
2017-05-02 19:38:24.449 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2017-05-02 19:38:24.450 TRACE 7760 --- [io-8080-exec-79] o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]

这意味着SimpleJpaRepository的每个方法都是事务性的,当我们查看它的代码时就是如此:

@Transactional
public <S extends T> S save(S entity) {

    if (entityInformation.isNew(entity)) {
        em.persist(entity);
        return entity;
    } else {
        return em.merge(entity);
    }
}

那么,我该如何解决这个问题?

0 个答案:

没有答案