javacdideltaspike

DeltaSpike @Transactional is not called


Well, i'm trying to use @Transactional by DeltaSpike but TransactionalInterceptor is never called.

This is my EntityManagerProducer:

public class EntityManagerProducer implements Serializable {

    private static final long serialVersionUID = 1L;


    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("generalPU");

    @Produces
    @TransactionScoped
    // is a bit better than @RequestScoped because it won't allow perform injection outside transaction context
    public EntityManager createEntityManager() {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager em) {
        if (em.isOpen()) {
            em.close();
        }
    }


    public EntityManagerFactory getEmf() {
        return emf;
    }


    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

}

I have a class that inject EntityManager and use @Transactional, see:

@Named
@RequestScoped
public class BasicDAOImpl implements BasicDAO, Serializable {
    @Inject
    private EntityManager entityManager;

    @Transactional
    public void save(AbstractBean bean) {
    try {
        entityManager.merge(bean);
    } catch (Exception e) {
        logger.error("Erro ao tentar salvar. \n " + e);
    }
    }
}

And i have a beans.xml with interceptor, look:

<?xml version="1.0"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd">
     <interceptors>
        <class>org.apache.deltaspike.jpa.impl.transaction.TransactionalInterceptor</class>
    </interceptors>
</beans>

How can i fix this problem ? I'm following exactly the same example in the apache page (https://deltaspike.apache.org/documentation/jpa.html).

I'm using Tomcat7 + JDK 1.8 + CDI 1.2 (Weld 2.3.0 final implementation)

Edit 1:

This is my 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="generalPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:/comp/env/jdbc/cedDS</non-jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <property name="hibernate.cache.use_query_cache" value="false" />
            <property name="hibernate.jdbc.batch_size" value="50" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />           
        </properties>
    </persistence-unit>

</persistence>

Edit 2: My dependencies in pom.xml

<dependency>
            <groupId>org.facebook4j</groupId>
            <artifactId>facebook4j-core</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jpa-module-api</artifactId>
            <version>1.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jpa-module-impl</artifactId>
            <version>1.7.1</version>
        </dependency>


        <!-- JASPER REPORT IMPORTS -->

        <dependency>
            <groupId>net.sourceforge.barbecue</groupId>
            <artifactId>barbecue</artifactId>
            <version>1.5-beta1</version>
        </dependency>


        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>5.0.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.10.0</version>
        </dependency>


        <!-- FIM JASPER RESPORT IMPORTS -->

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.6</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>


        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- CDI -->
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.3.0.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>


        <!-- HIBERNATE -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-commons</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>20040616</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.5</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

Solution

  • This works by me, with breakpoint into TransactionalInterceptor. Manuelly register the TransactionalInterceptor is no need.

    public class EntityManagerProducer implements Serializable {
    
        @Inject
        @PersistenceUnitName(value="generalPU")
        private EntityManagerFactory entityManagerFactory;
    
        @Produces
        @TransactionScoped
        protected EntityManager createTranEntityManager() {
            return this.entityManagerFactory.createEntityManager();
        }
    
        protected void closeEntityManager(@Disposes EntityManager entityManager){
            if (entityManager.isOpen()) {
                entityManager.close();
            }
        }
    }