Announcement Announcement Module
Collapse
No announcement yet.
Why Always use ExtendedEntityManagerCreator? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why Always use ExtendedEntityManagerCreator?

    Dear All:

    I use follow code, but always get extended persistence context. and as spring's document say "Extended EntityManagers are not thread-safe" "In general, do not inject EXTENDED EntityManagers".

    Anybody can tell me why?

    spring2.1M3
    sun jdk 1.6.0_u1
    resinpro3.1.1
    openjpa0.9.7
    mysql5
    ========================
    @Repository
    public class ProfService {
    @PersistenceContext
    private EntityManager em;
    ========================

    applicationContext.xml
    ========================
    <bean id="dataSource"
    class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="java:comp/env/jdbc/prof"/>
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading .ReflectiveLoadTimeWeaver"/>
    </property>
    <property name="jpaVendorAdapter">
    <bean id="jpaAdapter"
    class="org.springframework.orm.jpa.vendor.OpenJpaV endorAdapter">
    <property name="database" value="MYSQL"/>
    <property name="showSql" value="true"/ >
    <property name="generateDdl" value="true"/>
    </bean>
    </property>
    </bean>
    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor"/>
    <bean class="org.springframework.dao.annotation.Persiste nceExceptionTranslationPostProcessor"/>

    <bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="cn.pconline.prof.service" />
    <context:component-scan base-package="cn.pconline.prof.validator" />
    ===================================


    thanks!

    xhchen

  • #2
    I have the same problem using hibernate-entitymanager-3.2.1.ga, spring-2.0.4 and WebLogic 9.2MP1.

    I use org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean and org.springframework.transaction.jta.WebLogicJtaTra nsactionManager.

    I believe Spring should be acting as the container from the JPA perspective, correctly scoping a SharedEntityManager for the duration of the underlying JTA transaction. However a new EntityManager is being used for each transactional method. From a code inspection, this appears to be due to the EntityManagerSynchronization.beforeCompletion() method being invoked for each of these transactional methods even though they are part of an overall JTA transaction. As seen below, this method removes the EntityManager if it thinks it created it (newEntityManager == true).

    Code:
    public void beforeCompletion() {
    	if (this.newEntityManager) {
    		TransactionSynchronizationManager.unbindResource(this.entityManagerFactory);
    		this.holderActive = false;
    		this.entityManagerHolder.getEntityManager().close();
    	}
    }
    That approach will only work when requesting an EntityManager in a scope that is nested within an existing scope.

    Shouldn't the
    Code:
    TransactionSynchronizationManager.unbindResource(this.entityManagerFactory);
    be left until the end of the JTA transaction?

    Why is beforeCompletion() being called for each transactional method instead of just once for the JTA transaction?

    Comment


    • #3
      jim_b_o, Thanks for your reply.

      In fact I am not care use EXTEND persistence context.
      I just want to know when I use

      OpenEntityManagerInViewFilter + extended persitence context, is there any thread-safe problem.

      I think with OpenEntityManagerInViewFilter every http request shall use the per-thread/request EntityManager, and after finish the request, the EntityManager be closed.
      There had no thread problem and extended persitence shall closed with the EntityManager closed.

      Is my point ok? any reply is welcome.
      Last edited by xhchen; Jun 26th, 2007, 02:04 AM.

      Comment

      Working...
      X