1

I am using threads in JAVA to do a few of operations over the same entity. The problem appears when I do the persist method. The way that I do is the next:

@Transactional
    private void persist(){
        synchronized(this){
            JPA.em().getTransaction().begin();
            <nameObject>.save();
            JPA.em().getTransaction().commit();
        }
    }

where nameObject is the name of the object to persist. The error that show me is:

Exception in thread "Thread-38" javax.persistence.PersistenceException: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:845)
    at play.db.jpa.JPABase._save(JPABase.java:31)
    at play.db.jpa.GenericModel.save(GenericModel.java:204)
    at models.invoicing.PreInvoiceThread.persist(PreInvoiceThread.java:290)
    at models.invoicing.PreInvoiceThread.run(PreInvoiceThread.java:273)
    at java.lang.Thread.run(Thread.java:745)

I have try make a lockoptimistic over the Object without results.

1 Answer 1

1

You marked the method as Transactional and at the same time you are beginning transaction before save, which is causing two sessions to open Change the code to,

@Transactional
private void persist(){
    synchronized(this){
        <nameObject>.save();
    }
}

or

private void persist(){
    synchronized(this){
        JPA.em().getTransaction().begin();
        <nameObject>.save();
        JPA.em().getTransaction().commit();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

What is JPA in the second example? What import do I need?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.