事务注释在Spring Boot 2.1.3中不起作用 [英] Transactional annotation not working in spring boot 2.1.3

查看:94
本文介绍了事务注释在Spring Boot 2.1.3中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

@Transactional annotation is not working for me in a springboot-hibernate project. I am using the annotation configuration for which i have done the following configuration. I have tried using the @Transactional on method and class name in the service layer and also in the dao layer, but no luck. I think there is some issue with the transaction manager configuration but I am unable to figure out how to configure the transaction manager in my application.

application.properties

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

dao

@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并进入休眠状态.我已经按照上面的方法配置了entitymanagerfactory,并使用以下内容来获取会话

I am using spring boot 2.1.3 and hibernate. I have configured the entitymanagerfactory as above and used the following to obtain session

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

@Transactional不起作用

推荐答案

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

You are opening a Session inside a @Transactional method. This is wrong, because when you annotate method as transactional, it is invoked inside a single session, you don't need to open another one.

这篇关于事务注释在Spring Boot 2.1.3中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆