SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-24
Coding a Multithreaded SQLJ Program
This example uses different execution contexts for each thread:
public class MultiThread1 implements Runnable {
...
//main() method
public static void main (String [] args) {
try {
#sql { MODULE samdbcat.sales.mt1mod };
showPrices();
//Instantiating Runnable objects
MultiThread1 mt1 = new MultiThread1(new BigDecimal(186),
(double)15);
MultiThread1 mt2 = new MultiThread1(new BigDecimal(186),
(double)2);
//Wrapping the Runnable objects in Thread objects
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt2);
//Starting the execution of the threads
t1.start();
t2.start();
t1.join();
t2.join();
showPrices();
}
...
//Overriding the run() method
public void run() {
try {
//Instantiating an explicit execution context object
ExecutionContext execCtx = new ExecutionContext();
#sql [execCtx] { UPDATE samdbcat.sales.parts
SET price = price * (1 +
(:percent/100))
WHERE partnum = :partNum };
System.out.println("Updated the price of " + partNum +
" by " + percent + " percent");
}
catch (SQLException se) {
System.err.println("Error updating part number " + partNum
+ ": " + se);
}
}
...
For the entire program, see MultiThread1.sqlj—Threads With Explicit Execution
Contexts on page C-13.