tomee closes the EM after the transaction, if you need to call clear() it means the close doesnt release the data in your provider - can be a bug.
In terms of pattern you need to clear it when you don't need it anymore. A way to do it is a Synchronization and register it in the transaction synchronization registry - what we do by default. Now if you don't have any transaction it is after your last usage. The repo being not transactional and application scoped can justify a leak, if you put it request scoped (view scoped if you can) or @Transactional it should get fixed. Romain Manni-Bucau @rmannibucau <https://twitter.com/rmannibucau> | Blog <https://rmannibucau.metawerx.net/> | Old Blog <http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> | LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book <https://www.packtpub.com/application-development/java-ee-8-high-performance> 2018-02-13 9:42 GMT+01:00 cocorossello <cocorosse...@gmail.com>: > Hi, > > Calling entityManager.clear() seems the best approach since most of the > queries are not executed inside a transaction. But I have no idea on where > I > should use it or what would be a good implementation pattern. > > I mean, let's say I have a @ViewScoped with a couple of injected > @ApplicationScoped. > > @ViewScoped > public class MyViewBean{ > > @Inject > private MyRepo1 myRepo1; > > @Inject > private MyRepo2 myRepo2; > > @PostConstruct > void init(){ > someVar1= myRepo1.loadSomeStuff(); > someVar2 = myRepo2.loadSomeStuff(); > } > .. > } > > @ApplicationScoped > public class MyRepo1{ > @PersistenceContext(unitName = "myUnit") > private EntityManager entityManager; > > public List<Something> loadSomeStuff(){ //Non transactional > entityManager.createQuery(....).getResultList(); > } > } > > } > > So, should I call entityManager.clear() after all non transactional queries > (or at least some of them)? Can I do it in some web filter? > > This seems pretty basic stuff but I can't get it right... > > > > -- > Sent from: http://tomee-openejb.979440.n4.nabble.com/TomEE-Users- > f979441.html >