English 中文(简体)
@ 采用另一种方法进行的交易方法没有获得交易
原标题:@Transactional method called from another method doesn t obtain a transaction

In Spring, a method that is annotated with @Transactional will obtain a new transaction if there isn t one already, but I noticed that a transactional method does not obtain any transaction if it is called from a non-transactional one. Here s the code.

@Component
public class FooDao {
    private EntityManager entityManager;

    @PersistenceContext
    protected void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Transactional
    public Object save(Object bean) {
        return this.entityManager.merge(bean);
    }

    public Object saveWrap(Object bean) {
        return save(bean);
    }
}

@Component
public class FooService {
    private FooDao fooDao;

    public void save(Object bean) {
        this.fooDao.saveWrap(bean); // doesn t work.
        this.fooDao.save(bean); // works
    }
}

<>代码>saveWrap(>)是一种固定方法,称为<代码>save(>,即交易,但saveWrap(<>得起任何改动。

I m 采用第3和第3版。 我在这里做了什么错误? 感谢。

最佳回答

这是春天行动的限制之一。 由于达奥人事实上是春天创建的代理,这意味着从同一类别中提取一种方法不会引起咨询意见(即交易)。 任何其他方面也是如此。

问题回答

是的,这是预期的行为。 春天,在物体周围制造代理。 代理拦截从其他物体向物体打电话。 代理人不拦截物体内的电话。

如果你想做这项工作,就“外派”援引的方法添加<条码>@Transactional。

我知道,这是很晚的,但只是想补充克服这一限制的办法,就是在方法上从申请的角度来看获得泉星,并采用这种方法。 如果从申请角度获得春天豆,则将不是原星。 由于代理行人现在正在使用这种方法,而不是原始信条,因此将执行交易建议。

A possible workaround is to call the method like if it was invoked from "outside"

你们可以这样做,获得该构成部分目前的代理,然后使用这种方法:

 ((MyService) AopContext.currentProxy()).innerMethod();

资料来源:。 https://www. programrsought.com/article/58773839126/





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签