/** The proxy datasource resource reference name */ private static String proxyResRef = "jdbc/proxyDS"; /** DataSource resource reference 1 */ private static String resRef1 = "jdbc/myDS1"; /** DataSource resource reference 2 */ private static String resRef2 = "jdbc/myDS2"; /** Global datasource JNDI name for the DataSource resource reference 1 */ private String globalDSJNDIName1 = null; /** Global datasource JNDI name for the DataSource resource reference 2 */ private String globalDSJNDIName2 = null; /** Globle datasource JNDI name for the Proxy Datasource resource reference */ private String globalProxyDSJNDIName = null;WSProxyDataSourceHelper インスタンスも定義されているので、 このセッション Bean でそのインスタンスを使用して、データ・ソース JNDI 名を解決し、 トランザクションに対してそのデータ・ソース JNDI 名を設定できます。
/** * the WSProxyDataSourceHelper instance used to set the datasource JNDI name * for the current transaction and resolve the datasource resource reference * to the global JNDI name */ WSProxyDataSourceHelper dsHelper = null;setSessionContext メソッドは次のとおりです。
public void setSessionContext(javax.ejb.SessionContext ctx) { mySessionCtx = ctx; // Lookup the WSProxyDataSourceHelper, and the EJB local homes try { InitialContext ic = new InitialContext(); dsHelper = (WSProxyDataSourceHelper) ic.lookup(WSProxyDataSourceHelper.JNDI_NAME); accountHome = (AccountLocalHome) ic.lookup(accountHomeJNDIName); accountOwnerHome = (AccountOwnerLocalHome) ic.lookup(accountOwnerHomeJNDIName); } catch (Exception e) { throw new EJBException(e); } // Get the server name AdminService service = AdminServiceFactory.getAdminService(); SERVER_NAME = service.getCellName() + "/" + service.getNodeName() + "/" + AdminServiceFactory.getAdminService().getProcessName(); // Resolve the resource references to the global datasource JNDI names try { globalDSJNDIName1 = dsHelper.resolveDataSourceReference(resRef1); globalDSJNDIName2 = dsHelper.resolveDataSourceReference(resRef2); globalProxyDSJNDIName = dsHelper.resolveDataSourceReference(proxyResRef); } catch (ResRefNotFoundException rrnfe) { throw new EJBException(rrnfe);以下のステートメントを使用して、JNDI ネーム・スペースから WSProxyDataSourceHelper を検索できます。
dsHelper = (WSProxyDataSourceHelper ) ic.lookup(WSProxyDataSourceHelper .JNDI_NAME);次に、通常どおり、AccountLocalHome および AccountOwnerLocalHome を検索します。 続いて、AdminService API を使用して、サーバー名を取得します。このサーバー名を使用して、 区画経路指定が正しいかどうかを検証します。ユーザーのアプリケーションでこの検証を行う必要はありません。 区画化機能ランタイムにより、区画経路指定は正しく動作するようになっているからです。
globalDSJNDIName1 = dsHelper.resolveDataSourceReference(resRef1);アプリケーションの開発段階では、 このリソース参照がどの JNDI 名にマップされるか開発者には分かりません。 Proxy Datasource コンポーネントには、アプリケーションがデータ・ソースの JNDI 名を取得するための API が用意されています。この JNDI 名のデータ・ソースにより、どのデータ・ソースを使用すべきかがランタイムに伝えられます。
/** * withdraw money from an account * @param accountId * @param amount * @return * @throws InSufficientFundException */ public String withdraw(String accountId, float amount) throws InSufficientFundException { // Set the datasource this transaction will access. setDataSource(accountId); try { AccountLocal account = accountHome.findByPrimaryKey(accountId); account.withdraw(amount); } catch (ObjectNotFoundException onfe) { throw new EJBException(onfe); } catch (FinderException fe) { throw new EJBException(fe); } return SERVER_NAME; }このメソッドと通常のメソッドとの違いは、次のステートメントが追加されていることです。
// Set the datasource this transaction will access. setDataSource(accountId);メソッド setDataSource は次のとおりです。
/** * Set the datasource the CMP is going to use for the current transaction. * If the accountID starts with W, datasource 1 will be used. If the accountID * starts with E, datasource 2 will be used. * * @param s */ private void setDataSource(String s) { if (s.startsWith("W")) { dsHelper.setCurrentDataSourceJndiName(globalProxyDSJNDIName, globalDSJNDIName1); } else { dsHelper.setCurrentDataSourceJndiName(globalProxyDSJNDIName, globalDSJNDIName2); } }setDataSource メソッドでは、WSProxyDataSourceHelper を使用して、 現在のトランザクションで使用する現行のデータ・ソース JNDI 名を設定します。accountId が W で始まる場合、 つまりアカウント ID を西海岸のデータベースに保管する必要がある場合は、 JNDI 名 globalDSJNDIName1 がスレッドに設定されます。accountId が E で始まる場合、つまりアカウント ID を東海岸のデータベースに 保管する必要がある場合は、JNDI 名 globalDSJNDIName2 がスレッドに設定されます。
これで、 アプリケーションはプロキシー・データ・ソース対応となりました。 次のステップでは、DB2 データ・ソースとそのプロキシー・データ・ソースの構成方法を紹介します。
Related concepts
WSAD のプロキシー・データ・ソース・サポートを使用した
アプリケーションの開発