OpenTP1のトランザクション制御機能を使用する場合は,TP1Connectionオブジェクトを使用します。RPCインタフェースに次のどのインタフェースを使用した場合でも使用できます。
次の条件でトランザクション制御機能を使用した場合のコーディング例を示します。
using System;
namespace MyCompany
{
public interface IGyoumuB
{
void PutData(string clientId, string[] key, string[] data);
int Invoke(string clientId, ref string result);
}
}
using System;
using Hitachi.OpenTP1;
using Hitachi.OpenTP1.Connector;
using MyCompany;
namespace MyCompany
{
[WebService]
public class MyWebService1
{
…
[WebMethod]
private string Service(string[] key, string[] data)
{
string clientId = "MyWS_Service_" + seqNo.ToString();
string result = "";
TP1Connection tc = null;
bool trnFlag = false;
try {
// グローバル変数のアプリケーション状態から
// TP1ConnectionManagerを取得
TP1ConnectionManager tcm =
(TP1ConnectionManager)this.Application["tcmTP1Host1"];
tc = tcm.GetConnection();
IGyoumuBStub server = new IGyoumuBStub(tc, "GRP1");
// トランザクション開始
tc.Begin();
trnFlag = true;
// 連鎖RPC
server.Flags = RpcInfo.DCRPC_CHAINED;
// 最大応答待ち時間(60秒)
server.WatchTime = 60;
// PutDataの呼び出し(サーバにデータの書き込み)
server.PutData(clientId, key, data);
// 連鎖RPCの終了
server.Flags = RpcInfo.DCNOFLAGS;
// 最大応答待ち時間(180秒)
server.WatchTime = 180;
// Invokeの呼び出し(処理要求)
server.Invoke(clientId, ref result);
// トランザクション終了
tc.Commit();
trnFlag = false;
return result;
} catch (TP1UserException exp) {
// Service3()からユーザ例外がスローされた
} catch (TP1RemoteException exp) {
// Service3()で予期しない例外発生
} catch (TP1ConnectorException exp) {
// Connector .NETが検知したエラー
} catch (TP1Exception exp) {
// その他スタブなどが検知したエラー
} catch (Exception exp) {
// 予期しない例外
}
finally
{
try {
// ロールバック
if (trnFlag) tc.Rollback();
}
catch (Exception) {
// 無視
}
// コネクションをコネクションプールに戻す
if (tc != null) tc.Dispose();
}
}
}
}