JPA транзакция фиксирует странное поведение

Я пытаюсь понять работу транзакций 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>

и следующий тестовый код с анализатором MySQL Server:

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");
    }
}

ЗАЧЕМ?! Почему нет фиксации при вызове method1?

1 ответ

Решение

Ошибка типичная: пропущенный журнал не управляется после вызова 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();
    }
Другие вопросы по тегам