Announcement Announcement Module
Collapse
No announcement yet.
@PersistenceContext: When the Extended-EntityManager gets closed?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @PersistenceContext: When the Extended-EntityManager gets closed??

    Hello everybody,

    If an EntityManager gets injected into a dao object using the @PersistenceContext annotation, this EM is, by default, a transaction-EM and, hence, it gets closed at the end of the transaction. However, if we want it to be an extended-EM:

    class JPABookDAO implements BookDAO{

    @PersistenceContext(unitName="alexandria",type=Per sistenceContextType.EXTENDED)
    private EntityManager em;
    ....
    }
    When will it be closed?

    I have closed it manually, in an operation of the same JPABookDAO class:

    public void close(){
    try{
    em.flush();
    }
    catch(Exception e){
    ...
    }
    finally{
    em.close();
    }
    }

    But then, the last modifications on the EM (before closing it, I mean) do not synchronize to the database. If I ommit this em.close() all works properly.

    Which is the reason? and when the extended-EM gets closed?

    Thank you very much....
    Josepma

  • #2
    Huhhh... Very romantic issue.

    I faced the same, and finally resolved the same.

    @Transactional(readOnly=true, timeout=3000)
    public List<EpBizzPartner> filter(BizzPartner bizzPartner) {

    ...
    Session session = (Session) entityManager.getDelegate();
    org.hibernate.Transaction transaction = session.getTransaction();
    transaction.begin();
    Criteria criteria = session.createCriteria(EpBizzPartner.class);
    criteria.add(Example.create(bizzPartner.getEpBizzP artner()));

    List list = criteria.list();

    // session.flush();
    // session.close();
    transaction.commit();

    entityManager.flush();

    return list;
    }


    The issue is due to @Transactional was missing.

    You can check, without changing anything wwith the configuration.

    Its working for me, Hope the same at your desk.

    Comment

    Working...
    X