#using <mscorlib.dll>
#using <system.dll>
#using <Fujitsu.Shunsaku.dll>
using namespace System;
using namespace Fujitsu::Shunsaku;
class SampleTransaction {
public:
static void Main( String *args __gc[] ) {
ShunService *service = 0;
try {
// ShunService の作成
service = new ShunService();
// ホスト名 ポート番号を指定して Shunsaku に接続
service->Host = "DirSvr1";
service->Port = 33101;
service->ShunsakuFile = "shunsakuFile1";
service->Connect();
// 受講者数を増やしたいコースを検索するための検索条件を設定
ShunSearchRequirement *courseReq = new ShunSearchRequirement();
courseReq->QueryExpression = "/course/name == 'Business negotiation'";
courseReq->ReturnExpression = "/";
// 更新対象のコース情報を検索
ShunResultSet *courseRs = service->Search( courseReq );
// 検索結果からレコード情報を取得
ShunRecord *updateCourseRecord = courseRs->Records->Item[0];
// コース情報更新データ
updateCourseRecord->Data = String::Concat(
S"<course>",
S" <name>Business negotiation</name>",
S" <instructor>",
S" <first-name>Max</first-name>",
S" <last-name>cameron</last-name>",
S" </instructor>",
S" <capacity>40</capacity>",
S" <current-auditors>31</current-auditors>",
S"</course>" );
// 受講中の情報を追加したい生徒を検索するための検索式、リターン式を設定
ShunSearchRequirement *studentReq = new ShunSearchRequirement();
studentReq->QueryExpression = "/student/e-mail == 'mary\\.tompson@def\\.com'";
studentReq->ReturnExpression = "/";
// 更新対象の生徒情報を検索
ShunResultSet *studentRs = service->Search( studentReq );
// 検索結果からレコード情報を取得
ShunRecord *updateStudentRecord = studentRs->Records->Item[0];
// 生徒情報更新データ
updateStudentRecord->Data = String::Concat(
S"<student>",
S" <first-name>Mary</first-name>",
S" <last-name>Tompson</last-name>",
S" <e-mail>mary.tompson@def.com</e-mail>",
S" <course>Chinese language</course>",
S" <course>Business negotiation</course>",
S"</student>" );
// 自動コミットを無効に設定
service->AutoCommit = false;
try {
// コース情報の更新
service->Update( updateCourseRecord );
Console::WriteLine( "コース情報の更新終了" );
// 生徒情報の更新
service->Update( updateStudentRecord );
Console::WriteLine( "生徒情報の更新終了" );
// ここまでの更新をコミット
service->Commit();
}
catch ( Exception *e ) {
if ( service->State == ShunConnectionState::Open ) {
service->Rollback();
}
throw;
}
// Shunsaku から切断
service->Disconnect();
}
catch ( ShunContinuousException *e ) {
// ShunContinuousExceptionが発生した場合の処理を記述
try {
if( service != 0 && service->State == ShunConnectionState::Open ) {
service->Disconnect();
}
}
catch ( ShunException *ex ) {
Console::WriteLine( "エラーメッセージ : {0}", ex->Message );
}
Console::WriteLine( "エラーメッセージ : {0}", e->Message );
}
catch ( ShunConnectionTerminatedException *e ) {
// ShunConnectionTerminatedExceptionが発生した場合の処理を記述
Console::WriteLine( "エラーメッセージ : {0}", e->Message );
}
}
};
int main() {
return SampleTransaction::Main( Environment::GetCommandLineArgs() );
}
|