Paul Copeland <[EMAIL PROTECTED]> writes:

> Nic - Can you be more specific about how you "batch away" the
> transaction? What thread is this transaction running in? JMS would make
> it asynchronous. You imply some other mechanism would also work. What
> would that be?

Sure. The thread servicing the request can be used.

Here is a specific example: imagine we have a legacy call logging
system which has a SQL stored proc interface allowing you to:

- create a call
- change a call's priority
- plus a bunch of other stuff (mark calls done, etc...)

We're asked by the PHB to build a web interface that allows someone to
send a POST which creates a call of one of a number of different
prioritys. The current SQL proc create interface doesn't allow for
that so we're gonna have to join two operations together.

The trouble is, the creation of the call is a very long process; it's
a foxpro database running on a 386 in a cupboard next to the loo:  but
hey, it's legacy so we're not allowed to change it.


So here's an almost complete example of a servlet's doPost:

public void doPost (...)
{
  PrintWriter out = response.getWriter();
  response.setContentType("text/html");
  String caller = request.getParameter("caller");
  String problem = request.getParameter("problem");
  String priority = request.getParameter("priority");

  HttpSession session = request.getSession();
  Hashtable callDetails = new Hashtable();
  session.setAttribute("caller", callDetails);

  String checkUrl = request.getServletPath();
  out.println("<html><body><a href='" + checkUrl + "'>");
  out.println("Check the call here");
  out.println("</a></body></html>");
  // The client sees a completed page from here
  out.close();

  try {
    Connection con = conPool.getConnection();
    CallableStatement create
      = con.prepareCall("{ ? = call call_make ( ?, ? ) }");
    create.setString(2, caller);
    create.setString(3, problem);
    create.execute();
    String callId = create.getString(1);

    // Update the record
    callDetails.put(callId, "");

    CallableStatement create
      = con.prepareCall("{ call call_set_pri ( ?, ? ) }");
    create.setString(1, callId);
    create.setString(2, priority);
    create.execute();

    callDetails.put(callId, priority);
  }
  catch (SQLException e) {
     throw new AghException(e);
  }
  finally {
    conPool.returnConnection(con);
  }
}


So the thread servicing the request is used, after the response has
been finished, to do some important work. This only works if you've
understood and implemented the asynchronous nature of the web.

btw this is the most misunderstood and least applied technique of web
programming. Most programmers just don't seem to understand that the
web is asynchronous and try to make it synchronous. The whole of EJB
can be seen as a misguided attempt to apply synchronicity to webapps.

Once you accept that the web is async, and learn how to deal with it,
life gets much much easier.

--
Nic Ferrier
http://www.tapsellferrier.co.uk

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to