Once a transaction is started all methods called from the
run
method are in the transaction. For example:
Example 4.6. Transaction thread of control
// $Revision: 1.1.2.1 $ package com.kabira.snippets.transactions; import com.kabira.platform.Transaction; /** * Thread of control snippet * <p> * <h2> Target Nodes</h2> * <ul> * <li> <b>domainnode</b> = A * </ul> */ public class ThreadOfControl { /** * Main entry point * @param args Not used */ public static void main(String [] args) { new Transaction("Thread of Control") { @Override protected void run() throws Rollback { methodOne(); } }.execute(); } private static void methodOne() { // // This is executing in a transaction // methodTwo(); } private static void methodTwo() { // // This is also executing in a transaction // // ... } }
The thread of control of a transaction can span threads, JVMs, and nodes. Executing a method on a remote object extends the transaction to the remote node transparently. Application programs cannot assume that a transaction executes in a single thread.
If a new thread is created in a transaction, the new thread is not executing in a transaction when it starts. The creation of a thread is also not transactional. Specifically if a thread is started in a transaction and the transaction rolls back the thread is still running.
Example 4.7. Thread creation
// $Revision: 1.1.2.1 $ package com.kabira.snippets.transactions; import com.kabira.platform.Transaction; /** * Starting threads in a transaction * <p> * <h2> Target Nodes</h2> * <ul> * <li> <b>domainnode</b> = A * </ul> */ public class Threads { /** * An application thread */ public static class MyThread extends Thread { @Override public void run() { System.out.println("new thread not in a transaction"); } } /** * Main entry point * @param args Not used */ public static void main(String [] args) { new Transaction("Threads") { @Override protected void run() throws Rollback { // // Create a new daemon thread // MyThread myThread = new MyThread(); myThread.setDaemon(true); // // The thread is started even if the transaction rolls back. // The thread run method is not in a transaction // myThread.start(); } }.execute(); } }