使用 Java EE 連接器架構連接器存取資料
若要從 WebSphere® Application Server中符合 Java™ EE 連接器架構 (JCA) 標準的應用程式存取資料,您可以配置並使用資源配接器和 Connection Factory。
關於這項作業
應用程式元件利用 Connection Factory 來存取連線實例,之後,這個元件便利用這個連線實例來連接基礎的企業資訊系統 (EIS)。 連線範例包括資料庫連線、「Java 訊息服務」連線及 SAP R/3 連線。
如 Java EE 連接器架構 (JCA) 規格所示,每一個企業資訊系統 (EIS) 都需要資源配接器和 Connection Factory。 然後會透過下列程式設計模型來存取這個 Connection Factory。 如果您使用 Rational ® Application Development 工具,則會為您產生下列大部分部署描述子及程式碼。 此範例顯示存取 EIS 資源的手動方法。
程序
範例
下列程式碼區段顯示應用程式元件如何建立互動並在 EIS 上實作它:
javax.resource.cci.ConnectionFactory connectionFactory = null;
javax.resource.cci.Connection connection = null;
javax.resource.cci.Interaction interaction = null;
javax.resource.cci.InteractionSpec interactionSpec = null;
javax.resource.cci.Record inRec = null;
javax.resource.cci.Record outRec = null;
try {
// Locate the application component and perform a JNDI lookup
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
connectionFactory = (javax.resource.cci.ConnectionFactory)
ctx.lookup("java:comp/env/eis/myConnection");
// create a connection
connection = connectionFactory.getConnection();
// Create Interaction and an InteractionSpec
interaction = connection.createInteraction();
interactionSpec = new InteractionSpec();
interactionSpec.setFunctionName("GET");
// Create input record
inRec = new javax.resource.cci.Record();
// Execute an interaction
interaction.execute(interactionSpec, inRec, outRec);
// Process the output...
} catch (Exception e) {
// Exception Handling
}
finally {
if (interaction != null) {
try {
interaction.close();
}
catch (Exception e) {/* ignore the exception*/}
}
if (connection != null) {
try {
connection.close();
}
catch (Exception e) {/* ignore the exception */}
}
}