Interacting with databases by using the JavaCompute node

Access databases from Java™ code included in the JavaCompute node.

About this task

Choose from the following options for database interaction:

If you use JDBCProvider for type 4 connections or MbSQLStatement, the databases that you access can participate in globally coordinated transactions. In all other cases, database access cannot be globally coordinated.

MbSQLStatement

About this task

The MbSQLStatement class provides full transactional database access by using ESQL and ODBC. Global coordination is provided by IBM® MQ on distributed systems, and by RRS on z/OS®.

Create instances of the MbSQLStatement class by using the createSQLStatement() method of MbNode, passing to the method the ODBC data source, an EQSL statement, and, optionally, the transaction mode.
  • Calling select() on this object returns the results of the query.
  • Calling execute() on this object runs a query where no results are returned, such as updating a table.
The following Java code shows how to access a database by using MbSQLStatement:
MbMessage newMsg = new MbMessage(assembly.getMessage());
MbMessageAssembly newAssembly = new MbMessageAssembly(assembly, newMsg);

String table = "dbTable";

MbSQLStatement state = createSQLStatement( "dbName", 
	"SET OutputRoot.XMLNS.integer[] = PASSTHRU('SELECT * FROM " + table + "');" );

state.setThrowExceptionOnDatabaseError(false);
state.setTreatWarningsAsErrors(true);
state.select( assembly, newAssembly );

int sqlCode = state.getSQLCode(); 
if(sqlCode != 0)
{
	// Do error handling here 
}

getOutputTerminal("out").propagate(assembly); 

JDBC API in an unmanaged environment

About this task

You can access standard Java APIs in the code that you write for your JavaCompute nodes, including JDBC calls. Therefore, you can use JDBC APIs to connect to a database, write to or read from the database, and disconnect from the database. On operating systems other than z/OS, your JDBC connection code can call both type 2 and type 4 JDBC drivers in this environment, but they are not supplied. You must obtain these drivers from your database vendor. On z/OS, type 2 drivers are not supported.

If you choose this method to access databases, managing the transactions is not supported; your code must manage the local commit and rollback of database changes. Your code must also manage the connection lifecycle, connection thread affinity, and connection pooling. You must also monitor the access to databases when you use this technique to ensure that these connections do not cause interference with connections that are made by the integration server. In particular, be aware that type 2 drivers bridge to an ODBC connection that might be in use in message flows that access databases from ESQL.

SQLJ

About this task

SQLJ is a Java extension that you can use to embed static SQL statements within Java code. Create SQLJ files by using the IBM App Connect Enterprise Toolkit. The resource manager does not coordinate database access when using SQLJ.

Procedure

Create an SQLJ file within a Java project by completing the following steps:

  1. Right-click the Java project in which you want to create the file.
  2. Select New > Other
  3. Expand Data.
  4. Expand SQLJ Applications.
  5. Select SQLJ File.
  6. Click Next.
  7. Follow the directions given by the New SQLJ File wizard to generate the SQLJ file.

Results

You can now reference the class in this SQLJ file from a JavaCompute node class in this project or in another referenced project.