I do some experiments with Glassfish and Postgres, lets consider the next code snippet :
the theory sustains that an entity can become managed by using the perist method. If is managed it content will be synchronized with the database when the transaction ends or when the corresponding query is used. In the previous code snippet the method doStuff get it own transaction, this transaction end once with the method, that means the entity content must by synchronized with the database.
Well, I just try this and this it seams to be wrong. To get the changes in the database I need extra to trigger the flush method.
My question is why do I need to call flush ?
On line 5 in your code you have the TransactionAttributeType as REQUIRED_NEW, I think the value your looking for is REQUIRES_NEW. I'm not sure if that would mess with the compilation of your code or not, but if it's running, your transaction could be defaulting to REQUIRED if the dodgy annotation is ignored (I'm not 100% sure what happens in that scenario).
If that's the case, and your doStuff() method is called from an existing transaction (e.g. called from another EJB bean), then is it possible your entity won't be persisted until the enclosing transaction commits?
Yes you are right I type the code snippet and I don't have a checker for the java syntax, the REQUIRED_NEW is REQUIRES_NEW.
Please see the find and persist in the same transaction post, this post describes the problem better.