Initiating a transaction from a Servlet

It is better to initiate the transactions from the beans, however in some cases you want the transaction to be initiated from the servlet. In this case, proceed as follows:

...
    try
    {
      Context initialContext = new InitialContext();
      UserTransaction utx = (javax.transaction.UserTransaction)
                  initialContext.lookup("java:comp/UserTransaction");
      utx.begin();
      ... do some interesting work here ...
      if (success)
        utx.commit();
      else
        utx.rollback();
    } 
    catch (Exception ignore) 
    {
      return ;
    }
...