事务注释在spring boot 2.1.3中不起作用

问题描述 投票:0回答:2

@Transactional注释在springboot-hibernate项目中对我不起作用。我正在使用我已完成以下配置的注释配置。我已尝试在服务层和dao层中使用@Transactional方法和类名,但没有运气。我认为事务管理器配置存在一些问题,但我无法弄清楚如何在我的应用程序中配置事务管理器。

application.properties

#spring configuration
spring.jpa.show-sql = true
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

@Autowired
    private EntityManagerFactory entityManagerFactory;
    @Override
    public void deleteSMS(String id) {
        logger.info("Delete sms details with id :: \"" + id + "\"");
        Session session = null;
        try {
            session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
            SMSDetails smsDetails = session.get(SMSDetails.class, Long.parseLong(id));
            if (smsDetails != null)
                session.delete(smsDetails);
        } catch (Exception e) {
            logger.error("Error occured while deleting the sms with id :: \"" + id + "\" :: " + e.getMessage());
            throw e;
        } finally {
            if (session != null)
                session.close();
        }
    }

服务

@Override
    @Transactional
    public void deleteSMS(String id) {
        smsDao.deleteSMS(id);
    }

我正在使用spring boot 2.1.3和hibernate。我已经如上所述配置了entitymanagerfactory并使用以下内容来获取会话

session = entityManagerFactory.unwrap(SessionFactory.class).openSession();

但是@Transactional没有用

java spring hibernate spring-boot kotlin
2个回答
3
投票

你在Session方法中打开一个@Transactional。这是错误的,因为当您将方法注释为事务性时,它在单个会话中调用,您不需要打开另一个。


0
投票

我有同样的问题,我在我的类应用程序@EnableTransactionManagement(proxyTargetClass = true)中添加了这个注释

这就是我班级服务的方法

@Transactional(rollbackFor = Exception.class)

© www.soinside.com 2019 - 2024. All rights reserved.