Announcement Announcement Module
No announcement yet.
Issues in using LocalContainerEntityManagerFactoryBean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issues in using LocalContainerEntityManagerFactoryBean

    I am having a strange problem deleting entities and I am thinking that this is because of wrong spring configuration / bug and i am not able to exactly point what it is . I am able to do reads , but whenever i try remove i get an error..

    Following is my set up.. I have a spring based transaction management delegating to jboss's jta. Following is my spring configuration

    <context:annotation-config />
    <context:component-scan base-package="com.blah.blah">
    <context:exclude-filter type="regex"
    expression="com.blah.blah.batch.*" />

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="pmabDataSource" />
    <property name="persistenceUnitName" value="MABPersistenceEl" />
    <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" />

    <property name="jpaPropertyMap">
    <entry key="" value="JBOSS" />
    <entry key="" value="Oracle" />
    <entry key="eclipselink.persistence-context.flush-mode" value="AUTO" />
    <entry key="eclipselink.jdbc.native-sql" value="false" />
    <entry key="eclipselink.weaving" value="false" />
    <entry key="eclipselink.logging.level" value="FINEST" />
    <entry key="eclipselink.logging.parameters" value="true" />
    <entry key="eclipselink.logging.exceptions" value="true" />
    <entry key="eclipselink.orm.throw.exceptions" value="true" />

    <bean id="eclipseLinkDialect" class="org.springframework.orm.jpa.vendor.EclipseL inkJpaDialect" />

    <tx:jta-transaction-manager />

    persistence file is as follows

    <persistence version="1.0"
    xmlns="" xmlns:xsi=""

    <persistence-unit name="MABPersistenceEl"
    <provider>org.eclipse.persistence.jpa.PersistenceP rovider</provider>
    <!-- ,,,, other things ommited for brevity/> -->

    My basedao gets injected with the EntityManager like this

    @PersistenceContext(unitName = "MABPersistenceEl")
    protected EntityManager entityManager ;

    somewhere down in my derived dao i have the following

    UserRole newRole=entityManager.find(UserRole.class, tId()) ;

    I get the following
    09:53:00,386 DEBUG[SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    09:53:00,413 DEBUG [EntityManagerFactoryUtils]:328 Closing JPAEntityManager
    09:53:04,119 DEBUG[SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    09:53:04,146 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA
    09:53:05,760 DEBUG
    [SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler]:231
    Creating new EntityManager for shared EntityManager invocation
    09:53:05,781 DEBUG [EntityManagerFactoryUtils]:328 Closing JPA

    That is springs EntityManager creator is creating an entityManager each time it is accessed, instead of returning the one for the transaction. This is causing EL libraries to go berserk.
    java.lang.IllegalArgumentException: Entity must be managed to call
    remove: [email protected], try merging the detached and try the remove
    org.eclipse.persistence.internal.sessions.UnitOfWo rkImpl.performRemove(
    org.eclipse.persistence.internal.jpa.EntityManager Impl.remove(

    How do i configure the springs entity manager creator to give me back the entity manager for the transaction instead of creating one each time ?


  • #2
    Please use [ code][/code ] tags when posting code that way it remains readable... Also I nowhere see the thing that drives your transaction. You have a transaction manager and that appears to be it, no tx:annotation-driven or aop:config that starts/stops your transaction.


    • #3
      I did make the following change and it now works..
         <tx:jta-transaction-manager />
          <!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
      	<tx:advice id="pmabTxAdvice" transaction-manager="transactionManager">
      		<!-- the transactional semantics... -->
      			<!-- all methods starting with 'get' are read-only -->
      			<!-- other methods use the default transaction settings (see below) -->
      	      <tx:method name="*" propagation="REQUIRED" read-only="false"/>
      		<aop:pointcut id="serviceOperation"
      			expression="execution(**.*.*(..))" />
      		<aop:advisor advice-ref="pmabTxAdvice" pointcut-ref="serviceOperation" />
      Thanx for the suggestion.. Do you see any issues with the above configuration ? I am still wondering whether i am able to get the same transaction throughout . In other words the jta transaction should be started by the ejb layer and the same transaction should go down to the daos .. I dont think that this is happening because when a rollback happens it is not happening properly.. Can you spot any problems with my configuration above ?
      Last edited by vaidya1973; Aug 20th, 2012, 11:58 AM.


      • #4
        I am struck with this issue? Can someone please help or point me to some configuration with jta and eclipselink using spring please?


        • #5
          What are you stuck with? Last post indicates that it works but now apparently it doesn't?!

          You are using jboss so why not simply let jboss do the entity manager construction instead of spring. Simply create the persistence.xml, do a jndi lookup to get the entitymanager (instead of the LCEMF) and leave the rest as is. You also might want to read this


          • #6
            We want to use the org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean for the jboss's entityManagerFactory , since it gives better configurability . Also by using the LCEMF in both the test and in server configuration it allows us to have a consistent spring set up.

            The problem that we are having is that instead of having one transaction which starts at the boundary of the ejb method we are having a bunch of them and so when a rollback is done we are not having the whole operation rolled back. For eg. within the ejb method A we do B and C and when we get an error we would like for us to rollback both B and C , now only C is rolled back .. We had problems creating the transaction which we resolved by using the aop pointcuts but its still not running under 1 transaction..


            • #7
              If you get multiple transactions either your pointcut is wrong or there is still something missing in your eclipselink/jta integration part but I know to little of eclipselink to mention something about that.


              • #8
                The issue was resolved by using the container provided datasource instead of the BasicManagedDataSource that we used. The other configurations helped too but the final icing was using the container provided datasource which we did a jndi lookup on..