How to manually start a transaction on a shared EntityManager in Spring?

帅比萌擦擦* 提交于 2019-11-30 02:02:58

You should use TransactionTemplate object to manage transaction imperatively:

transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            em.createNativeQuery("TRUNCATE TABLE MyTable).executeUpdate();
        }
    });

To create TransactionTemplate just use injected PlatformTransactionManager:

transactionTemplate = new TransactionTemplate(platformTransactionManager);

And if you want to use new transaction just invoke

transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

As a workaround I now created a new EntityManager explicit using the EMF, and starting the transaction manually.

@Autowired
private EntityManagerFactory emf;

public void clearTable() {
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.createNativeQuery("TRUNCATE TABLE MyTable).executeUpdate();
    tx.commit();
    em.close();
}

That's probably not ideal, but works for the moment.

Spring Data JPA automatically runs CRUD method in transactions for you (without needing to set up anything except a transaction manager). If you want to use transactions for your query methods, you can simply add @Transactional to these:

interface MyRepository extends CrudRepository<MyEntity, Integer> {

  @Transactional
  @Modifying
  @Query(value = "TRUNCATE TABLE MyTable", nativeQuery = true)
  void clear();
}

On a more general note, what you have declared here is logically equivalent to CrudRepository.deleteAll(), except that it (your declaration) doesn't honor JPA-level cascades. So I wondered that's really what you intended to do. If you're using Spring Boot, the activation and transaction manager setup should be taken care of for you.

If you want to use @Transactional on the service level, you need to setup both a JpaTransactionManager and activate annotation based transaction management through either <tx:annotation-driven /> or @EnableTransactionManagement (looks like the activation was the missing piece on your attempt to create transactions on the service layer).

@Transactional annotation should not be applied on Dao method but on a service method. Although your code says DatabaseService is a service, but by inserting EntityManger inside a service does not make any sense.

Correct way to implement is to create a Dao like below.

@Repository
public class DatabaseDao {
    @PersistenceContext
    private EntityManager em;

    public void clear() {
        em.createNativeQuery("TRUNCATE TABLE MyTable").executeUpdate();
    }
}

Then call the dao method from a service method with @Transactional annotation.

@Service
public class DatabaseService {
    @Autowired
    private DatabaseDao dao;

    @Transactional
    public void clear() {
        dao.clear();
    }
}

Also, add @EnableTransactionManagement in your Configuration class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!