Note that there are some explanatory texts on larger screens.

plurals
  1. PONo EntityManager bound to this thread JPA
    primarykey
    data
    text
    <p>I have a POJO(Myclass in this example) which I persist/update/delete in my app. </p> <p>I detect changes to that object using a listener class and then in listener class I save the changes to another table.</p> <pre><code>Here is my class (dummy example) : EntityListeners({MyListener.class}) class MyClass { String name; String surname; /*...getters...setter..etc */ public void save() { JPA.em().persist(this); return this; } public void update() { JPA.em().merge(this); } } class MyListener { @preUpdate public void preUpdate() { /*...some logic to save the changes irrelevant*/ someAuditObj.createWithData(.......); someAuditObj.save(); } } </code></pre> <p>I'm building my web app using <code>play framework v2.1.3</code>, and all this was working great, I was really happy the way it works.</p> <p>Today I updated play framework to a newer version <code>v2.2.1</code>. And the for some reason when instance of <code>MyClass</code> changes and the listener picks up the change and it tries to save the changes the transaction fails and I find this in the log :</p> <pre><code>Caused by: java.lang.RuntimeException: No EntityManager bound to this thread </code></pre> <p>So it took me a while to figure out that for some reason transaction is not propagated to listener and then I tried something to fix it (Listener class):</p> <pre><code>@preUpdate public void preUpdate() { /*...some logic to save the changes irrelevant*/ JPA.withTransaction(new F.Callback0() { @Override public void invoke() throws Throwable { someAuditObj.createWithData(.......); someAuditObj.save(); }); } </code></pre> <p>So this fixes it, and it works like before without issues.</p> <p>My question(s) is :</p> <ol> <li>Why did it work before without meddling manually with transactions with earlier version of play framework</li> <li>Is there a better way of achieving the same thing more elegantly (I'm not sure that is the right word for it)?</li> </ol> <p><strong>Update</strong> </p> <p>This is my controller method :</p> <pre><code>@Transactional public Result updateName(Long id, String name){ MyClass c = MyClass.findById(id); c.setName(name); c.update(); return ok(); } </code></pre> <p>So transaction should be propagated to all methods right? but why not to listener as well? </p> <p>My estimate was this :</p> <p>if a method has @Transactional annotation then all calls that happens inside should be in a transaction?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload