Results 1 to 2 of 2

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

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Default 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. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •