The problem is that the transaction borders are defined on the EJB layer, not Servlet, meaning if you would do that in the EJB method, that would be committed to the DB after your EJB method returns. Also bear in mind, that the Foo
instance in your servlet is not managed anymore, which means that its changes are not tracked anymore.
On the other side, it would be a bad practice to implement any DB operations/business logic in your servlet.
To make it shorter: make another method save(Fao fao)
in your DAO service with the following code:
em.merge(fao);