English 中文(简体)
间接Hibernate/JPA方法称交易损失
原标题:Indirect Hibernate/JPA method call loses transaction

I m 采用Sp/JPA2/hibernate这一代码:

class A {
  @Autowired
  B b;

  @RequestMapping("/test")
  public void test(final HttpServletRequest r1, HttpServletResponse r2) throws ... {

    b.inner();   // Works

    b.outer();   // javax.persistence.TransactionRequiredException: 
                 // no transaction is in progress ... :|
}

@Component
class B {
   @PersistenceContext
   EntityManager em;

   public void outer() { inner(); }

   @Transactional
   public void inner() { em.flush(); }
}

为什么只有当交易间接松散时,才有<条码>内()?

最佳回答

http://static.children.org/children/docs/ 当前/vis/transaction.html#transaction-declarative-annotations

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.

如果你预计自雇工作也与交易进行总结,则考虑采用AspectJ模式(见下表)。 在这种情况下,首先不会有代理;相反,目标类别将重现(即修改其分法),以便把“交易”变成任何一种方法的操作时间行为。

The @Autowired reference B b (inside class A), is wrapped with a Spring AOP transaction-aware proxy.

当打电话到b.inner(>时,你在交易通知中援引该编码,编号为@Transactional。 因此,春天管理的交易已经开始。

When b.outer() is called, it is also on a transaction-aware instance, but it is not @Transactional. Thus a Spring managed transaction is not started.

Once you are inside the invocation of outer() the call to inner() is not going through the transaction-aware proxy, it is being invoked directly. It is the same as this.inner(). Since you are invoking it directly, and not through the proxy, it does not have the Spring transaction-aware semantics.

由于没有开始交易,结果见<代码>《交易查询

Possible solutions include making the method outer() @Transactional.

   @Transactional
   public void outer() { inner(); }

或使整整整类“<代码>@Transactional。

@Transactional   
@Component
class B {
   @PersistenceContext
   EntityManager em;
问题回答




相关问题
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 ...

热门标签