title: How to persist SOAP Messages in MySQL Database - working with Axis Client
body: <p>I am writing a java axis client, how could I persist raw xml into data base, till now I have found two ways of logging raw xml, but they are for console or to a file, but I need to persist each request and response into mysql database, where I could use connection as a user parameter.</p>
  
  <p>here is what I have done already.</p>
  
  <ol>
  <li><a href="http://www.coderanch.com/t/148794/java-Web-Services-SCDJWS/certification/Getting-Raw-Request-Response-XML" rel="nofollow">log raw ws xml to console</a> </li>
  <li><a href="http://wiki.apache.org/ws/FrontPage/Axis/AxisClientConfiguration/ViewSOAPMessages" rel="nofollow">log raw ws xml to a file</a></li>
  </ol>
  
  <p>Well I have found a solution, First we need to use the Custom handler as I mentioned earlier(1), we can set property in the message context
  like </p>
  
  <pre><code>public class FedexWsHandler extends GenericHandler {
  
      public QName[] getHeaders() {
          return null;
      }
  
      public boolean handleRequest(MessageContext context) {
      try {
          SOAPMessageContext smc = (SOAPMessageContext) context;
          SOAPMessage reqMsg = smc.getMessage();
          context.setProperty("req-msg", reqMsg);
          } catch (Exception ex) {
          ex.printStackTrace();
              }
      return true;
      }
  
      public boolean handleResponse(MessageContext context) {
      try {
          SOAPMessageContext smc = (SOAPMessageContext) context;
          SOAPMessage reqMsg = smc.getMessage();
          context.setProperty("res-msg", reqMsg);
          } catch (Exception ex) {
          ex.printStackTrace();
              }
      return true;
      }
  
  }
  </code></pre>
  
  <p>and then in out client we can get that property and do what ever you want to do, like </p>
  
  <pre><code>MyServiceLocator locator = new MyServiceLocator(); 
  MyService service = locator.getMyService(); 
  service.getResults("foo", "bar"); // call the service
  
  // I want to get that message I have set in request and response handler methods
  MessageContext ctx = locator.getCall().getMessageContext();
  SOAPMessage reqMsg = (SOAPMessage) requestContext.getProperty("req-msg");         
  SOAPMessage resMsg = (SOAPMessage) requestContext.getProperty("res-msg");         
  </code></pre>
  
  <p>But it is not a safe way to do that as this is not <strong>Thread Safe</strong>. as per its <a href="http://ws.apache.org/axis/java/apiDocs/org/apache/axis/client/Service.html#getCall%28%29" rel="nofollow">docs</a></p>
  
  <p>.... so if any one can suggest me some better solution.</p>