2 Replies Latest reply on Mar 8, 2011 11:46 AM by sathees

    multiple entity-transactions

      Hi,
      i did a lot of research here in the user forum about transactions, but no one seams to have the same problem. I don't know if it is a bug or just an architectural problem.


      The problem only occurs in test sources, because here I use the entity-transaction, whereas in productive I use ejb-transaction, which works normal.


      Initial situation: I have 2 jar modules, each one has it's own persistence.xml and components.xml, as both of them have their own entities and access to database (same database).


      Module 1 - component.xml:




      <persistence:entity-manager-factory name="coreService.entityManagerFactory" 
              class="org.jboss.seam.persistence.EntityManagerFactory" persistence-unit-name="CORE_SERVICE_HSQL_TEST_PERSISTENCE" precedence="10" />
            
          <transaction:entity-transaction  entity-manager="#{coreService.entityManager}" />  
                
          <persistence:managed-persistence-context name="coreService.entityManager" auto-create="true" precedence="10"
              entity-manager-factory="#{coreService.entityManagerFactory}"  />
          
          <component name="coreService.companyDao" class="com.core.service.dao.GenericDaoJpa" auto-create="true">
              <property name="type">com.core.service.domain.Company</property>
              <property name="entityManager">#{coreService.entityManager}</property>
          </component>
          
          <component name="coreService.userDao" class="com.core.service.dao.GenericDaoJpa" auto-create="true">
              <property name="type">com.core.service.domain.Users</property>
              <property name="entityManager">#{coreService.entityManager}</property>
          </component>



      Module 2 - component.xml:




      <persistence:entity-manager-factory name="coreWorkflow.entityManagerFactory"
              class="org.jboss.seam.persistence.EntityManagerFactory" persistence-unit-name="CORE_WORKFLOW_HSQL_TEST_PERSISTENCE" />
      
          <persistence:managed-persistence-context name="coreWorkflow.entityManager" auto-create="true" precedence="10"
              entity-manager-factory="#{coreWorkflow.entityManagerFactory}" />
              
         <transaction:entity-transaction name="coreWorkflow.transaction" entity-manager="#{coreWorkflow.entityManager}" />
          
          <!-- Test DAO for TestUser Entity -->
          <component name="coreWorkflow.testUserDao" class="com.core.service.dao.GenericDaoJpa" auto-create="true">
              <property name="type">com.core.workflow.domain.TestUser</property>
              <property name="entityManager">#{coreWorkflow.entityManager}</property>
          </component>




      As you see the Module 2 -component.xml has a named entity-transaction tag in it, which referes to the local coreWorkflow.entityManager. And here is the problem:


      If I give both transaction:entity-transaction tags a name an error at @Transactional method processesing - in class Work - occurs, because the class Work tries to get the instance of a transaction by type and not by name. So in the second module it always tries to get a transaction instance with the unnamed entity-transaction declaration from Module 1. This is the coreService.entityManager.


      Can anybody tell me how to avoid that the class Work takes the default transaction (getInstance(Transaction.class,...)). It should take the named entity-transaction declaration from module2 - component.xml, where entity-manager is coreWorkflow.entityManager.


      Thanks in advance,


      Tony




        • 1. Re: multiple entity-transactions

          I realized that having multiple PersistenceUnits, EntityManagers and Transactions is not possible. Due to some errors and due to understanding.


          But having multiple modules with Entities in it, leads to TestNG/Unit Test errors: Unknown entity, as only the entities from the module, which contains the persistence.xml is scanned.


          To overcome this shortcoming I wrote my own EntityManagerFactory, which reads the existing persistence.xml and adds all Entities from classpath to it.


          With this solution it is possible to test multiple modules with entities in it.


          If someone needs more information - I could post my implementations!


          br, Tony

          • 2. Re: multiple entity-transactions
            sathees

            Hi Tony,


            I am very new to the Seam Framwork and I have to use multiple database schemas to retrieve and update the tables.  I didn't get how u did this.  Can you please explain or give me some sample code so I could try.


            I really appreciate your help.


            Sathees