0 Replies Latest reply on Mar 18, 2014 5:51 AM by riccardo marfisi

    JBoss EAP 6.2.0 - "java.lang.IllegalStateException: BaseTransaction.rollback - ARJUNA016074: no transaction!" when try persist

    riccardo marfisi Newbie

      Hi at all,

       

      I'm very beginner and I'm studying how work CDI, JPA, Hibernate, JSF

       

      with this guide (greeter: Greeter Example) I tried to do CRUD with my DB (PostgreSql v9.3)

       

      I'm work in local machine Windows Server 2008, where i have installed JBoss EAP 6.2.0.GA (AS 7.3.0.Final-redhat-14), PostgreSql and Eclipse Kepler.

       

      When i read from DB, no problem, but when try to write, I obtain "ARJUNA016074: no transaction" problem.

       

      Probably It's a my mistake, but I'm not able to understand where e what I do.

       

      Controller

      
      
      package it.cascino.controller;
      
      
      import java.util.Iterator;
      import java.util.List;
      import it.cascino.dao.FotoDao;
      import it.cascino.h8.entity.Foto;
      import javax.enterprise.context.RequestScoped;
      import javax.enterprise.context.SessionScoped;
      import javax.enterprise.inject.Produces;
      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      import javax.inject.Inject;
      import javax.inject.Named;
      
      
      @Named
      @RequestScoped
      //@SessionScoped
      public class FotoController{
      
        @Inject
        private FacesContext facesContext;
      
        @Inject
        private FotoDao fotoDao;
      
        private String fotoName;
        private String fotoPath;
      
        private String esito;
      
        @Named
          @Produces
         // @RequestScoped
          private Foto foto = new Foto();
      
        public void getFotoForId(){
        Foto foto = fotoDao.getForId(Integer.parseInt(fotoName));
        if(foto != null){
        esito = "Reperita foto: " + foto.getPath() + foto.getOriginale();
        }else{
        esito = "non ho trovato la foto!";
        }
        String message = esito + " >" + fotoName + "<";
        facesContext.addMessage(null, new FacesMessage(message));
        }
      
      
        public void addFoto(){
        fotoDao.insertFoto(foto);
        if(foto != null){
        esito = "Aggiunta foto: " + foto.getPath() + foto.getOriginale();
        }else{
        esito = "non ho trovato la foto!";
        }
        String message = esito + " >" + fotoName + "<";
        facesContext.addMessage(null, new FacesMessage(message));
        }
      
        public String getFotoName(){
        return fotoName;
        }
      
        public void setFotoName(String fotoName){
        this.fotoName = fotoName;
        }
      
        public String getFotoPath(){
        return fotoPath;
        }
      
        public void setFotoPath(String fotoPath){
        this.fotoPath = fotoPath;
        }
      
        public String getEsito(){
        return esito;
        }
      
      }
      

       

       

      in insertFoto function, when execute entityManager.persist(foto) I obtain the error

       

      import java.util.List;
      import it.cascino.h8.entity.Foto;
      import it.cascino.h8.entity.Test;
      import javax.enterprise.context.ConversationScoped;
      import javax.enterprise.context.RequestScoped;
      import javax.inject.Inject;
      import javax.persistence.EntityManager;
      import javax.persistence.NoResultException;
      import javax.persistence.Query;
      import javax.transaction.SystemException;
      import javax.transaction.UserTransaction;
      import org.jboss.logging.Logger;
      
      
      //@SessionScoped
      public class ManagedBeanFotoDao implements FotoDao{
        /**
        * Logger
        */
        private static Logger log = Logger.getLogger(ManagedBeanFotoDao.class.getName());
      
        @Inject
        private EntityManager entityManager;
      
        @Inject
        private UserTransaction utx;
      
      
        public Foto getForId(int fotoId){
        Foto foto = new Foto();
        try{
        try{
        utx.begin();
        foto = entityManager.find(Foto.class, fotoId);
        }finally{
        utx.commit();
        }
        }catch(Exception e){
        try{
        utx.rollback();
        }catch(SystemException se){
        throw new RuntimeException(se);
        }
        throw new RuntimeException(e);
        }
        return foto;
        }
      
        public void insertFoto(Foto foto){
        try{
        try{
        utx.begin();
        log.info("1");
        log.info("transaction:" + " " + utx.getStatus());
        entityManager.persist(foto);
        log.info("transaction:" + " " + utx.getStatus());
        }finally{
        log.info("2");
        log.info("transaction:" + " " + utx.getStatus());
        utx.commit();
        }
        }catch(Exception e){
        try{
        log.info("3");
        log.info("transaction:" + " " + utx.getStatus());
        log.info("4");
        utx.rollback();
        }catch(SystemException se){
        log.info("5");
        throw new RuntimeException(se);
        }
        log.info("6");
        try{
        log.info("transaction:" + " " + utx.getStatus());
        }catch(SystemException e1){
        e1.printStackTrace();
        }
        throw new RuntimeException(e);
        }
        }
      }
      

       

       

      I've configured a datasource in this way:

      C:\dev\jboss-eap-6.2\modules\org\postgresql\main\module.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <module xmlns="urn:jboss:module:1.0" name="org.postgresql">
        <resources>
        <resource-root path="postgresql-9.3-1101.jdbc41.jar"/>
        </resources>
        <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
        </dependencies>
      </module>
      
      

       

      inside folder there is jdbc driver:

      postgresql-9.3-1101.jdbc41.jar

       

      C:\dev\jboss-eap-6.2\standalone\configuration\standalone.xml

              <subsystem xmlns="urn:jboss:domain:datasources:1.1">
                  <datasources>
                      <datasource jta="true" jndi-name="java:jboss/datasources/PostgresqlDS" pool-name="java:jboss/datasources/PostgresqlDS_Pool" enabled="true" use-java-context="true" use-ccm="true">
                          <connection-url>jdbc:postgresql://localhost:5432/name_db</connection-url>
                          <driver>postgresql-jdbc4</driver>
        <security>
                              <user-name>xxx_user_db</user-name>
                              <password>yyyyyy</password>
                          </security>
                      </datasource>
                      <drivers>
                          <driver name="postgresql-jdbc4" module="org.postgresql">
                              <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                          </driver>
                      </drivers>
                  </datasources>
              </subsystem>
      
      

       

      /prj/src/main/resources/META-INF/persistence.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.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_2_0.xsd">
         <persistence-unit name="primary">
            <jta-data-source>java:jboss/datasources/PostgresqlDS</jta-data-source>
            <properties>
               <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.connection.pool_size" value="1" />
      
      
        <!-- SQL dialect -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
      
      
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class" value="org.hibernate.cache.internal.NoCacheProvider" />
        </properties>
         </persistence-unit>
      </persistence>
      
      

       

      in server.log:

      10:21:53,432 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) 1
      10:21:53,433 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) transaction: 0
      10:21:53,592 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) 2
      10:21:53,593 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) transaction: 1
      10:21:53,595 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) 3
      10:21:53,595 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) transaction: 6
      10:21:53,595 INFO  [it.cascino.dao.ManagedBeanFotoDao] (http-localhost/127.0.0.1:8080-3) 4
      10:21:53,596 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-localhost/127.0.0.1:8080-3) #{fotoController.addFoto}: java.lang.IllegalStateException: BaseTransaction.rollback - ARJUNA016074: no transaction!: javax.faces.FacesException: #{fotoController.addFoto}: java.lang.IllegalStateException: BaseTransaction.rollback - ARJUNA016074: no transaction!
      
      

       

       

      i'm sorry for my bad english e thanks in advance.

      Riccardo