6 Replies Latest reply on Jul 11, 2006 2:14 AM by grdzeli_kaci

    transactions and distributed transactions in ejb3

      fierst of all sorry for my english :)
      1.I've jboss-4.0.4.GA configured with EJB3 and it was working fine before i deside working with transaction. :)
      i have one entity bean (for example Student) and one stateless session bean like this :

      TransactionManagement(value = TransactionManagementType.BEAN)
      @Remote(StudentFasade.class)
      public @Stateless
      class StudentFasadeBean implements StudentFasade {
       @PersistenceContext
       private EntityManager manager;
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       public Long addStudent(Student stbean)
       throws MagtiException {
       try {
       manager.getTransaction().begin();
       manager.persist(_student);
       manager.getTransaction().commit();
       return _student.getId();
       } catch (Exception e) {
       manager.getTransaction().rollback();
       }
       }
      }
      
      


      and this is my persistance.xml file :

      <?xml version="1.0" encoding="UTF-8" ?>
      <persistence 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"
       version="1.0">
       <persistence-unit name="Students Registration" transaction-type="JTA">
       <jta-data-source>java:/XAOracleDS</jta-data-source>
       <class>com.magti.businesslayer.ejb3entity.Student</class>
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
       <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
       <property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.9.200.177:1521:devnbtwo"/>
       <property name="hibernate.connection.username" value="paata"/>
       <property name="hibernate.connection.password" value="1982226"/>
       <property name="hibernate.default_schema" value="PAATA"/>
       </properties>
       </persistence-unit>
      </persistence>
      


      and in client side i get error like this:

      Exception in thread "main" java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
       at org.jboss.ejb3.entity.TransactionScopedEntityManager.getTransaction(TransactionScopedEntityManager.java:219)
      
      



      and one other question :) how i can workig with distributed transactions in ejb3 (i mean that bean insert two different database and i want manage both transaction of them)

      thanks .....




        • 1. Re: transactions and distributed transactions in ejb3
          weston.price

          Hi, your English is just fine!

          The issue is you are trying to acquire a transaction in a component that manages transactions for you.

          Remove the

          
          manager.getTransaction().begin();
          manager.getTransaction().commit();
          
          


          and the associated rollback.

          For the distributed (multi DB) transaction you just need two XA datasources. Please see

          http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossJCA

          to learn how to set up XA datasources.





          • 2. Re: transactions and distributed transactions in ejb3

            thanks ...
            i will try it .

            • 3. Re: transactions and distributed transactions in ejb3


              weston.price@jboss.com

              did u mean "JBoss Transactions API" named ArjunaTA ?


              i removed this code from my program

              manager.getTransaction().begin();
              manager.getTransaction().commit();
              manager.getTransaction().rollback();
              

              but i get en error again :(
              error
              javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
              23:19:47,281 ERROR [STDERR] at org.jboss.ejb3.entity.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:149)
              23:19:47,281 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:174)
              


              my bean looks like :


              import [...]
              @TransactionManagement(value = TransactionManagementType.BEAN)
              @Remote(StudentFasade.class)
              public @Stateless
              class StudentFasadeBean implements StudentFasade {
              
               @PersistenceContext
               private EntityManager manager;
              
               @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
               public Long addStudent(Student stbean)
               throws MagtiException {
               try {
               System.out.println("================= Adding Student =====================");
               manager.persist(stbean);
               return _student.getId();
              
               } catch (Exception e) {
               e.printStackTrace();
               return 0L;
               }
               }
              }
              
              
              


              • 4. Re: transactions and distributed transactions in ejb3
                epbernard

                remove @TransactionManagement(value = TransactionManagementType.BEAN)

                in most case you don't want to handle tx management yourself

                • 5. Re: transactions and distributed transactions in ejb3

                  ok thanks u're rigth .... it works.... but
                  what can i do when i wont work with multiple database ?
                  i think that i need to xml datasourse file (for example : oracle-xa-ds.xml and postgres-ds.xml) both of them configured corectly :
                  oracle ds file :

                  <datasources>
                   <xa-datasource>
                   <jndi-name>XAOracleDS</jndi-name>
                   <track-connection-by-tx/>
                   <isSameRM-override-value>false</isSameRM-override-value> <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
                   <xa-datasource-property name="URL">jdbc:oracle:thin:@192.9.200.177:1521:devnbtwo</xa-datasource-property>
                   <xa-datasource-property name="User">paata</xa-datasource-property>
                   <xa-datasource-property name="Password">1982226</xa-datasource-property> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
                   <no-tx-separate-pools/>
                   <metadata>
                   <type-mapping>Oracle9i</type-mapping>
                   </metadata>
                   </xa-datasource>
                   <mbean code="org.jboss.resource.adapter.jdbc.vendor.OracleXAExceptionFormatter"
                   name="jboss.jca:service=OracleXAExceptionFormatter">
                   <depends optional-attribute-name="TransactionManagerService">jboss:service=TransactionManager</depends>
                   </mbean>
                  </datasources>
                  

                  and postgres ds file:

                  <datasources>
                   <local-tx-datasource>
                   <jndi-name>PostgresDS</jndi-name>
                   <connection-url>jdbc:postgresql://localhost:5432/Test</connection-url>
                   <driver-class>org.postgresql.Driver</driver-class>
                   <user-name>postgres</user-name>
                   <password>1982226</password>
                   <new-connection-sql>select 1</new-connection-sql>
                   <metadata>
                   <type-mapping>PostgreSQL 7.2</type-mapping>
                   </metadata>
                   </local-tx-datasource>
                  </datasources>
                  


                  and i want do operation wich inserts into one db and then into second db in one transaction ....
                  how i can do it ? :( plz F1 :)

                  weston.price@jboss.com told me about JBossJCA
                  but i didn't find any sample about it ...
                  is there any simple example about distributed transactions ...




                  • 6. Re: transactions and distributed transactions in ejb3

                    i can't resolve this problem again ...
                    could anybody help me ?