JPA事务提交奇怪的行为

时间:2014-03-14 13:25:26

标签: java jpa persistent-storage

我正在努力了解jpa交易工作。

说我有以下persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="survex" transaction-type="JTA">
    <jta-data-source>jdbc/MyDatabase</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

并使用sniffering mysql服务器执行以下测试代码:

public class Servlet extends HttpServlet {

    @PersistenceContext(type = PersistenceContextType.TRANSACTION)
    EntityManager em;

    @Resource
    UserTransaction utx;

    Log log;

    public void initialize() {
        log = em.find(Log.class, 1);
    }

    public void meth1() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
        utx.begin();
        em.merge(log);
        log.setMessage(String.valueOf(Math.random()));
        utx.commit();
    }

    public void meth2() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
        utx.begin();
        em.merge(log);
        log.setMessage(String.valueOf(Math.random()));
        utx.commit();
    }

    protected void doGet(HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        try {

            this.initialize();
            this.meth1(); // NOTHING HAPPENS!
            this.meth2(); 
            // SET autocommit=0; 
            // UPDATE LOG SET message = '0.1523804781755964' WHERE (id = 1);
            // commit;
            // SET autocommit=1;

        } catch ... {
            ...
        }
        response.getWriter().write("test");
    }
}

WHY?为什么没有提交method1调用?

1 个答案:

答案 0 :(得分:2)

错误是典型错误:调用merge()后,传递的日志无法得到管理。仅管理返回的实例。 请尝试以下方法:

public void meth1() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
        utx.begin();
        Log newLog = em.merge(log);
        newLog.setMessage(String.valueOf(Math.random()));//we make the changes to the managed instance
        utx.commit();
    }