本人Spring菜鸟,当我使用Springcloud+redis队列时,我在其中一个模块启动定时器定时从Redis中取数据更新至数据库,出现了TransactionRequiredException,解决方ran案是在做更新操作的service层的方法上加上@Transactionl 并添加配置类如下即可完美解决事务问题:
import org.apache.tomcat.jdbc.pool.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.TransactionManagementConfigurer; import javax.persistence.EntityManagerFactory; /**