Recent Blog Entries

Thursday, 12 August 2010

Web Service Transactions Part 1: WS-AtomicTransaction with EJB-based Webservices and WLS 11gR1

To make it clear at first: I am not a fan of web service transactions. They contradict the principle of loose coupling in a SOA. But it is like with XA and 2PC – there are situations where you want to use them or where it is almost mandatory.

In SOA there have been concepts like compensation in BPEL to avoid atomic transactions spanning several SOAP calls (before they were possible and available technically) but some business requirements do not allow to commit at first and undo later on.

Also, Oracle SOA Suite optimizes local calls, so transactions were possible over multiple service invocations if they are on the same application server instance (see optSoapShortcut property in 10g)

To allow web services with SOAP using transactions like in EJB-based distributed applications the following standards have been published by OASIS (

  • WS-AtomicTransaction v1.2
  • WS-BusinessActivity v1.2
  • WS-Coordination v1.2

    The latest version 1.2 was published in February 2009 and WS-Coordination and WS-AtomicTransaction support started in Weblogic Server 11gR1 (10.3.3) and Oracle Soa Suite 11g PS2 ( WS-BusinessActivity is for long running business transactions and therefore not covered here.

    With WS-AtomicTransaction (abbreviated WS-AT) it is possible to call another web service (callee) from a web service (caller) within the same transaction. The transaction context is passed to the other web service via soap in a CoordinationContext structure in the SOAP header. Some other components work together as outlined here to let the second web service participate in the transaction.

    Let’s have a look at how you can setup a transaction spanning 2 or more web services.

    The first scenario we will look at is when you have implemented 2 web service based on JAX-WS EJBs in Weblogic and you want to call the one from the other.

    A good startup example for WS-AtomicTransaction is included in the Weblogic Server 11gR1 installation when you select custom install and include the "Server Samples” for installation. Then you can move to %WLS_HOME%\wlserver_10.3\samples\server\examples\src\examples\webservices\jaxws\wsat.

    After setting the examples environment, compiling and deploying with ant you will see the Bank application:


    You can enter for example local account number 100 and remote account number 100 with initial amounts of 100 USD.Then try to transfer 20 USD from local to remote account. This is done in one atomic transaction.

    The application consists of one servlet which makes modifications on the local account and then calls the web service for the remote  located at


    The wsdl is available at


    To be able to see the SOAP request to the web service I have put TcpMon  inbetween: change the following lines in to map the TcpMon port (i.e. 8081):

    public void doPost(HttpServletRequest request,HttpServletResponse response)

               throws ServletException, IOException {

        // define remote Webservice server
        String url = "
        URL wsdlURL = new URL(url + "/WsatBankTransferService/WsatBankTransferService");

    The SOAP request looks like follows – the WS-AT specific parts are contained in the CoordinationContext structure:

    <?xml version='1.0' encoding='UTF-8'?>
    <S:Envelope xmlns:S="">
        <To xmlns="">http://localhost:8081/WsatBankTransferService/WsatBankTransferService</To>
        <Action xmlns=""></Action>
        <ReplyTo xmlns="">
        <FaultTo xmlns="">
        <MessageID xmlns="">uuid:a64a702b-0ce6-40fc-9b12-71a494c41520</MessageID>
        <CoordinationContext xmlns:ns3=""
              <wls-wsat:txId xmlns:wls-wsat="
              <wls-wsat:routing xmlns:wls-wsat="

        <ns2:createAccount xmlns:ns2="">

    More details regarding the implementation:

    The WsatBankTransferService bean contains the following annotations to enable WS-AT:

    @WebService(serviceName = "WsatBankTransferService", targetNamespace = "", portName = "WSHttpBindingIService")

    Each method of the webservice looks up the JTA user transaction (if available):

    ctx = new InitialContext();
    tx = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");

    The servlet passes the TransactionalFeature to the web service as documented in

    Oracle® Fusion Middleware Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server – Chapter 3: Using Web Services Atomic Transactions

    // Passing the TransactionalFeature to the Client
    private WsatBankTransferService getWebService(URL wsdlURL) {
      TransactionalFeature feature = new TransactionalFeature();
            WsatBankTransferService_Service service =
                    new WsatBankTransferService_Service(wsdlURL,
                        new QName("", "WsatBankTransferService"));
      return service.getWSHttpBindingIService(new, feature);

    In the next parts we will call the WsatBankTransferService from a SOA Composite and we will span atomic transactions over multiple SOA composites.

  • No comments:

    Post a Comment