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

You should use TransactionTemplate object to manage transaction imperatively:

transactionTemplate.execute(
    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);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)