Announcement Announcement Module
Collapse
No announcement yet.
Is it thead safe to make query to the same EntityManager from multiple threads? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it thead safe to make query to the same EntityManager from multiple threads?

    Hello,

    Maybe it's not Spring specific question, but as it gains a lot of attention and a lot of guru around it, so my question could be answered here without confusion.

    I'm using Spring and Hibernate in my DAO layer, below is my a DAO code snippets.

    @Service("SomeDao")
    @Repository
    public class SomeDaoImpl implements SomeDao {

    @PersistenceContext
    private EntityManager em;

    public Collection loadSomething(String query) {
    Query query = em.createQuery(...);
    return query.getResultList();
    }
    }

    Per http://static.springsource.org/sprin...m-jpa-straight, the EntityManager is bound to a transaction, and thus it's thread safe, but in my application the 'loadSomething' can be called by multiple threads, though it's read-only, I'm not sure if it's thead safe or not having any other issues?

    Thanks for your advice.

  • #2
    Hello

    use code tags the next time


    About this

    the EntityManager is bound to a transaction, and thus it's thread safe,
    In your same provided link Implementing DAOs based on plain JPA (BTW 2.5.x version) said

    While EntityManagerFactory instances are thread-safe, EntityManager instances are not

    Comment

    Working...
    X