簡體   English   中英

事務注釋在Spring Boot 2.1.3中不起作用

[英]Transactional annotation not working in spring boot 2.1.3

@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並休眠。 我已經按照上面的方法配置了entitymanagerfactory,並使用以下內容來獲取會話

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

但是@Transactional無法正常工作

您正在@Transactional方法內打開Session 這是錯誤的,因為當您將方法注釋為事務性方法時,將在單個會話中調用該方法,而無需打開另一個會話。

我有同樣的問題,我在類應用程序@EnableTransactionManagement上添加了此批注(proxyTargetClass = true)

這在我班級服務的方法上

@Transactional(rollbackFor = Exception.class)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM