0

I read about about Persistence Context but not able to get a clear picture about my doubts which are as follows.
1. I have a DAO class which as a @PersistenceContext(unitName="") private EntityManager entityManager and from my Service method i am starting the transaction(Spring Managed) using @Transactional(propagation = Propagation.REQUIRED). I understand here is every time this Service method is called, a Transaction will start and when it reaches to DAO class it will use the same Transaction but for every EntityManager operation it look for Active PersistenceContext and create it as required. Is this approach correct and thread safe? So, if a new thread starts the same service method then a new Transaction and a persistence Context will be created and flushed away when the method ends?
2. I have multiple WARS which need to interact with database so I am sharing the EntityManagerFactory using Spring Shared contexts. I am having all the Hibernate related configurations at a common place and in every WAR i am specifying where transactionManager will be shared. Is it right?
Please clarify my doubts and your comments are valued. Thanks in advance.

Art
  • 414
  • 7
  • 24
  • if all applications share one session, then you can share entities from application to application by set the entity into session. Otherwise there is no way for you to get access to an entity outside scope of an application. Although you might package all applications in one EAR and have a `persistence.xml` for the EAR, then yes you can share entities across applications. Same concept for Threads. Each application should use own thread, otherwise it is a security vulnerability. – pms Dec 12 '14 at 20:24
  • @pmp So to clarify more on this, I have a Spring `SharedContext` where hibernate Configurations will be initialized which will be loaded once and shared across different WAR, and every WAR Transactions will be managed by Spring by `@Transactional`. Let me know where i am missing, also can you share some links which makes your statement more clear.
    Thanks for the reply.
    – Art Dec 12 '14 at 21:01
  • I am not using `pesistence.xml` , everything is annotation Driven and i am keeping all the entities in one jar which will be present in the classpath. – Art Dec 12 '14 at 21:07
  • look at this: http://stackoverflow.com/questions/16065773/how-to-best-share-entity-classes-daos-across-multiple-pus and http://stackoverflow.com/questions/16789292/i-want-to-share-a-jpa-annotated-entity-model-over-a-several-jee6-web-projects – pms Dec 12 '14 at 21:59

0 Answers0