Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Getting Started with WS-AT using Spring

See in this article how to share the application context server via WS-AT protocol between two WebServices using Spring framework

Using the service-oriented architecture (SOA), you can create various services (WebServices) interacting with some datebases. Typically, these services are composed to be created a more robust service, such as a service that registers the loan a book from a library and other service that changes the status of the book to 'borrow'.

For this type of orchestration technologies such as BPEL or Biztalk are commonly used. But it is not always possible to use such technologies, which allow the atomic transaction control, and, when returning an exception in any of the services, the entire transaction is rolled back (returns to its original state without registering the changes made). In some cases, this rollback needs to be written so that when you return an exception, a routine previously written to be performed by reversing the changes made in the datebases.

The purpose of this post is to show how the WS-AT protocol (Web Services Atomic Transaction), along with the Spring framework can be used to commit changes to be made in the various services of the composition (book lending library, for example ) are only performed at the end of the process, when all the steps have been performed.

Used tools

For this article, we will use the application server Oracle, WebLogic, in the version 11g. For the creation of Web Services, the framework used will be the Spring, in 3.0.7.

Interactions with the datebase will be made through pure SQL, but it can be done without any change, using HQL or other datebase language. The way the interaction with the datebase is made does not interfere in the statement.

The protocol to be used for communication between the services and the application context is sending WS-AT (

The datebase that was used for this post is MySQL. Any datebase which allows XA connection can be used for this purpose.

Business rules

To demonstrate how it works, it will create a service that registers the loan books. To this end, two WebServices will be created, one for inclusion of books and other movement to change the book's status.

Data Repository

Two tables are used for this purpose. Containing the registration books and other registration drives for books, as a history of loans. Of course, a library system, there are several other tables to store all relevant information, but for what we will demonstrate, there are necessary. Follows the two scripting:

Listing 1. Script Creation of tables used

CREATE TABLE history_loan 
                          history_loan_sq INT(11) NOT NULL auto_increment, 
                          date_loan TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
                          book_sq INT(11) NOT NULL, 
                          PRIMARY KEY (history_loan_sq), 
                          CONSTRAINT history_loan_fk1 FOREIGN KEY (book_sq) REFERENCES book (book_sq) ON
             DELETE no action 
             UPDATE CASCADE, 
             engine=innodb DEFAULT charset=latin1;

     book_sq     INT(11) NOT NULL auto_increment, 
     name_book   VARCHAR(50) NOT NULL, 
     status_book INT(1) NOT NULL DEFAULT 0, 
     PRIMARY KEY (book_sq) 
DEFAULT charset=latin1;

Some code

Let's see now the DAOs (Data Access Object) of the two WebServices. DAO, by definition it is a standard introduced in JEE environment to simplify and decouple the interaction of Java applications with the JDBC API. In them we can see changes in the tables of the datebase that will be made by the two services:

Listing 2. DaoService1

public class DaoService1 {
 private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
 DaoService1(@Qualifier("LIBRARY_DB") DataSource dateSource) {
  this.namedParameterJdbcTemplate = new

 public int change(int book_sq, int status) throws SQLException {
  String sql = "UPDATE BOOK SET
  STATUS_LIVRO = "+ status +"
  WHERE LIVRO_SQ = "+ book_sq;
  Map<String, String> parameters = new
  java.util.HashMap<String, String> ();
  return namedParameterJdbcTemplate.update(sql, parameters);
Listing 3: DaoService2
public class DaoService2 {

 private NamedParameterJdbcTemplate
 DaoService2(@Qualifier("LIBRARY_DB") DataSource dateSource) {
  this.namedParameterJdbcTemplate = new

 public int add(int book_sq) throws
 SQLException {
  String sql = "INSERT INTO HISTORY_LOAN(LIVRO_SQ) VALUES("+ book_sq +")";
  Map<String, String> parameters = new
  java.util.HashMap<String, String> ();
  namedParameterJdbcTemplate.update(sql, parameters);

In the constructor of the two classes there is a 'autowired' for the dateSource. This dateSource must be configured in the application-context.xml services, as follows:

Listing 4. Application-context.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:context="" xmlns:jaxws="" xmlns:tx="" xmlns:xsi="" xsi:schemaLocation="              ">
   <context:component-scan base-package="com.library" />
   <bean id="LIBRARY_DB" name="LIBRARY_DB" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dateSource">
         <ref bean="LIBRARY_DB" />
   <bean id="transactionManager" autowire="autodetect" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">
      <property name="transactionManagerName" value="javax.transaction.TransactionManager" />
      <property name="userTransactionName" value="javax.transaction.UserTransaction" />
   <tx:annotation-driven transaction-manager="transactionManager" />

Since the purpose of this post is not Spring, but its interaction with WS-AT, let's take a very quick last this XML. We create a 'JndiObjectFactoryBean' to point the dateSource in the application server. We create a 'AnnotationSessionFactoryBean' to generate sessions used by the service.

Finally, we created a 'transactionManager' to manage the transactions. This is the main bean in this post. Note that this 'transactionManager' has a UserTransaction, so that the service does not have to open a hand. Another important point is that it is a 'WebLogicJtaTransactionManager', indicating who should take care of the transactions is the application server, not the service itself.

For a good organization in the code, now we create two classes of service, which should make the 'middle ground' between the WSImpl and Dao, as follows:

Listing 5. Service1

@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED, rollbackFor = Exception.class)
public class Service1 {

 private DaoService1 dao;

 @WebServiceRef(value = WsatService2_Service.class)
 WsatService2 service2;
 public void lendBook(int book_sq) throws Exception {

Listing 6. Service2

@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)
public class Service2 {
  DaoService2 dao;
  public int insert(int book_sq) throws SQLException {
    return dao.insert(book_sq);

In this layer, the Spring through the annotation '@ org.springframework.transaction.annotation.Transactional', picks up a session with the application directly from the datebase server within a transaction already set up by the application server.

On the Service1, "the service2 field, WsatService2 type is annotated with '@ weblogic.wsee.wstx.wsat.Transactional' to indicate that, to make the call for this service, taking into account the transaction context. There are some parameters for this note, however, the defaults are sufficient for this example.

Finally, we show now the implementation of WebServices, where we received the context of the application.

Listing 7. WsatService1

@WebService(serviceName = "WsatService1", targetNamespace = "http://com.library", portName = "wsatPort1")
public class WsatService1 {
  private Service1 service = new Service1();
  public WsatService1() {
    service = (Service1)
  public String lendBook(@WebParam(name="book_sq") int book_sq) {
    try {
      return "OK";
    } catch (Exception e) {
      return "NOK";

Listing 8. WsatService2

@WebService(serviceName = "WsatService2", targetNamespace = http://com.library, portName = "wsatPort2")
public class WsatService2 {
  private Service2 service;
  public WsatService2() {
    service = (Service2) ApplicationContextHelper.getBeanForClass(Service2.class);
  public String registerLoan(@WebParam(name = "book_sq") int book_sq) throws Exception {
    return "OK";

The classes that implement the WebService should be noted with '@ weblogic.wsee.wstx.wsat.Transactional'. This indicates that they support the receipt of an application server context.

Logic description

With all relevant classes presented, we will describe a little how to run this example.

When making a call on WsatService1 in 'lendBook' method, the Service1 class is instantiated and starts orchestration.

In the orchestration, two steps should be performed:

  • Register the book of the loan;

  • Change the status of the loan book.

For this, the Service1 class makes a call to WsatService2 service method 'registerLoan', passing the id of the book and then calls the DaoService1 to change the book's status.

So far there is nothing different from any other service. The difference exists because the notation '@ weblogic.wsee.wstx.wsat.Transactional' [SF-CV10] that allows sending/receiving of context, so that the commit is performed only at the end of the process. Through this note, the first service run sends the context of the application server to the other services of the composition. Without it, each service performed within the composition would create a different context on the server, creating multiple transactions, one for each service. Having only one context, and this context being trafficked between services, through the WS-AT (using the aforementioned notation only), there is only one transaction for all services performed.

If the service is called WsatService2 and include a record in the datebase, but when trying to change the book's status, give a problem, the record included in the datebase is not comitado.

On the other hand, if the WsatService2 service is called directly, without being part of an orchestration WsatService1 with the inclusion of the record in the datebase is done normally.


As we see in this post, you can perform a transaction with the datebase that goes beyond a service that includes an orchestration through Java using Spring.

We have seen that this atomic transaction causes the execution of all of the compound services is made within the same transaction, within a context server, thanks to the WS-AT protocol and simple notation 'Transactional' the weblogic.

You can get the same results with an active rollback but routine would be necessary to carry out this rollback, which means more code to be managed.

Web developer and passioned for web design, SEO and front end technologies.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login