Synchronizationオブジェクトは、Synchronizationインタフェースの以下の2つの機能を実装したオブジェクトです。
Synchronizationオブジェクト内のbefore_completionメソッドは、データベース連携サービスがデータベースにトランザクション完了を依頼する前に、データベース連携サービスから呼ばれます。before_completionメソッド実行時にエラーが発生した場合は、該当トランザクションは、データベース連携サービスによりrollbackされます。
Synchronizationオブジェクト内のafter_completionメソッドは、トランザクション完了後に、データベース連携サービスから呼ばれます。
Synchronizationオブジェクトは、データベースにアクセスするサーバアプリケーションオブジェクトと同じプロセス上に配置するように作成します。
注) 図中番号順に制御されます。
Syncronizaionオブジェクト用のアプリケーションの例を以下に示します。
■サーバアプリケーションオブジェクト内にSynchronizationインタフェースをもつ場合
Synchronizationオブジェクトの実装について、サーバアプリケーションと同じオブジェクトに配置する場合のIDL定義とユーザアプリケーションの例を示します。なお、ユーザアプリケーションのインプリメンテーションリポジトリへの登録時は、設計に合わせ、プロセス多重度を少なくとも2以上に設定してください。
IDL定義
ユーザアプリケーションのインタフェース宣言において、Synchronizationインタフェースを以下のように継承してください。
include文で指定する“CosTransactions.idl”は、以下に格納されています。
注)本製品のインストールパスがデフォルトの場合のパスです。
C:\Interstage\ots\src\idls\CosTransactions.idl
/opt/FSUNots/src/idls/CosTransactions.idl
/opt/FJSVots/src/idls/CosTransactions.idl
[bankA.idl]
#include "CosTransactions.idl" module bankA{ interface accounA : CosTransactions::Synchronization{ void deposit1(in long a,in CosTransactions::Control control); }; };
ユーザアプリケーションの例
bankA_accountA_deposit1(bankA_accountA obj,long a, CosTransactions_Control control, CORBA_Environment *env) { CosTransactions_Coordinator coordinator; coordinator = CosTransactions_Control_get_coordinator(control, env ); CosTransactions_Coordinator_register_synchronization( coordinator, obj , env); /* ユーザの操作 */ SQL データベースへのアクセス : } /* before_completion */ bankA_accountA_before_completion( bankA_accountA obj, CORBA_Environment *env ) { fprintf(stderr,"bankA_accountA_before_completion \n"); return; } /* after_completion */ void bankA_accountA_after_completion( bankA_accountA obj, CosTransactions_Status status, CORBA_Environment *env ) { fprintf(stderr,"bankA_acoountA_after_completion %d\n",status); return; }
■サーバアプリケーションオブジェクトと別のオブジェクトとして配置した場合
Synchronizationオブジェクトの実装について、サーバアプリケーションと別のオブジェクトとして作成する場合のIDL定義とSynchronizationインタフェースを使用したアプリケーションの例を示します。なお、ユーザアプリケーションのインプリメンテーションリポジトリへの登録時は、プロセス多重度を2以上に設定してください。
IDL定義
Synchronizationオブジェクトのインタフェース宣言において、Synchronizationインタフェースを以下のように継承してください。
include文で指定している“CosTransactions.idl”は、以下に格納されています。
注)本製品のインストールパスがデフォルトの場合のパスです。
C:\Interstage\ots\src\idls\CosTransactions.idl
/opt/FSUNots/src/idls/CosTransactions.idl
/opt/FJSVots/src/idls/CosTransactions.idl
[bankA.idl]
#include "CosTransactions.idl" module bankA{ interface accountA { void deposit1(in long a,in CosTransactions::Control b); }; interface Synch : CosTransactions::Synchronization{ }; };
Synchronizationオブジェクトの例
/***********************************************************************/ /* Synchronization application */ /***********************************************************************/ #include <windows.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include "ORB.H" #include "COSNAMIN.H" #include "OTS.H" #include "bankAh" #define IMPL_bankA “IDL:bankA:1.0” static char *error_id; /* Global Variable */ static CORBA_ORB orb; static CORBA_BOA boa; static CORBA_Repository intf_rep; static FJ_ImplementationRep impl_rep; static CORBA_InterfaceDef intf; static CORBA_ImplementationDef impl; static CORBA_Environment env_g; static CosNaming_NamingContext cos_naming; static CosNaming_Name name2; static CosNaming_NameComponent name_component2; static void env_check( CORBA_string pos ) { if ( env_g._major != CORBA_NO_EXCEPTION ){ fprintf( stderr, "env_check: %s fails\n", pos ); error_id = CORBA_exception_id( &env_g ); fprintf(stderr,"%s\n",error_id); exit(1); } } /* before_completion */ void bankA_Synch_before_completion( bankA_Synch obj, CORBA_Environment *env ) { fprintf(stderr,"bankA_Sync_before_completion \n"); return; } /* after_completion */ void bankA_Synch_after_completion( bankA_Synch obj, CosTransactions_Status status, CORBA_Environment *env ) { fprintf(stderr,"bankA_Sync_after_completion %d\n",status); return; } int main( argc, argv ) int argc; char *argv[]; { int cr_argc = argc; CORBA_Object ots; /* Initialization of CORBA service */ orb = CORBA_ORB_init( &cr_argc, argv, FJ_OM_ORBid , &env_g); env_check( "CORBA_ORB_BOA_init" ); fprintf(stderr,"ORB_INIT OK\n"); boa = CORBA_ORB_BOA_init( orb, &cr_argc, argv, CORBA_BOA_OAid, &env_g ); env_check( "CORBA_ORB_BOA_init" ); fprintf(stderr,"BOA_INIT OK\n"); /* get the OR of InterfaceRepository */ intf_rep = CORBA_ORB_resolve_initial_references( orb, CORBA_ORB_ObjectId_LightInterfaceRepository, &env_g ); env_check( "CORBA_ORB_resolve_initial_references" ); /* get the OR of ImplementationRepository */ impl_rep = CORBA_ORB_resolve_initial_references( orb,CORBA_ORB_ObjectId_ImplementationRepository,&env_g ); env_check( "CORBA_ORB_resolve_initial_references" ); impl = FJ_ImplementationRep_lookup_id( impl_rep,_IMPL_bankA_Synch,&env_g ); env_check( "FJ_ImplementationRep_lookup_id" ); intf = CORBA_Repository_lookup_id( intf_rep, _INTF_ bankA_Synch, &env_g ); env_check( "CORBA_Repository_lookup_id" ); CORBA_BOA_impl_is_ready( boa, impl, &env_g ); env_check( "CORBA_BOA_impl_is_ready" ); return 0; }