uCosminexus Application Server, Expansion Guide
This subsection describes how to create a batch application to be connected to resources. This subsection also describes how to create a batch application and how to migrate from an existing batch application.
If you want to create a batch application, we recommend using DB Connector for connecting to resources. DB Connector is a resource adapter provided on Application Server used for connecting to a database. The following subsection describes how to connect to resources by using DB Connector.
<connector-runtime>
:
<resource-external-property>
<optional-name>optional name of DB Connector</optional-name>
</resource-external-property>
</connector-runtime>
|
String dbName = <Optional name of DB Connector>;
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
Connection con = ds.getConnection();
|
When migrating from an existing batch application (Java application), following two methods are used to connect to resources:
When you do not use DB Connector, you need not modify code of the batch application. However, you cannot use the functionality provided with DB Connector and the garbage collection control functionality. This section describes the migration method of changing the resource connection method to DB Connector and the migration method of using JDBC driver (without changing connection method).
If you want to use DB Connector, change the batch application so that you can acquire java.sql.Connection from DB Connector. You use the following method for changing batch applications:
<connector-runtime>
:
<resource-external-property>
<optional-name>optional name of DB Connector</optional-name>
</resource-external-property>
</connector-runtime>
|
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(uri,"user","pass");
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.executeBatch();
con.commit();
|
String dbName = <Optional name of DB Connector> InitialContext ic = new InitialContext(); DataSource ds = (DataSource)ic.lookup(dbName); Connection con = ds.getConnection(); con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.executeBatch(); con.commit(); |
You can use java.sql.Connection acquired from DB Connector in the same way as java.sql.Connection of JDBC driver. Hence, if you change only the acquisition method of java.sql.Connection, there is no need to change the code of other batch applications.
When using a JDBC driver, you need not modify code of a batch application. However, you must add libraries of the JDBC driver to be used to the class path of batch server. For details, follow the settings of the JDBC driver to be used. The following example describes how to add libraries of the JDBC driver to the class path of a batch server. To add libraries to the class path of a batch server, you add the following code to usrconf.cfg (option definition file for batch server):
add.class.path = full-path-of-library-of-JDBC-driver
For details on usrconf.cfg (option definition file for a batch server), see 3.2 usrconf.cfg (option definition file for batch server) in the uCosminexus Application Server Definition Reference Guide.
Note the following, when creating a batch application to be connected to resources:
Do not stop or change the settings of DB Connector, when a batch application is running. You stop or change the settings of DB Connector after the batch application ends.
With a batch server, connection is not automatically closed. Therefore, implement in the application in such a way so that the used connections close without fail.
You can use local transactions of JTA in a batch application. You use local transactions of JTA with the following methods:
For details on how to use the UserTransaction interface, see 3.4.8 Processing overview and points to be considered when using UserTransaction interface in the uCosminexus Application Server Common Container Functionality Guide.
The points to be considered when using UserTransaction are as follows:
If you start a transaction in a batch application, make sure to implement conclude processing in the batch application. If you close the batch application without implementing conclude processing of the transaction, the transaction is rolled back after a timeout time exceeds.
In this case, depending on the specified value of ejbserver.batch.application.exit.enabled parameter in Easy Setup definition file, behavior at the time of starting a transaction (javax.transaction.UserTransaction#begin()) in the batch application to be executed new, varies.
For details on the settings of the ejbserver.batch.application.exit.enabled parameter, see 2.3.10 Settings in the execution environment (batch server settings).
All Rights Reserved. Copyright (C) 2013, 2015, Hitachi, Ltd.