Reputation: 1145
I'm trying to understand jpa transaction work.
Say I have following 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>
and following test code with sniffering 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");
}
}
WHY?! Why there is no commit on method1 invocation?
Upvotes: 0
Views: 376
Reputation: 19002
The error is a typical one: the passed log does not get managed after calling merge()
. Only the returned instance is managed.
Try the following:
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();
}
Upvotes: 2