1 25 26 package eb; 27 28 import java.util.Enumeration; 29 30 import javax.naming.Context; 31 import javax.naming.InitialContext; 32 import javax.rmi.PortableRemoteObject; 33 import javax.transaction.UserTransaction; 34 35 40 public class ClientAccount { 41 42 45 private static UserTransaction utx = null; 46 47 private static void accountList(AccountHome h) { 48 Enumeration alist; 49 Account acc; 50 try { 51 utx.begin(); alist = h.findAllAccounts(); 53 while (alist.hasMoreElements()) { 54 acc = (Account) PortableRemoteObject.narrow(alist.nextElement(), Account.class); 55 System.out.println(acc.getNumber() + " " + acc.getCustomer() + " " + acc.getBalance()); 56 } 57 utx.commit(); 58 } catch (Exception e) { 59 System.err.println("Exception getting account list: " + e); 60 System.exit(2); 61 } 62 } 63 64 public static void main(String[] args) { 65 66 String beanName = args[0]; 68 69 Context initialContext = null; 71 try { 72 initialContext = new InitialContext(); 73 } catch (Exception e) { 74 System.err.println("Cannot get initial context for JNDI: " + e); 75 System.exit(2); 76 } 77 78 System.out.println("Getting a UserTransaction object from JNDI"); 80 try { 81 82 utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction"); 84 85 } catch (Exception e) { 86 System.err.println("Cannot lookup UserTransaction: " + e); 87 System.exit(2); 88 } 89 90 System.out.println("Connecting to the AccountHome"); 92 AccountHome home = null; 93 try { 94 home = (AccountHome) PortableRemoteObject.narrow(initialContext.lookup(beanName), AccountHome.class); 95 } catch (Exception e) { 96 System.err.println("Cannot lookup " + beanName + ": " + e); 97 System.exit(2); 98 } 99 100 System.out.println("Getting the list of existing accounts in database"); 102 accountList(home); 103 104 System.out.println("Creating a new Account in database"); 106 Account a1 = null; 107 try { 108 a1 = home.create(109, "John Smith", 0); 109 } catch (Exception e) { 110 System.err.println("Cannot create Account: " + e); 111 System.exit(2); 112 } 113 114 System.out.println("Finding an Account by its number in database"); 116 Account a2 = null; 117 try { 118 a2 = home.findByNumber(102); 119 } catch (Exception e) { 120 System.err.println("Cannot find Account: " + e); 121 System.exit(2); 122 } 123 124 System.out.println("Starting a first transaction, that will be committed"); 127 try { 128 double value = 100.00; 129 utx.begin(); 130 a1.setBalance(value); 131 a2.setBalance(-value); 132 utx.commit(); 133 } catch (Exception e) { 134 System.err.println("exception during 1st Tx: " + e); 135 System.exit(2); 136 } 137 138 System.out.println("Starting a second transaction, that will be rolled back"); 141 try { 142 double value = 20.00; 143 utx.begin(); 144 a1.setBalance(-value); 145 a2.setBalance(value); 146 utx.rollback(); 147 } catch (Exception e) { 148 System.err.println("exception during 2nd Tx: " + e); 149 System.exit(2); 150 } 151 152 System.out.println("Getting the new list of accounts in database"); 154 accountList(home); 155 156 System.out.println("Removing Account previously created in database"); 158 try { 159 a1.remove(); 160 } catch (Exception e) { 161 System.err.println("exception during remove: " + e); 162 System.exit(2); 163 } 164 165 System.out.println("ClientAccount terminated"); 167 } 168 169 } 170 171 | Popular Tags |