In Play! if you call this:
void method()
{
User u = User();
u.name = "bob";
u.save();
while(true){/* endless loop */}
}
Nothing will actually be saved into the db (The Play! class needs to get the hand back to flush the saves.)
How do I have to proceed in order to either force a flush or to make it automatically flush at save ?
The reason why your changes are not visible in the database, is that the transaction is not yet commited and so other transactions can't see your changes (at least in a good database like PostgreSQL, Oracle, MSSQL, DB2). To make your changes seen, you'll have to commit your transaction before it enters the infinite loop, like this:
If you want to access your database inside the infinite loop or after it (if you have a break condition), you'll have to begin a new transaction or you'll get exceptions from hibernate. Do this like this
It's important that you either commit or rollback the transaction in the loop, if you start it there, as else you'll run into problems with too many open transactions soon.
As already said by Andrei Bodnarescu, you can use
JPA.em().flush()
orUser.em().flush()
to flush persistence context explicitly.Note, however, that doing so won't make the saved entity immediately available to other transactions, since the current transaction should be committed first. You can commit the current transaction as
JPA.em().getTransaction().commit()
.Try
If I'm not mistaken, the default Play! model holds a refference to the EntityManager which can be accessed like that.