Announcement Announcement Module
Collapse
No announcement yet.
Issues in using LocalContainerEntityManagerFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.*" />
    </context:component-scan>

    <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" />
    </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.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" />
    </map>
    </property>
    </bean>

    <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="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="MABPersistenceEl"
    transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceP rovider</provider>
    <jta-data-source>pmabDataSource</jta-data-source>
    <!-- ,,,, other things ommited for brevity/> -->
    </persistence-unit>
    </persistence>


    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,urole.ge tId()) ;
    entityManager.remove(newRole);

    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
    EntityManager
    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
    EntityManager

    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: UserRole@419d, 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)

    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 ?

    Cheers
    Vaidya

  • #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.

    Comment


    • #3
      I did make the following change and it now works..
      Code:
         <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... -->
      		<tx:attributes>
      			<!-- 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"/>
      		</tx:attributes>
      	</tx:advice>
      
      	<aop:config>
      		<aop:pointcut id="serviceOperation"
      			expression="execution(* com.foo.foobar.abc.*.*.*(..))" />
      		<aop:advisor advice-ref="pmabTxAdvice" pointcut-ref="serviceOperation" />
      	</aop:config>
      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, 10:58 AM.

      Comment


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

        Comment


        • #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

          Comment


          • #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..

            Comment


            • #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.

              Comment


              • #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..

                Comment

                Working...
                X