Announcement Announcement Module
Collapse
No announcement yet.
Spring , jta issue with Shared Entity Manager Creator.. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring , jta issue with Shared Entity Manager Creator..

    Greetings Spring Users,
    Having issues with LocalContainerEntityManagerFactoryBean when running jta transactions with spring in a java ee (jboss container). The SharedEntityManagerCreator is creating a new entity manager each time the entity manager is used ,instead of one for the transaction causing the underlying implementation(Eclipselink in our case) to give an error that the entity is not attached. Following is our spring set up
    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="ourDataSource" /> // this is a basic datasource
    <property name="persistenceUnitName" value="PersistenceEl" /> // this is defined in the persistence.xml file with type ="jta"
    <property name="jpaDialect" ref="eclipseLinkDialect" />
    <property name="persistenceXmlLocation"
    value="classpath:META-INF/persistence-eclipse.xml" />
    <property name="jpaVendorAdapter">
    <bean
    class="org.springframework.orm.jpa.vendor.EclipseL inkJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="generateDdl" value="false" />
    </bean>
    </property>

    <property name="jpaPropertyMap">
    <map>
    <entry key="eclipselink.target-server" value="JBOSS"/>
    <entry key="eclipselink.target-database"value="Oracle"/>
    <entry key="eclipselink.persistence-context.flush-mode" value="AUTO"/>
    <entry key="eclipselink.weaving" value="false" />
    <entry key="eclipselink.orm.throw.exceptions" value="true" />
    </map>
    </property>
    </bean>
    <bean id="eclipseLinkDialect" class="org.springframework.orm.jpa.vendor.EclipseL inkJpaDialect" />
    <tx:jta-transaction-manager />

    Following is how it gets injected . The entityManager is just injected ..
    public BaseEclipselinkDAO
    {
    protected EntityManager getEntityManager() throws DAOException
    {
    // The entityManager that is injected is getting a new one everytime causing delete to fails.
    return entityManager;
    }
    @PersistenceContext(unitName="PersistenceEl")
    private EntityManager entityManager;
    }

    This is how we use it in another dao (inherited from the above one)
    public class UserDAO{
    public <T extends PersistableObject> void deleteObject(T entity)
    throws DAOException
    {
    try
    {
    EntityManager em=getEntityManager();
    if (em.contains(entity)) {
    System.out.println("Succeeding contains");
    em.remove(entity);
    } else {
    entity = (T)em.find(entity.getClass(), entity.getId());
    if (em.contains(entity))
    {
    System.out.println("Succeeding contains");
    }
    em.remove(entity);
    }
    }
    }

    we get the following error
    17:57:41,817 INFO [STDOUT]:152 17:57:41,817 TRACE
    [ClassPathXmlApplicationContext]:301 Publishing event in
    17:57:41,851 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231Creating new EntityManager for shared EntityManager invocation
    17:57:41,855 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:42,392 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:42,398 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:42,405 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:42,410 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:42,480 INFO [STDOUT]:152 17:57:42,479 TRACE
    [ClassPathXmlApplicationContext]:301 Publishing event in
    ApplicationContext 'pmabWebBeanFactory':
    org.springframework.security.access.event.PublicIn vocationEvent[source=FilterInvocation:
    URL: /secured/rest/roles?__ts=1342047462452]
    17:57:42,485 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:42,490 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:50,493 INFO [STDOUT]:152 17:57:50,493 TRACE
    [ClassPathXmlApplicationContext]:301 Publishing event in
    ApplicationContext 'pmabWebBeanFactory':
    org.springframework.security.access.event.PublicIn vocationEvent[source=FilterInvocation:
    URL: /secured/rest/users/1320/5/details]
    17:57:50,500 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:50,502 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:50,557 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:50,563 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:50,564 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:50,629 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:50,630 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:50,642 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:50,647 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:53,932 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:53,947 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:55,107 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:55,127 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:55,602 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:55,618 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:58,393 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:58,403 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:57:58,410 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    17:57:58,419 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA EntityManager
    17:58:00,842 SEVERE [MappedExceptions]:17 400 BAD REQUEST
    java.lang.IllegalArgumentException: Entity must be managed to call
    remove: com.dst.hps.pmab.security.bom.helpers.UserRole@349 d, try
    merging the detached and try the remove again.
    at org.eclipse.persistence.internal.sessions.UnitOfWo rkImpl.performRemove(UnitOfWorkImpl.java:3559)
    at org.eclipse.persistence.internal.jpa.EntityManager Impl.remove(EntityManagerImpl.java:518)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.ExtendedEntityManagerC reator$ExtendedEntityManagerInvocationHandler.invo ke(ExtendedEntityManagerCreator.java:365)
    at $Proxy641.remove(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:240)
    at $Proxy641.remove(Unknown Source)
    at com.dst.hps.pmab.common.dao.eclipselink.EclipseLin kDAO.deleteObject(EclipseLinkDAO.java:192)
    Is there anything wrong with the above configuration which forces the SharedentityManaged to create a new EntityManager every time we use it , so the find and deletes are getting different entity managers? Also when writing to the eclipselink group this was one suggestion that i got .. See link below for my original transcript
    http://dev.eclipse.org/mhonarc/lists.../msg07244.html

    It might be some wrong configuration that I have with spring that is the root cause or it might be that i need to use a different Container Bean , but i am not able to spot it .
    Please help...

    Cheers
    Vaidya

  • #2
    Can someone please help ? I am struck with this issue. Any pointers would be appreciated !!!

    Comment


    • #3
      Just saw this post. I had a similar problem. Could it be you relied on managing TX boundaries without using Spring? I.e. just via the JTA transaction manager, tyically using the UserTransaction object? Spring seems to require to use its own transaction demarcation means as only those initialize the thread-based keeping of the entity manager for example. So, in other words, use can use Spring's @Transactional (or the XML version of it), the OEIV filter etc, and you need to declare something like <tx:jta-transaction-manager/> in your app context. Just using JTA and letting Spring figure out the rest is unfortunately not enough :-(

      Comment

      Working...
      X