首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> DB2 資料庫 >> DB2 教學

DB2 XML 編程實戰(四):基於 SOAP 驅動的多數據源集成架構

     編輯:DB2 教學

從驅動程序到 SOA 節點:DB2 SOAP 驅動程序原型

下一代數據庫驅動將拋棄 ODBC/JDBC 等低層 API,轉而採用 SOAP/REST 高層協議,使數據庫直接成為 SOA 參與者。本文提供一個 DB2 SOAP 驅動程序示例,通過隱式調用存儲過程實現無映射集成。

特性級體系結構

設計級體系結構

業務場景:酒店預訂全流程

  1. 用戶登錄 → 獲取個人信息(本地 DB2,JDBC)
  2. 搜索城市酒店 → 調用內部防火牆後的訂房數據庫(SOAP)
  3. 選擇酒店預訂 → 呼叫 PayPal 處理信用卡(REST)

步驟 1:登錄與個人信息(JDBC 直連)

表結構:CUSTOMERS(CUSTID, CC VARCHAR FOR BIT DATA, INFO XML),信用卡加密存儲。

INSERT INTO CUSTOMERS VALUES ('hardeep',
  ENCRYPT('<CC type="visa" number="4721930402892796" cvv="808"><name>hardeep</name></CC>', 'passWord'),
  '<Customer customerid="hardeep" firstname="hardeep" lastname="singh"/>');

登錄命令與數據流

登錄界面

客戶端 REST 調用

var cid = document.getElementById("userid").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', servletpath + "?cmd=customerinfo&msg=" + cid, true);
xmlhttp.onreadystatechange = function() { getCustomerInfoCallback(xmlhttp); };
xmlhttp.send("");

服務端 JDBC 查詢

Connection conn = DriverManager.getConnection("jdbc:db2:article4");
Statement stmt = conn.createStatement();
stmt.setMaxRows(1);
ResultSet rs = stmt.executeQuery("SELECT info FROM customers WHERE custid='" + msg + "'");
if (rs.next()) retValue = rs.getString(1);
stmt.close(); conn.close();

響應頭 Content-Type: text/xml 直接返回 XML。

步驟 2:搜索酒店(SOAP 調用內部數據庫)

列出酒店命令與數據流

搜索酒店界面

客戶端構造 OTA 請求

var req = 'OTA_HotelSearchRQ xmlns="https://www.opentravel.org/OTA/2003/05" ... '
        + '<HotelRef HotelCityCode="' + citycode + '"/> ... </OTA_HotelSearchRQ>';
var msg = '<request cmd="hotelrates">' + req + '</request>';
// AJAX POST 發送

服務端構造 SOAP 調用 DB2

String body = "<db:getHotelRates xmlns:db='https://ibm.com/db2/soap'>"
            + "<db:arg>" + msg + "</db:arg></db:getHotelRates>";
return sendURLMessage("https://localhost:8080/article4/db2soapdriver", body, "https://ibm.com/db2/soap#article4");

SOAPAction 設置為目標數據庫名。sendURLMessage 手工構造 SOAP Envelope,證明 SOAP 本質是標準化的 HTTP POST。

DB2 存儲過程:XQuery 過濾酒店

CREATE PROCEDURE getHotelRates(IN request XML)
DYNAMIC RESULT SETS 1 LANGUAGE SQL
BEGIN
  DECLARE c_cur CURSOR WITH RETURN FOR
    SELECT XMLQUERY('declare namespace ns1="https://www.opentravel.org/OTA/2003/05";
      $info//ns1:HotelDescriptiveContents' PASSING info AS "info")
    FROM hotel
    WHERE XMLEXISTS('declare namespace ns1="https://www.opentravel.org/OTA/2003/05";
      $info//ns1:HotelDescriptiveContents[@HotelCityCode=$req//ns1:HotelRef/@HotelCityCode]'
      PASSING request AS "req", info AS "info");
  OPEN c_cur;
END

SOAP 響應原樣透傳回客戶端,服務端僅作中介。

客戶端解析 SOAP 響應

var soapxml = new xmlparse(xmlhttp.responseXML, false);
soapxml.XMLRoot.setProperty("SelectionNamespaces",
  "xmlns:SOAP-ENV='https://schemas.xmlsoap.org/soap/envelope/' xmlns:db='https://ibm.com/db2/soap'");
soapxml.find("//SOAP-ENV:Body//db:row", null, true);
for (var i=0; i<soapxml.currentFind.length; i++) {
  var row = new XMLparse(soapxml.getValue("db:col/text()", i), true);
  row.setNamespaces("xmlns:x='https://www.opentravel.org/OTA/2003/05'");
  var id = row.getValue("//x:HotelDescriptiveContents/@HotelCode");
  var name = row.getValue("//x:HotelName/@HotelShortName");
  var rooms = row.getValue("//x:GuestRoomInfo/@Quantity");
  var charge = row.getValue("//x:Charge/@Amount");
  // 渲染酒店列表,綁定 bookRoom(hotelId, charge)
}

步驟 3:預訂房間(REST 調用 PayPal)

預訂命令與數據流

選擇酒店界面

客戶端發送預訂請求

function bookRoom(hotelid, amount) {
  var msg = '<request cmd="bookroom"><message><ccinfo units="1" amount="' + amount + '"/>'
          + '<username>' + cid + '</username><hotelid>' + hotelid + '</hotelid></message></request>';
  // AJAX POST
}

服務端解密信用卡並調用內部存儲過程

String userid = msgXML.getValue("//username/text()");
ResultSet rs = stmt.executeQuery("SELECT info, DECRYPT_CHAR(CC,'passWord') FROM customers WHERE custid='" + userid + "'");
if (rs.next()) {
  String custinfo = rs.getString(1);
  String body = "<db:bookaroom xmlns:db='https://ibm.com/db2/soap'>"
              + "<db:arg>" + hotelid + "</db:arg>"
              + "<db:arg>" + custinfo + "</db:arg>"
              + "<db:arg></db:arg></db:bookaroom>";
  sendURLMessage("https://localhost:8080/article4/db2soapdriver", body, "https://ibm.com/db2/soap#article4");
}

架構啟示

  • 數據庫通過 SOAP 驅動直接暴露業務邏輯(存儲過程),成為服務提供者。
  • 應用服務器退化為協議轉換器(REST↔SOAP)、安全網關、聚合器。
  • XML 作為統一數據契約,貫穿客戶端、中間層、多個異構數據庫。
  • OTA、PayPal 等外部標準無縫融入,無需對象映射層。

結語

DB2 SOAP 驅動程序原型預示了數據庫即服務的未來:標準協議、原生 XML、業務邏輯下推,讓集成更簡潔、更敏捷。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved