TimesTen Scaleoutのクライアント・ルーティングAPI
この項では、TimesTen Scaleoutのクライアント・ルーティングAPIについて説明します。
クライアント・ルーティングAPIの機能
パフォーマンスを向上させるために、TimesTen Scaleoutでは、クライアント・アプリケーションにおいて接続を要素にハッシュ分散キーのキー値に基づいてルーティングできます。
分散キーのキー値を指定すると、TimesTen Scaleoutはデータベースによりその値が割り当てられている要素ID (またはレプリカ・セットID)の配列を戻します。これにより、クライアント・アプリケーションは指定されたキー値を持つ行が格納されている要素に接続できるようになり、行が格納されている要素とアプリケーションに接続されている要素との間で不要な通信が発生するのを回避できます。
分散キーの作成
クライアント・アプリケーションは、分散キーを識別および作成する必要があります。これは特定のキー値のセットを割り当てる要素(またはレプリカ・セット)を特定するのに必要です。TimesTenDistributionKey
およびTimesTenDistributionKeyBuilder
インタフェースは、分散キーを作成するための機能を指定します。
ノート:
アプリケーションは、分散キーの作成、要素IDまたはレプリカ・セットIDの計算、およびこの3つのいずれかに基づくデータベースの要素への接続の作成のために、データベースへのクライアント接続を保持する必要があります。「一連のキー値に与えられた要素の位置の取得」および「分散キーに基づく要素への接続」という2つの項にある例を参照してください。
TimesTenDistributionKeyBuilder
インタフェースでは次のビルダー・メソッドを指定して、データ型が異なる複合キーをサポートします。
subkey(Object subkey, java.sql.Types subkeyType)
複合分散キーの場合、表のハッシュ分散キーのすべての列に対してsubkey
メソッドを1回ずつ呼び出します。各subkey
を、表の分散キー列のキー値およびタイプと同じ順序で起動します。
一連のキー値に与えられた要素の位置の取得
分散キーを作成したら、分散キーで指定されているキー値を格納する要素IDまたはレプリカ・セットIDを取得するのにTimesTenDistributionKey
インタフェースのgetElementIDs
またはgetReplicaSetID
メソッドを使用します。
ノート:
TimesTen Scaleoutの場合、TimesTenDataSource
クラスに接続および分散キーのファクトリ・メソッドが実装されています。
この例では、単一列の分散キーのキー値の要素IDおよびレプリカ・セットIDを計算して出力します。
import java.sql.SQLException;
import java.sql.Types;
import com.timesten.jdbc.TimesTenDataSource;
import com.timesten.jdbc.TimesTenDistributionKey;
import com.timesten.jdbc.TimesTenDistributionKeyBuilder;
public class ClientRouting {
public static void main(String[] args) {
try {
/* Establish a connection to an element of the database. Maintain this
* connection for the duration of the application for computing element
* IDs and creating connections. */
TimesTenDataSource ds = new TimesTenDataSource();
ds.setUrl("jdbc:timesten:client:database1");
ds.setUser("terry");
ds.setPassword("password");
/* Build a distribution key. The distribution key is composed of a single
* TT_INTEGER column. */
TimesTenDistributionKey dk = ds.createTimesTenDistributionKeyBuilder()
.subkey(3, Types.INTEGER)
.build();
// Get the element IDs for the distribution key.
short[] elementIDs = dk.getElementIDs();
for (short id : elementIDs) {
System.out.println("Distribution key(3), element ID: " + id);
}
// Get the replica set ID for the disitribution key.
System.out.println("Distribution key(3), replica set ID: " +
dk.getReplicaSetID());
} catch (SQLException ex) { ... }
}
}
このコード・スニペットでは、複数の列で構成される分散キーのキー値のセットの要素IDおよびレプリカ・セットIDを計算して出力します。
...
/* Build a distribution key. The distribuion key is composed of two columns --
* one TT_INTEGER and one VARCHAR2. */
dk = ds.createTimesTenDistributionKeyBuilder()
.subkey(1, Types.INTEGER)
.subkey("john.doe", Types.VARCHAR)
.build();
// Get the element IDs for the distribution key
elementIDs = dk.getElementIDs();
for (short id : elementIDs) {
System.out.println("Distribution key(1, john.doe), element ID: " + id);
}
// Get the replica set ID for the distribution key.
System.out.println("Distribution key(1, john.doe), replica set ID: " +
dk.getReplicaSetID());
...
分散キーに基づく要素への接続
クライアント・アプリケーションは、TimesTen Scaleoutのデータベースの特定要素に接続するのに任意のカスタム・メソッドを使用できます。ただし、TimesTenConnectionBuilder
インタフェースで指定されている機能では、分散キー、要素IDまたはレプリカ・セットIDに基づく最適な要素へのアプリケーションの接続を可能にします。
この例では、分散キーを作成し、それにより接続を作成します。
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import com.timesten.jdbc.TimesTenDataSource;
import com.timesten.jdbc.TimesTenDistributionKey;
import com.timesten.jdbc.TimesTenDistributionKeyBuilder;
public class ClientRouting {
public static void main(String[] args) {
try {
// Create and mantain connection to database.
TimesTenDataSource ds = new TimesTenDataSource();
ds.setUrl("jdbc:timesten:client:database1");
ds.setUser("terry");
ds.setPassword("password");
// Build a distribution key.
TimesTenDistributionKey dk = ds.createTimesTenDistributionKeyBuilder()
.subkey(1, Types.INTEGER)
.subkey("john.doe", Types.VARCHAR)
.build();
// Connect to optimal element based on a distribuion key.
Connection conn;
conn = ds.createTimesTenConnectionBuilder()
.user("terry")
.password("password")
.distributionKey(dk)
.build();
Statement stmt = conn.createStatement();
stmt.execute("... SQL statement here ...");
stmt.close();
conn.close();
} catch (SQLException ex) {
...
}
}
}
次のコード・スニペットでは、要素IDに基づいて接続を作成します。
...
// Connect to optimal element based on an element ID.
short[] elementIDs = dk.getElementIDs();
conn = ds.createTimesTenConnectionBuilder()
.user("terry")
.password("password")
.elementID(elementIDs[0])
.build();
Statement stmt = conn.createStatement();
stmt.execute("... SQL statement here ...");
stmt.close();
conn.close();
...
このコード・スニペットでは、レプリカ・セットIDに基づいて接続を作成します。
...
// Connect to optimal element based on a replica set ID.
short repSetID = dk.getReplicaSetID();
conn = ds.createTimesTenConnectionBuilder()
.user("terry")
.password("password")
.replicaSetID(repSetID)
.build();
Statement stmt = conn.createStatement();
stmt.execute("... SQL statement here ...");
stmt.close();
conn.close();
...
サポートされているデータ型
最高のパフォーマンスが得られるように、推奨されるオブジェクト型を使用して型の変換を回避してください。
表2-5に、サポートされているデータ型および使用可能なオブジェクト・タイプを示します。
表2-5 サポートされているデータ型および使用可能なオブジェクト・タイプ
SQLの型 | java.sql.Types | 推奨されるオブジェクト・クラス | 使用可能なオブジェクト・クラス |
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
制限事項
クライアント・ルーティングには制限事項があります。
TimesTen Scaleoutでのクライアント・ルーティングのJDBC拡張機能には、『Oracle TimesTen In-Memory Database C開発者ガイド』のTimesTen Scaleoutのクライアント・ルーティングAPIにリストされているのと同じ制限事項が当てはまります。