“`text @Transactional(propagation = Propagation.SUPPORTS) 无事务正在进行中 “`

huangapple go评论61阅读模式
英文:

@Transactional(propagation = Propagation.SUPPORTS) no transaction is in progress

问题

我有一个简单的CRUD项目
我有一个使用Hibernate 5和Spring Boot的简单CRUD项目当我使用`@Transactional(propagation = Propagation.SUPPORTS)`我捕获到错误`javax.persistence.transactionrequiredexception: no transaction is in progress`。当我使用`@Transactional(propagation = Propagation.REQUIRED)`一切正常
我做错了什么

Repository:

    
    @Transactional(propagation = Propagation.SUPPORTS)
    public Toy getToy(String companyName, String toyName) throws Exception {
        Session session = sessionFactory.getCurrentSession();
        try {
            Query query = session.createQuery("from Toy where company.name = :company and name = :name");
            query.setParameter("company", companyName);
            query.setParameter("name", toyName);
            return (Toy) query.getSingleResult();
        } catch (NoResultException e) {
            Toy toy = new Toy();
            Query query = session.createQuery("from  Company where name = :name");
            query.setParameter("name", companyName);
            Company company = (Company) query.getSingleResult();
            toy.setCompany(company);
            toy.setName(templateName);
            toy.bought(0);
            toy.setViewed(0);
            return toy;
        }
    }
英文:

I have a simple CRUD project.
I have a simple CRUD project with Hibernate 5 and Spring Boot. When I use @Transactional(propagation = Propagation.SUPPORTS), I catch the error javax.persistence.transactionrequiredexception: no transaction is in progress. When I use the @Transactional(propagation = Propagation.REQUIRED), everything is fine.
What I doing wrong?

Repository:

@Transactional(propagation = Propagation.SUPPORTS)
public Toy getToy(String companyName, String toyName) throws Exception {
    Session session = sessionFactory.getCurrentSession();
    try {
        Query query = session.createQuery("from Toy where company.name = :company and name = :name");
        query.setParameter("company", companyName);
        query.setParameter("name", toyName);
        return (Toy) query.getSingleResult();
    } catch (NoResultException e) {
        Toy toy = new Toy();
        Query query = session.createQuery("from  Company where name = :name");
        query.setParameter("name", companyName);
        Company company = (Company) query.getSingleResult();
        toy.setCompany(company);
        toy.setName(templateName);
        toy.bought(0);
        toy.setViewed(0);
        return toy;
    }
}

答案1

得分: 0

REQUIRED是默认的传播行为。Spring会检查是否存在活动事务,如果没有任何事务存在,则会创建一个新事务。否则,业务逻辑会追加到当前活动事务中。

SUPPORTS,Spring首先检查是否存在活动事务。如果存在事务,则将继续使用现有事务。如果没有事务,则会以非事务方式执行。

因此,你之所以在进入“SUPPORT传播”事务包裹的方法时出现错误,仅仅是因为没有事务存在。

顺便说一下,SUPPORTS的使用情况非常有限...大多数情况下,使用默认的“REQUIRED”正好满足我们的需求。

英文:

REQUIRED is the default propagation. Spring checks if there is an active transaction, then it creates a new one if nothing existed. Otherwise, the business logic appends to the currently active transaction.

SUPPORTS, Spring first checks if an active transaction exists. If a transaction exists, then the existing transaction will be used. If there isn't a transaction, it is executed non-transactional.

So you got this error simply because no transaction exists when entering inside the method wrap by "SUPPORT-propagation" transaction.

By the way SUPPORTS have very limited use-cases... most of the time using default "REQUIRED" offers exactly what we need.

huangapple
  • 本文由 发表于 2020年5月5日 03:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61599832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定