Misra’s blog

Archive for July 11th, 2007

Notes on Internationalization

Posted by mtwinkle on July 11, 2007

  • Set the charset of the page to an encoding that is supported by your target languages. I tend to use UTF-8 because it covers the most languages. The following meta declaration in a HTML/JSP page will set the content type:
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  • In the page JavaScript make sure to encode any parameters sent to the server. JavaScript provides the escape() function which returns Unicode escape strings in which localized text will appear in hexadecimal format. For more details on JavaScript encoding see Comparing escape(), encodeURI(), and encodeURIComponent().
  • On the server-side component set the character encoding using the HttpServletRequest.setCharacterEncoding() method. Before you access the localized parameter using the HttpServletRequest.getParameter() call. In the case of UTF this would be request.setCharactherEncoding("UTF-8");.

A server-side component returning AJAX responses needs to set the encoding of the response to the same encoding used in the page.

    response.setContentType("text/xml;charset=;UTF-8");
    response.getWriter().write("<response>invalid</response>");

Posted in J2EE | Leave a Comment »

What is 2-phase commit?

Posted by mtwinkle on July 11, 2007

A commit operation is an all-or-nothing affair. If a transaction cannot be completed, the rollback must restore the system to the pre-transaction state.

In order to ensure that a transaction can be rolled back, a software system typically logs each operation, including the commit operation itself. A recovery manager uses the log records to undo (and possibly redo) a partially completed transaction.

When a transaction involves multiple distributed resources, for example, a database server on two different network hosts, the commit process is somewhat complex because the transaction includes operations that span two distinct software systems, each with its own resource manager, log records, and so on.

With a two-phase commit protocol, the distributed transaction manager employs a coordinator to manage the individual resource managers.

The commit process proceeds as follows:

  • Phase 1
    • Each participating resource manager coordinates local operations and forces all log records out:
    • If successful, respond “OK”
    • If unsuccessful, either allow a time-out or respond “OOPS”
  • Phase 2
    • If all participants respond “OK”:
      • Coordinator instructs participating resource managers to “COMMIT”
      • Participants complete operation writing the log record for the commit
    • Otherwise:
      • Coordinator instructs participating resource managers to “ROLLBACK”
      • Participants complete their respective local undos

In order for the scheme to work reliably, both the coordinator and the participating resource managers independently must be able to guarantee proper completion, including any necessary restart/redo operations. The algorithms for guaranteeing success by handling failures at any stage are provided in advanced database texts.

Posted in J2EE | Leave a Comment »