users@glassfish.java.net

WS with required transaction problem

From: Aleksandr Bissekerskij <aleksandrb_at_gmail.com>
Date: Thu, 20 Mar 2008 11:23:22 +0200

Hi all,

the problem is that my CMT (container managed transaction) is not working
properly, here is my servlet with simple method performing single record
persistence:

-------------------------------------------------------------------------------------------------
package lt.dpd.ws;

import javax.ejb.EJBException;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import lt.dpd.db.TestMaster;

@WebService()
public class MasterStateless {

    @PersistenceContext (unitName="persistPU")
    private EntityManager em;

    @WebMethod(operationName = "persist")
    public void persist() {

        // JPA entity
        TestMaster tm = new TestMaster();
        tm.setName("Stateless master");
        tm.setEnabled(true);

        em.persist(tm);

        throw new EJBException("EntityManager transaction should rollback");

    }

}
-------------------------------------------------------------------------------------------------


Transaction attribute for this method is set to REQUIRED, here is fragment
from wsit-lt.dpd.ws.MasterStateless.xml (this was generated by NetBeans)
where transaction is configured:
-------------------------------------------------------------------------------------------------
<binding name="MasterStatelessPortBinding" type="tns:MasterStateless">
        <operation name="persist">
            <wsp:PolicyReference
URI="#MasterStatelessPortBinding_persist_Policy"/>
            <input/>
            <output/>
        </operation>
    </binding>
    <service name="MasterStatelessService">
        <port name="MasterStatelessPort"
binding="tns:MasterStatelessPortBinding"/>
    </service>
    <wsp:Policy wsu:Id="MasterStatelessPortBinding_persist_Policy">
        <wsp:ExactlyOne>
            <wsp:All>
                <wsat:ATAssertion wsp:Optional="true"/>
                <wsat:ATAlwaysCapability/>
            </wsp:All>
        </wsp:ExactlyOne>
    </wsp:Policy>
-------------------------------------------------------------------------------------------------


the problem is that JTA transaction commits (and EntityManager transaction
commits) even if I throw EJBException.. instead of performing rollback for
everything in case of EJBException throwing as it is specificated.

notice that my MasterStateless web service is not JavaBean.

please help me! i've been googling for three days to find out why this is
not working and found nothing..

Regards,
AleBi