WebSphere® eXtreme
Scale リソース・アダプターは、クライアント接続管理およびローカル・トランザクション・サポートを提供します。
このサポートによって、Java Platform, Enterprise Edition (Java EE) アプリケーションは、Java EE ローカル・トランザクションや eXtreme Scale API を使用して、eXtreme Scale のクライアント接続を調べたり、ローカル・トランザクションを区分したりすることができます。
オプションで、接続ハンドルを取得するための追加オプションを提供する com.ibm.websphere.xs.ra.XSConnectionFactory に javax.resource.cci.ConnectionFactory インスタンスをキャストすることができます。 結果の接続ハンドルは、getSession メソッドを提供する com.ibm.websphere.xs.ra.XSConnection インターフェースにキャストする必要があります。 getSession メソッドは com.ibm.websphere.objectgrid.Session オブジェクト・ハンドルを返します。このハンドルにより、アプリケーションが eXtreme Scale データ・アクセス API (ObjectMap API や EntityManager API など) をどれでも使用できるようになります。
Session ハンドルと派生オブジェクトは XSConnection ハンドルが存続している限り有効です。
以下の手順を使用して eXtreme Scale トランザクションを区分することができます。 それぞれの手順を混合することはできません。例えば、同じアプリケーション・コンポーネントという状況下でグローバル・トランザクション区分とローカル・トランザクション区分を混用することはできません。
// (C) Copyright IBM Corp. 2001, 2012.
// All Rights Reserved. Licensed Materials - Property of IBM.
package com.ibm.ws.xs.ra.test.ee;
import javax.naming.InitialContext;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.LocalTransaction;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import com.ibm.websphere.objectgrid.ObjectMap;
import com.ibm.websphere.objectgrid.Session;
import com.ibm.websphere.xs.ra.XSConnection;
/**
* This sample requires that it runs in a J2EE context in your
* application server. For example, using the JUnitEE framework servlet.
*
* The code in these test methods would typically reside in your own servlet,
* EJB, or other web component.
*
* The sample depends on a configured WebSphere eXtreme Scale connection
* factory registered at of JNDI Name of "eis/embedded/wxscf" that defines
* a connection to a grid containing a Map with the name "Map1".
*
* The sample does a direct lookup of the JNDI name and does not require
* resource injection.
*/
public class DocSampleTests extends TestCase {
public final static String CF_JNDI_NAME = "eis/embedded/wxscf";
public final static String MAP_NAME = "Map1";
Long key = null;
Long value = null;
InitialContext ctx = null;
ConnectionFactory cf = null;
public DocSampleTests() {
}
public DocSampleTests(String name) {
super(name);
}
protected void setUp() throws Exception {
ctx = new InitialContext();
cf = (ConnectionFactory)ctx.lookup(CF_JNDI_NAME);
key = System.nanoTime();
value = System.nanoTime();
}
/**
* This example runs when not in the context of a global transaction
* and uses autocommit.
*/
public void testLocalAutocommit() throws Exception {
Connection conn = cf.getConnection();
try {
Session session = ((XSConnection)conn).getSession();
ObjectMap map = session.getMap(MAP_NAME);
map.insert(key, value); // Or various data access operations
}
finally {
conn.close();
}
}
/**
* This example runs when not in the context of a global transaction
* and demarcates the transaction using session.begin()/session.commit()
*/
public void testLocalSessionTransaction() throws Exception {
Session session = null;
Connection conn = cf.getConnection();
try {
session = ((XSConnection)conn).getSession();
session.begin();
ObjectMap map = session.getMap(MAP_NAME);
map.insert(key, value); // Or various data access operations
session.commit();
}
finally {
if (session != null && session.isTransactionActive()) {
try { session.rollback(); }
catch (Exception e) { e.printStackTrace(); }
}
conn.close();
}
}
/**
* This example uses the LocalTransaction interface to demarcate
* transactions.
*/
public void testLocalTranTransaction() throws Exception {
LocalTransaction tx = null;
Connection conn = cf.getConnection();
try {
tx = conn.getLocalTransaction();
tx.begin();
Session session = ((XSConnection)conn).getSession();
ObjectMap map = session.getMap(MAP_NAME);
map.insert(key, value); // Or various data access operations
tx.commit(); tx = null;
}
finally {
if (tx != null) {
try { tx.rollback(); }
catch (Exception e) { e.printStackTrace(); }
}
conn.close();
}
}
/**
* This example depends on an externally managed transaction,
* the externally managed transaction might typically be present in
* an EJB with its transaction attributes set to REQUIRED or REQUIRES_NEW.
* NOTE: If there is NO global transaction active, this example runs in auto-commit
* mode because it doesn't verify a transaction exists.
*/
public void testGlobalTransactionContainerManaged() throws Exception {
Connection conn = cf.getConnection();
try {
Session session = ((XSConnection)conn).getSession();
ObjectMap map = session.getMap(MAP_NAME);
map.insert(key, value); // Or various data access operations
}
catch (Throwable t) {
t.printStackTrace();
UserTransaction tx = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
if (tx.getStatus() != Status.STATUS_NO_TRANSACTION) {
tx.setRollbackOnly();
}
}
finally {
conn.close();
}
}
/**
* This example demonstrates starting a new global transaction using the
* UserTransaction interface. Typically the container starts the global
* transaction (for example in an EJB with a transaction attribute of
* REQUIRES_NEW), but this sample will also start the global transaction
* using the UserTransaction API if it is not currently active.
*/
public void testGlobalTransactionTestManaged() throws Exception {
boolean started = false;
UserTransaction tx = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
if (tx.getStatus() == Status.STATUS_NO_TRANSACTION) {
tx.begin();
started = true;
}
// else { called with an externally/container managed transaction }
Connection conn = null;
try {
conn = cf.getConnection(); // Get connection after the global tran starts
Session session = ((XSConnection)conn).getSession();
ObjectMap map = session.getMap(MAP_NAME);
map.insert(key, value); // Or various data access operations
if (started) {
tx.commit(); started = false; tx = null;
}
}
finally {
if (started) {
try { tx.rollback(); }
catch (Exception e) { e.printStackTrace(); }
}
if (conn != null) { conn.close(); }
}
}
}