4 Replies Latest reply on Aug 8, 2006 9:24 AM by paata lominadze

    Simple EJB3 Transaction Question

    paata lominadze Master

      hi all,
      i hava some problems with transactions again:
      problem is that i want to manage them by myself not like this :
      Session Bean

      @Remote(UserManager.class)
      public @Stateless
      class UserManagerBean implements UserManager {
      
       @PersistenceContext(unitName = "Oracle")
       private EntityManager oracleManager;
      
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public UserRecord login(User userObject,UserContext context) throws MagtiException {
       try {
       oracleManager.persist(userObject);
       } catch (Exception e) {
       e1.printStackTrace();
      }
      }
      



      then i did some changes :
      I Version by some EJB3 Documentation
      @Remote(UserManager.class)
      public @Stateless
      class UserManagerBean implements UserManager {
      
       @Resource public UserTransaction utx;
       @Resource public EntityManagerFactory factory;
      
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public UserRecord login(User userObject,UserContext context) throws MagtiException {
       try {
       utx.begin();
       oracleManager.persist(userObject);
       utx.commit();
       } catch (Exception e) {
       utx.rollback();
       e1.printStackTrace();
      }
      }
      


      but i get an error like this :

      java.lang.IllegalStateException: Container UserManagerBean: it is illegal to inject UserTransaction into a CMT bean
       at org.jboss.ejb3.injection.UserTransactionFieldInjector.<init>(UserTransactionFieldInjector.java:46)
      


      if i remove this :
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      the problem was not resolved.

      In II version program succsessfully deployed but i get an error again
      Session Bean
      @Remote(UserManager.class)
      public @Stateless
      class UserManagerBean implements UserManager {
      
       @PersistenceContext(unitName = "Oracle")
       private EntityManager oracleManager;
      
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public UserRecord login(User userObject,UserContext context) throws MagtiException {
       try {
       oracleManager.getTransaction().begin();
       oracleManager.persist(userObject);
       oracleManager.getTransaction().commit();
       } catch (Exception e) {
       oracleManager.getTransaction().rollback();
       e1.printStackTrace();
      }
      }
      

      in this case error was like this :

      03:42:07,015 ERROR [STDERR] java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
      03:42:07,015 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.getTransaction(TransactionScopedEntityManager.java:219)
      



      ooo ya there is my persistance.xml file :
      <?xml version="1.0" encoding="ISO-8859-1" ?>
      
      <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="Oracle" transaction-type="JTA">
       <jta-data-source>java:/XAOracleDS</jta-data-source>
       <class>com.magti.businesslayer.ejb3entity.oracle.User</class>
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
       <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
       <property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.9.133:1521:devnbtwo"/>
       <property name="hibernate.connection.username" value="USER_MANAGER"/>
       <property name="hibernate.connection.password" value="USER_MANAGER"/>
       <property name="hibernate.default_schema" value="USER_MANAGER"/>
       </properties>
       </persistence-unit>
      </persistence>
      



      COULD ANYBODY HELP ME.
      i don't know how i can manage transactions by myself :(


        • 1. Re: Simple EJB3 Transaction Question
          Carlo de Wolf Master

          Please add the following annotation to your bean class:

          @TransactionManagement(TransactionManagementType.BEAN)


          • 2. Re: Simple EJB3 Transaction Question
            paata lominadze Master

            thanks. It's working but i have another problem :(
            working code :

            @TransactionManagement(TransactionManagementType.BEAN)
            @Remote(Fasade.class)
            public @Stateless class FasadeBean implements Fasade
            {
             @PersistenceContext(unitName = "TEST")
             private EntityManager oracleManager;
             @Resource public UserTransaction utx;
            
             @TransactionAttribute(TransactionAttributeType.REQUIRED)
             public void addTest(Test t) throws IllegalStateException, SecurityException, SystemException
             {
             try {
             System.out.println("============== Income =============");
             utx.begin();
             System.out.println("EntityManager = "+oracleManager);
             System.out.println("UserTransaction = "+utx);
            
             System.out.println("Test = "+t);
             oracleManager.persist(t);
            
             utx.commit();
             System.out.println(" =============== Persisted =======");
             } catch (Exception e) {
             utx.rollback();
             e.printStackTrace();
             }
             }
            
            



            and then i did some changes on this :
            remove commit from this method and create new methot :

             public void commit() throws IllegalStateException, SecurityException, SystemException
             {
             try {
             utx.commit();
             System.out.println(" =============== Commited =======");
             } catch (Exception e) {
             utx.rollback();
             e.printStackTrace();
             }
             }
            



            i want that one method begis transactions and another finish it.
            but i get an error :

            javax.ejb.EJBException: Application error: BMT stateless bean FasadeBean should complete transactions before returning (ejb1.1 spec, 11.6.1)
             at org.jboss.ejb3.tx.BMTInterceptor.checkStatelessDone(BMTInterceptor.java:184)
            


            • 3. Re: Simple EJB3 Transaction Question
              Carlo de Wolf Master

              Read either EJB 1.1 11.6.1 or EJB3 13.6.1.

              • 4. Re: Simple EJB3 Transaction Question
                paata lominadze Master

                thank u vary much.
                it's working the problem was that session bean must statefull not stateless