EJB Rollback does not work with Container Management Transaction or Bean Managament Transaction on commit exception

给你一囗甜甜゛ 提交于 2020-01-07 00:06:22

问题


I spent hours triying to do a rollback with EJB. I have a CDI controller where I want to remove some object. When I try to remove in EJB I get an exception and I Try to do rollback, but It does not work. Every SQL which has been execute with commit before get the exception does not rollback. Obviusly It is not because I get another exception when I try to do the rollback in BMT. Otherwise when I tried with CMT I get an exception of hibernate but I get the same results that BMT.

My controller is

@Named
@Stateful
@ConversationScoped
public class PRequerimientoConjuntoCertificacionesBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1779474550283190942L;

    @Inject
    private Conversation conversation;

    @PersistenceContext(type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;

    @Inject
    private DatosSesion datosSesion;

    public void eliminarDocumento() {
        // TODO hay que probarlo
        DocumentoGeneradoSSCC documentoEliminar;
        try {
            documentoEliminar = (DocumentoGeneradoSSCC) daoBase
                    .getEntityById(DocumentoGeneradoSSCC.class,
                            10);
            documentoSSCCDAOBean.removeDocumentoSSCC(documentoEliminar,entityManager);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

With EJB BMT as follows:

@Stateful
@TransactionManagement(TransactionManagementType.BEAN)
public class DocumentoSSCCDAOBean implements IDocumentoSSCCDAOBeanLocal {

    @Resource
    private UserTransaction userTran;
    @PersistenceContext(type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void removeDocumentoSSCC(
            DocumentoGeneradoSSCC documentoGeneradoSSCC,
            EntityManager entityManager) {
        // TODO hue probarlo
        try {
            userTran.begin();
            // Eliminamos recurso asignado

            entityManager
                    .remove(entityManager.contains(documentoGeneradoSSCC) ? documentoGeneradoSSCC
                            : entityManager.merge(documentoGeneradoSSCC));
            userTran.commit();
        } catch (Exception e) {
            try {
                userTran.rollback();
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } 
    }
}

and I get this stacktrace

javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.

and when I try to do rollback I get this

java.lang.IllegalStateException: BaseTransaction.rollback - ARJUNA016074: no transaction!

I have standlone datasource with JTA=true,

<datasource jta="true" jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
    <driver>h2</driver>
    <security>
        <user-name>sa</user-name>
        <password>sa</password>
    </security>
</datasource>

my persistence.xml is

<persistence-unit name="JusticiaGratuita"
    transaction-type="JTA">
    <!-- <description>Forge Persistence Unit</description> -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
        <property name="hibernate.connection.driver_class" value="${db.driver}" />
        <property name="hibernate.connection.url" value="${db.url}" />
        <property name="hibernate.connection.username" value="${db.user}" />
        <property name="hibernate.connection.password" value="${db.pass}" />
        <property name="hibernate.default_schema" value="${db.schema}" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />

        <property name="hibernate.query.substitutions" value="true 1, false 0,'SI' 1, 'NO' 0" />

        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
        <property name="hibernate.jdbc.batch_size" value="20" />
        <property name="hibernate.cache.use_second_level_cache"
            value="true" />
        <property name="hibernate.cache.use_query_cache" value="false" />

        <property name="hibernate.show_sql" value="${db.showSql}" />
        <property name="hibernate.format_sql" value="true" />

        <property name="hibernate.connection.provider_class"
            value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />

        <property name="hibernate.c3p0.max_size" value="30" />
        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.acquire_increment" value="1" />
        <property name="hibernate.c3p0.idle_test_period" value="3000" />
        <property name="hibernate.c3p0.max_statements" value="0" />
        <property name="hibernate.c3p0.timeout" value="0" />
        <property name="hibernate.c3p0.autocommit" value="false" />
    </properties>

</persistence-unit>

You can see full stacktrace of my error here: http://pastebin.com/h17JD2xP

I would appreciate any help to do rollback and solve my problems.

Regards


回答1:


Finally I solve my problem. I was getting this exception because before of doing the rollback I execute commit, therefore It is imposible execute rollback after commit as those both are exclusive each other. Cause of that I got no transaction, because commit close it and when rollback execute there was no transaction.

java.lang.IllegalStateException: BaseTransaction.rollback - ARJUNA016074: no transaction!

Finally I decided to do in this way with CMT.

@TransactionManagement(TransactionManagementType.CONTAINER)
@Stateless
public class DocumentoSSCCServiceBean implements IDocumentoSSCCServiceBeanLocal {

    @EJB
    private IDaoBase daoBase;

    @PersistenceContext(unitName = "JusticiaGratuita", type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;

    @Resource
    private EJBContext ejbContext;

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void removeDocumentoSSCC(Long documentoGeneradoSSCC)
            throws RollbackSajgException {
        try {
            // Buscamos el documento a eliminar
            DocumentoGeneradoSSCC docu = (DocumentoGeneradoSSCC) daoBase
                    .getEntityById(DocumentoGeneradoSSCC.class,
                            documentoGeneradoSSCC);

            entityManager.remove(docu);
        } catch (Exception e) {

            ejbContext.setRollbackOnly();
            throw new RollbackSajgException();

        }
    }
}

I hope this could help someome.



来源:https://stackoverflow.com/questions/36356024/ejb-rollback-does-not-work-with-container-management-transaction-or-bean-managam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!