1

I'm successfully using JPA (Jboss 4.2 / Hibernate) within EJB 2.1 Stateless, retrieving Entity Manager Factory from Jndi (published using persistence.xml jboss.entity.manager.factory.jndi.name property).

When switching to Spring Entity Manager injection, in order to get it work in websphere, I'm getting "java.lang.IllegalArgumentException : Removing a detached instance" when performing:

    MyEntity entity = aDao.read(pKey);
    aDao.delete(entity);

This make me think that Spring is not reusing the JTA transaction and is creating one transaction to read the entity, and another to delete the entity.

My spring configuration is:

    <jee:jndi-lookup id="dataSource" resource-ref="true" jndi-name="java:OracleDSxa" />

  <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

<tx:jta-transaction-manager />
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="persistenceUnitName" value="mypersistenceunit" />
    </bean>

Is there anything wrong with the Spring configuration ? When I instanciate the EntityManagerFactory myself as a singleton it is working, is it an appropriate solution and why isn't it working with the Spring one ?

I tried without (re)defining the entity manager factory, but then injection fail and I get No bean named 'mypersistenceunit'

Thank you for any pointer

Edit 1: Here is my persistence xml configuration for hibernate:

        <property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory" />
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
        <property name="hibernate.connection.release_mode" value="auto" />
        <property name="hibernate.current_session_context_class" value="jta" />
        <property name="hibernate.transaction.flush_before_completion" value="true" />
        <property name="hibernate.transaction.auto_close_session" value="false" />

Edit 2: Transaction is managed by the container (CMB EJB 2.1 Stateless), which is working under Jboss using Entity Manager from Jndi

Edit 3: Making usage of jndi resource name referenced in the EJB 2.1 Session, jee:jndi-lookup id="dataSource" resource-ref="true" jndi-name="jdbc/myDataSourceRef" , did not solve the problem.

Edit 4: Hibernate version is 3.2.1.ga, provided by Jboss as JPA implementation

snowflake
  • 1,750
  • 2
  • 17
  • 40
  • 1
    How are your transactions managed? If you use Spring transaction manager, you should use Spring-specific transaction demarcation (`@Transactional`, etc), right? – axtavt Jan 07 '11 at 15:30
  • Thank you for your interest in this problem. Transactions are managed by the container (Container Managed Beans). I'm not using @Transactional annotation since I'd like to be spring agnotisc (except eventually in DAOs implementation, but not within the service). If it is the only working solution I will consider it. – snowflake Jan 10 '11 at 08:31

0 Answers0