この章では、Oracle OLAP Java APIのTransactionおよびTransactionProviderインタフェースについて説明します。また、アプリケーションにおいてこれらのインタフェースの実装を使用する方法についても説明します。TransactionProviderはDataProviderから取得します。TransactionProviderのcommitCurrentTransactionメソッドを使用すると、データベース内の永続記憶域にメタデータ・オブジェクトを保存できます。また、このメソッドは、導出Sourceの作成後かつSourceのCursorの作成前に使用します。メタデータ・オブジェクトの作成後にTransactionをコミットする例については、第4章を参照してください。
この章では、次の項目について説明します。
Oracle OLAP Java APIは、トランザクション処理を行います。問合せのSourceオブジェクトまたはメタデータ・オブジェクトの作成は、Transactionのコンテキスト内で実行されます。TransactionProviderは、アプリケーションにTransactionオブジェクトを提供し、それらのTransactionオブジェクトをコミットまたは破棄します。
TransactionProviderによって、次のことが保証されます。
Transactionが、その他のTransactionオブジェクトから分離されます。Transactionで実行中の操作は、別のTransactionオブジェクトでは参照できません(影響もありません)。
Transactionでの操作に失敗した場合、その操作の結果は取り消されます(そのTransactionがロールバックされます)。
完了したTransactionの結果は存続します。
別のSourceのメソッドをコールして導出Sourceを作成する場合、その導出Sourceは、現行のTransactionのコンテキスト内で作成されます。Sourceは、それを作成したTransactionの中またはそのTransactionの子Transactionの中でアクティブです。
TransactionProviderのメソッドをコールして、現行のTransactionを取得または設定します。または、子Transactionを開始します。子Transactionでは、ディメンション要素の選択を変更したり、データに対して様々な算術演算または分析処理を実行して、問合せを変更できます。これにより、親Transactionで作成したTemplateの状態を変更できます。Templateによって生成されたSourceが指定するデータを親Transactionおよび子Transactionで表示することによって、アプリケーションのエンド・ユーザーは簡単に問合せを変更でき、同じデータセットに対する異なる操作の結果や異なるデータセットに対する同じ操作の結果を表示できます。
OLAP Java APIには、次の2つのタイプのTransactionオブジェクトが存在します。
読取りTransaction。現行のTransactionは、最初は読取りTransactionです。読取りTransactionは、Cursorを作成してOracle OLAPからデータをフェッチする場合に必要です。Cursorオブジェクトの詳細は、第9章を参照してください。
書込みTransaction。書込みTransactionは、導出Sourceの作成またはTemplateの状態の変更を行う場合に必要です。導出Sourceを作成する方法の詳細は、第5章を参照してください。Templateオブジェクトの詳細は、第10章を参照してください。
最初の読取りTransactionにおいて、導出Sourceを作成した場合、またはTemplateオブジェクトの状態を変更した場合、子の書込みTransactionが自動的に生成されます。この子Transactionが、現行のTransactionになります。
その後、導出Sourceをもう1つ作成するか、またはTemplateの状態を再度変更した場合、その操作は同じ書込みTransactionで実行されます。同じ書込みTransactionで、導出Sourceオブジェクトをいくつでも作成でき、Templateの状態も何回でも変更できます。これらのSourceオブジェクト、またはTemplateによって作成されたSourceを使用すると、複雑な問合せを定義できます。
Cursorを作成して、導出Sourceによって指定される結果セットをフェッチする前に、子の書込みTransactionから親の読取りTransactionにSourceを移動する必要があります。これを行うには、Transactionをコミットする必要があります。
子Transactionで作成したSourceを親の読取りTransactionに移動するには、TransactionProviderのcommitCurrentTransactionメソッドをコールします。子の書込みTransactionをコミットした場合、子Transactionで作成したSourceは、親の読取りTransactionに移動します。子Transactionは削除され、親Transactionが現行のTransactionになります。Sourceは、現行の読取りTransactionでアクティブであるため、そのSourceのCursorを作成できます。
例7-1のcommit()は、現行のTransactionをコミットするメソッドです。例の中のdpは、DataProviderです。
現行のTransactionの取得および設定、子Transactionの開始およびTransactionのロールバックを実行すると、エンド・ユーザーは、動的問合せの任意の状態から様々な選択を実行できます。
エンド・ユーザーに同じ最初の問合せに基づいた代替を提供するには、次の手順を実行します。
親TransactionにTemplateを作成し、Templateの初期状態を設定します。
Templateによって作成されたSourceを取得します。Cursorを作成して結果セットを検索し、Cursorから値を取得します。その後、エンド・ユーザーに結果を表示します。
子Transactionを開始して、Templateの状態を変更します。
子TransactionでTemplateによって作成されたSourceを取得します。Cursorを作成して、取得した値を表示します。
その後、最初のTemplateの状態を2番目のものと置換できます。2番目の状態を廃棄して、1番目のものを保持することもできます。
子の読取りTransactionを開始するには、使用中のTransactionProviderのbeginSubtransactionメソッドをコールします。子の読取りTransactionでTemplateの状態を変更する場合、子の書込みTransactionが自動的に開始されます。書込みTransactionは、子の読取りTransactionの子です。
Templateによって作成されたSourceが指定するデータを取得するには、親の読取りTransactionへの書込みTransactionをコミットします。その後、Cursorを作成してデータをフェッチできます。Templateの変更後の状態は、元の親では参照できません。親の読取りTransactionに子の読取りTransactionをコミットするまでは、変更後の状態は、親では参照できません。
子の読取りTransactionを開始した後、その子の子の読取りTransaction(最初の親Transactionの孫)を開始できます。子および孫Transactionオブジェクトの作成方法の例については、例7-3を参照してください。
使用中のTransactionProviderのrollbackCurrentTransactionメソッドをコールすることによって、Transactionをロールバック(取消し)できます。Transactionをロールバックすることによって、Transaction中に実行されたすべての変更が廃棄され、Transactionが削除されます。
Transactionをロールバックする前に、そのTransactionで作成したすべてのCursorManagerオブジェクトをクローズする必要があります。Transactionをロールバックした後、作成したすべてのSourceオブジェクト、またはTransactionで実行したTemplate状態の変更が無効になります。そのSourceオブジェクト用に作成したすべてのCursorも無効になります。
Transactionをロールバックすると、そのTransactionをコミットすることはできません。同様に、Transactionをコミットした後は、それをロールバックすることはできません。
例7-2 Transactionのロールバック
次の例では、TopBottomTemplateおよびSingleSelectionTemplateクラスを使用します(第10章「動的問合せの作成」を参照)。この例では、例10-4「Templateによって作成されたSourceの取得」と同じコードを使用してTopBottomTemplateおよびSingleSelectionTemplateオブジェクトを作成しています。例7-2には、そのコードは表示されていません。この例では、TopBottomTemplateの状態を設定します。まず、子Transactionが開始されて、TopBottomTemplateが異なる状態に設定されます。その後、子Transactionがロールバックされます。printlnメソッドはCursorPrintWriterオブジェクトを介してテキストを表示します。getContextメソッドは、Cursorオブジェクトを作成しCursorPrintWriterを介してその値を表示するメソッドを持つContext11gオブジェクトを取得します。CursorPrintWriterおよびContext11gクラスは、このマニュアルのサンプル・プログラムで使用されています。
// The current Transaction is a read Transaction, t1.
// Create a TopBottomTemplate using a hierarchy of the Product dimension
// as the base and dp as the DataProvider.
TopBottomTemplate topNBottom = new TopBottomTemplate(prodHier, dp);
// Changing the state of a Template requires a write Transaction, so a
// write child Transaction, t2, is automatically started.
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_TOP);
topNBottom.setN(10);
topNBottom.setCriterion(singleSelections.getSource());
// Get the TransactionProvider and commit the Transaction t2.
TransactionProvider tp = dp.getTransactionProvider();
try
{
tp.commitCurrentTransaction(); // t2 disappears
}
catch(Exception e)
{
println("Cannot commit the Transaction. " + e);
}
// The current Transaction is now t1.
// Get the dynamic Source produced by the TopBottomTemplate.
Source result = topNBottom.getSource();
// Create a Cursor and display the results
println("\nThe current state of the TopBottomTemplate" +
"\nproduces the following values:\n");
getContext().displayTopBottomResult(result);
// Start a child Transaction, t3. It is a read Transaction.
tp.beginSubtransaction(); // t3 is the current Transaction
// Change the state of topNBottom. Changing the state requires a
// write Transaction so Transaction t4 starts automatically.
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_BOTTOM);
topNBottom.setN(15);
// Commit the Transaction.
try
{
tp.commitCurrentTransaction(); // t4 disappears
}
catch(Exception e)
{
println("Cannot commit the Transaction. " + e);
}
// Create a Cursor and display the results. // t3 is the current Transaction
println("\nIn the child Transaction, the state of the" +
"\nTopBottomTemplate produces the following values:\n");
getContext().displayTopBottomResult(result);
// The displayTopBottomResult method closes the CursorManager for the
// Cursor created in t3.
// Undo t3, which discards the state of topNBottom that was set in t4.
tp.rollbackCurrentTransaction(); // t3 disappears
// Transaction t1 is now the current Transaction and the state of
// topNBottom is the one defined in t2.
// To show the current state of the TopNBottom template Source, commit
// the Transaction, create a Cursor, and display the Cursor values.
try
{
tp.commitCurrentTransaction();
}
catch(Exception e)
{
println("Cannot commit the Transaction. " + e);
}
println("\nAfter rolling back the child Transaction, the state of"
+ "\nthe TopBottomTemplate produces the following values:\n");
getContext().displayTopBottomResult(result);
例7-2の出力は、次のようになります。
The current state of the TopBottomTemplate produces the following values: 1. PRODUCT_PRIMARY::TOTAL_PRODUCT::TOTAL 2. PRODUCT_PRIMARY::CLASS::SFT 3. PRODUCT_PRIMARY::FAMILY::ACC 4. PRODUCT_PRIMARY::CLASS::HRD 5. PRODUCT_PRIMARY::FAMILY::MOD 6. PRODUCT_PRIMARY::FAMILY::OS 7. PRODUCT_PRIMARY::FAMILY::DISK 8. PRODUCT_PRIMARY::ITEM::MOUSE PAD 9. PRODUCT_PRIMARY::ITEM::OS 1 USER 10. PRODUCT_PRIMARY::ITEM::DLX MOUSE In the child Transaction, the state of the TopBottomTemplate produces the following values: 1. PRODUCT_PRIMARY::ITEM::EXT CD ROM 2. PRODUCT_PRIMARY::ITEM::OS DOC ITA 3. PRODUCT_PRIMARY::ITEM::OS DOC SPA 4. PRODUCT_PRIMARY::ITEM::INT CD USB 5. PRODUCT_PRIMARY::ITEM::ENVY EXT KBD 6. PRODUCT_PRIMARY::ITEM::19 SVGA 7. PRODUCT_PRIMARY::ITEM::OS DOC FRE 8. PRODUCT_PRIMARY::ITEM::OS DOC GER 9. PRODUCT_PRIMARY::ITEM::ENVY ABM 10. PRODUCT_PRIMARY::ITEM::INT CD ROM 11. PRODUCT_PRIMARY::ITEM::ENVY EXE 12. PRODUCT_PRIMARY::ITEM::OS DOC KAN 13. PRODUCT_PRIMARY::ITEM::ENVY STD 14. PRODUCT_PRIMARY::ITEM::1GB USB DRV 15. PRODUCT_PRIMARY::ITEM::SENT MM After rolling back the child Transaction, the state of the TopBottomTemplate produces the following values: 1. PRODUCT_PRIMARY::TOTAL_PRODUCT::TOTAL 2. PRODUCT_PRIMARY::CLASS::SFT 3. PRODUCT_PRIMARY::FAMILY::ACC 4. PRODUCT_PRIMARY::CLASS::HRD 5. PRODUCT_PRIMARY::FAMILY::MOD 6. PRODUCT_PRIMARY::FAMILY::OS 7. PRODUCT_PRIMARY::FAMILY::DISK 8. PRODUCT_PRIMARY::ITEM::MOUSE PAD 9. PRODUCT_PRIMARY::ITEM::OS 1 USER 10. PRODUCT_PRIMARY::ITEM::DLX MOUSE
Oracle OLAP Java APIでは、DataProviderがTransactionProviderインタフェースの実装を提供します。TransactionProviderによって、アプリケーションにTransactionオブジェクトが提供されます。
「Transactionのコミット」で説明したように、子の書込みTransactionで作成した導出Sourceを親の読取りTransactionで参照できるようにするには、commitCurrentTransactionメソッドを使用します。その後、そのSource用のCursorを作成できます。
アプリケーションでTemplateオブジェクトを使用している場合は、TransactionProviderの他のメソッドを使用して次の操作を行うこともできます。
子Transactionを開始します。
保存するために、現行のTransactionを取得します。
現行のTransactionを以前に保存したものに設定します。
現行のTransactionをロールバック(取消し)します。これによって、Transactionで行われたすべての変更が廃棄されます。Transactionは、ロールバックされた後は無効となりコミットできません。Transactionは、コミットした後はロールバックできません。TransactionでSourceのCursorを作成した場合、そのTransactionをロールバックする前にCursorManagerをクローズする必要があります。
例7-3では、Transactionオブジェクトを使用して動的問合せを変更する例を示します。例7-2と同様に、この例でも例10-4「Templateによって作成されたSourceの取得」と同じコードを使用してTopBottomTemplateおよびSingleSelectionTemplateオブジェクトを作成しています。この例には、そのコードは表示されていません。
例では、Transactionオブジェクトを追跡できるように、getCurrentTransactionメソッドをコールすることによって別のTransactionオブジェクトが保存されています。例におけるtpは、TransactionProviderです。printlnメソッドはCursorPrintWriterを介してテキストを表示します。getContextメソッドは、Cursorオブジェクトを作成しCursorPrintWriterを介してその値を表示するメソッドを持つContext11gオブジェクトを取得します。commitメソッドは、例7-1のメソッドです。
例7-3 子Transactionオブジェクトの使用方法
// The parent Transaction is the current Transaction at this point.
// Save the parent read Transaction as parentT1.
Transaction parentT1 = tp.getCurrentTransaction();
// Get the dynamic Source produced by the TopBottomTemplate.
Source result = topNBottom.getSource();
// Create a Cursor and display the results.
println("\nThe current state of the TopBottomTemplate" +
"\nproduces the following values:\n");
getContext().displayTopBottomResult(result);
// Begin a child Transaction of parentT1.
tp.beginSubtransaction(); // This is a read Transaction.
// Save the child read Transaction as childT2.
Transaction childT2 = tp.getCurrentTransaction();
// Change the state of the TopBottomTemplate. This starts a
// write Transaction, a child of the read Transaction childT2.
topNBottom.setN(12);
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_BOTTOM);
// Save the child write Transaction as writeT3.
Transaction writeT3 = tp.getCurrentTransaction();
// Commit the write Transaction writeT3.
commit();
// The commit moves the changes made in writeT3 into its parent,
// the read Transaction childT2. The writeT3 Transaction
// disappears. The current Transaction is now childT2
// again but the state of the TopBottomTemplate has changed.
// Create a Cursor and display the results of the changes to the
// TopBottomTemplate that are visible in childT2.
try
{
println("\nIn the child Transaction, the state of the" +
"\nTopBottomTemplate produces the following values:\n");
getContext().displayTopBottomResult(result);
}
catch(Exception e)
{
println("Cannot display the results of the query. " + e);
}
// Begin a grandchild Transaction of the initial parent.
tp.beginSubtransaction(); // This is a read Transaction.
// Save the grandchild read Transaction as grandchildT4.
Transaction grandchildT4 = tp.getCurrentTransaction();
// Change the state of the TopBottomTemplate. This starts another
// write Transaction, a child of grandchildT4.
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_TOP);
// Save the write Transaction as writeT5.
Transaction writeT5 = tp.getCurrentTransaction();
// Commit writeT5.
commit();
// Transaction grandchildT4 is now the current Transaction and the
// changes made to the TopBottomTemplate state are visible.
// Create a Cursor and display the results visible in grandchildT4.
try
{
println("\nIn the grandchild Transaction, the state of the" +
"\nTopBottomTemplate produces the following values:\n");
getContext().displayTopBottomResult(result);
}
catch(Exception e)
{
println(""Cannot display the results of the query. " + e);
}
// Commit the grandchild into the child.
commit();
// Transaction childT2 is now the current Transaction.
// Instead of preparing and committing the grandchild Transaction,
// you could rollback the Transaction, as in the following
// method call:
// rollbackCurrentTransaction();
// If you roll back the grandchild Transaction, then the changes
// you made to the TopBottomTemplate state in the grandchild
// are discarded and childT2 is the current Transaction.
// Commit the child into the parent.
commit();
// Transaction parentT1 is now the current Transaction. Again,
// you can roll back the childT2 Transaction instead of committing it.
// If you do so, then the changes that you made in childT2 are discarded.
// The current Transaction is be parentT1, which has the original state
// of the TopBottomTemplate, without any of the changes made in the
// grandchild or the child transactions.
例7-3の出力は、次のようになります。
The current state of the TopBottomTemplate produces the following values: 1. PRODUCT_PRIMARY::TOTAL_PRODUCT::TOTAL 2. PRODUCT_PRIMARY::CLASS::SFT 3. PRODUCT_PRIMARY::FAMILY::ACC 4. PRODUCT_PRIMARY::CLASS::HRD 5. PRODUCT_PRIMARY::FAMILY::MOD 6. PRODUCT_PRIMARY::FAMILY::OS 7. PRODUCT_PRIMARY::FAMILY::DISK 8. PRODUCT_PRIMARY::ITEM::MOUSE PAD 9. PRODUCT_PRIMARY::ITEM::OS 1 USER 10. PRODUCT_PRIMARY::ITEM::DLX MOUSE In the child Transaction, the state of the TopBottomTemplate produces the following values: 1. PRODUCT_PRIMARY::ITEM::EXT CD ROM 2. PRODUCT_PRIMARY::ITEM::OS DOC ITA 3. PRODUCT_PRIMARY::ITEM::OS DOC SPA 4. PRODUCT_PRIMARY::ITEM::INT CD USB 5. PRODUCT_PRIMARY::ITEM::ENVY EXT KBD 6. PRODUCT_PRIMARY::ITEM::19 SVGA 7. PRODUCT_PRIMARY::ITEM::OS DOC FRE 8. PRODUCT_PRIMARY::ITEM::OS DOC GER 9. PRODUCT_PRIMARY::ITEM::ENVY ABM 10. PRODUCT_PRIMARY::ITEM::INT CD ROM 11. PRODUCT_PRIMARY::ITEM::ENVY EXE 12. PRODUCT_PRIMARY::ITEM::OS DOC KAN In the grandchild Transaction, the state of the TopBottomTemplate produces the following values: 1. PRODUCT_PRIMARY::TOTAL_PRODUCT::TOTAL 2. PRODUCT_PRIMARY::CLASS::SFT 3. PRODUCT_PRIMARY::FAMILY::ACC 4. PRODUCT_PRIMARY::CLASS::HRD 5. PRODUCT_PRIMARY::FAMILY::MOD 6. PRODUCT_PRIMARY::FAMILY::OS 7. PRODUCT_PRIMARY::FAMILY::DISK 8. PRODUCT_PRIMARY::ITEM::MOUSE PAD 9. PRODUCT_PRIMARY::ITEM::OS 1 USER 10. PRODUCT_PRIMARY::ITEM::DLX MOUSE 11. PRODUCT_PRIMARY::ITEM::LT CASE 12. PRODUCT_PRIMARY::ITEM::56KPS MODEM