User: mulder  
  Date: 00/10/21 09:29:28

  Added:       src/main/org/jboss/test/xa/test Main.java
  Log:
  Added a test of 2-phase commit.
  This creates 3 connections across 2 data sources and does work on them
    and commits them together as a 2-phase commit transaction.
  You must create DB Pools "XADataSource1" and "XADataSource2" pointing to
    different DBs, and create a table in each - it'll spew out the table
    create statement if you run it and it can't find the tables.
  
  Revision  Changes    Path
  1.1                  jbosstest/src/main/org/jboss/test/xa/test/Main.java
  
  Index: Main.java
  ===================================================================
  package org.jboss.test.xa.test;
  
  import java.rmi.*;
  
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.ejb.DuplicateKeyException;
  import javax.ejb.Handle;
  import javax.ejb.EJBMetaData;
  import javax.ejb.FinderException;
  
  import java.util.Date;
  import java.util.Properties;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Enumeration;
  
  import org.jboss.test.xa.interfaces.CantSeeDataException;
  import org.jboss.test.xa.interfaces.XATest;
  import org.jboss.test.xa.interfaces.XATestHome;
  
  
  public class Main {
  
        static boolean deployed = false;
  
        public static void main(String arg[]) {
                Main main = new Main();
  
                try {
                        main.setUp();
  
                } catch (Exception e) {
                        System.out.println("Setup failed:");
                        e.printStackTrace();
                }
  
                try {
                        main.testXABean();
                        System.out.println();
                        
System.out.println("_____________________________________________");
                        System.out.println("Congratulations!  Test completed");
                        
System.out.println("_____________________________________________");
                        System.out.println();
  
                } catch (Exception e) {
              e.printStackTrace();
                        System.out.println();
                        
System.out.println("_____________________________________________");
                        System.out.println("Sorry, test failed.  Try again with 
different settings!");
                        System.out.println("Thanks for your time...");
                        System.out.println();
  
                }
        }
  
  
        public void testXABean() throws Exception       {
                int test = 0;
  
                Context ctx = new InitialContext();
  
          System.out.println();
          System.out.print(++test+"- "+"Looking up the XATest home...");
  
                XATestHome home;
  
                try {
                        home = (XATestHome) ctx.lookup("XATest");
  
                        if (home == null) throw new Exception("No Home!");
              System.out.println("OK");
                } catch (Exception e) {
                        System.out.println();
                        System.out.println("Could not lookup the context:  the beans 
are probably not deployed");
                        System.out.println("Check the server trace for details");
  
                        throw new Exception();
          }
  
          System.out.println();
          System.out.print(++test+"- "+"Creating an the XATest bean...");
          XATest bean;
          try {
              bean = home.create();
              if(bean == null) throw new Exception("No Bean!");
              System.out.println("OK");
                } catch (Exception e) {
              System.out.println();
              System.out.println("Could not create the bean!");
              System.out.println("Check the server trace for details");
              e.printStackTrace();
  
  
                        throw new Exception();
          }
  
          System.out.println();
          System.out.print(++test+"- "+"Clearing any old data...");
          try {
              bean.clearData();
              System.out.println("OK");
          } catch(Exception e) {
                        System.out.println();
                        System.out.println("Could not clear the data: did you create 
the table in both data sources?");
                        System.out.println("CREATE TABLE XA_TEST(ID INTEGER NOT NULL 
PRIMARY KEY, DATA INTEGER NOT NULL)");
  
                        throw new Exception();
          }
  
          System.out.println();
          System.out.print(++test+"- "+"Testing DB connections...");
          try {
              bean.doWork();
              System.out.println("OK");
          } catch(CantSeeDataException e) {
              System.out.println("sort of worked.");
              System.out.println(e.getMessage());
          } catch(Exception e) {
                        System.out.println();
                        System.out.println("Error during DB test!");
                        System.out.println("Check the server trace for details");
  
                        throw new Exception();
          }
        }
  
  
        protected void setUp() throws Exception
        {
                if (deployed) return;
  
                System.out.println("_____________________________________________");
                System.out.println();
                System.out.println("jBoss, the EJB Open Source Server");
                System.out.println("Copyright (C), The jBoss Organization, 2000");
                System.out.println("_____________________________________________");
                System.out.println();
                System.out.println("Welcome to the XADataSource test");
                System.out.println("_____________________________________________");
                System.out.println();
                System.out.print("Deploying the bean...");
                System.out.flush();
                new org.jboss.jmx.client.Deployer().deploy("../deploy/xatest.jar");
                deployed = true;
                System.out.println("done!");
        }
  }
  
  
  

Reply via email to