i was told to inject the entitymanager like this @ApplicationScoped public class EntityManagerProducer { @PersistenceUnit(unitName = "myDb") private EntityManagerFactory entityManagerFactory;
@Produces @Default @RequestScoped public EntityManager create() { return this.entityManagerFactory.createEntityManager(); } public void dispose(@Disposes @Default EntityManager entityManager) { if (entityManager.isOpen()) { entityManager.close(); } } } injecting into your example like: @ApplicationScoped public class MyRepo1{ @Inject private EntityManager em; public List<Something> loadSomeStuff(){ //Non transactional em.createQuery(....).getResultList(); } } also with JSF i alway found @PostConstruct a bit dodgy. way better to add <f:metadata> <f:viewAction action="#{myViewBean.onload}" /> </f:metadata> then in your bean you have an onload function (no annotation necessary) public String onload() { someVar1= myRepo1.loadSomeStuff(); someVar2 = myRepo2.loadSomeStuff(); return null; //you can call action here if necessary } On 13/02/2018 09:42, cocorossello wrote:
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