1 43 44 45 import java.lang.reflect.Field ; 46 47 import javax.naming.Context ; 48 import javax.naming.InitialContext ; 49 import javax.transaction.RollbackException ; 50 import javax.transaction.Status ; 51 import javax.transaction.UserTransaction ; 52 53 59 public class BasicExample { 60 61 public static void main(String [] args) { 62 if (args.length!=1) { 63 System.out.println("usage : BasicExample [userTransactionURL]"); 64 System.exit(1); 65 } 66 67 68 UserTransaction utc = null; 69 70 try { 71 System.out.println("create initial context"); 72 Context ictx = new InitialContext (); 73 System.out.println("lookup UserTransaction at : "+args[0]); 74 utc = (UserTransaction )ictx.lookup(args[0]); 75 } catch (Exception e) { 76 System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown"); 77 System.out.println("Exception message :"+e.getMessage()); 78 e.printStackTrace(); 79 System.exit(1); 80 } 81 82 System.out.println(); 83 84 try { 86 System.out.println("a simple transaction which is committed:"); 87 System.out.println("\t- initial status : "+getStatusName(utc.getStatus())); 88 utc.begin(); 89 System.out.println("\t- after begin status : "+getStatusName(utc.getStatus())); 90 utc.commit(); 91 System.out.println("\t- after commit status : "+getStatusName(utc.getStatus())); 92 } catch (Exception e) { 93 System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown"); 94 System.out.println("Exception message :"+e.getMessage()); 95 e.printStackTrace(); 96 System.exit(1); 97 } 98 99 System.out.println(); 100 try { 102 System.out.println("a simple transaction which is rolled back."); 103 System.out.println("we set a transaction timeout to 1 second, begin the transaction, "+ 104 "and wait 5 seconds before committing it:"); 105 utc.setTransactionTimeout(1); 106 System.out.println("\t- initial status : "+getStatusName(utc.getStatus())); 107 utc.begin(); 108 System.out.println("\t- after begin status : "+getStatusName(utc.getStatus())); 109 System.out.println("\t- wait for 5 seconds"); 110 Thread.sleep(5*1000); 111 utc.commit(); 112 System.out.println("ERROR: the commit method should have failed due to timeout expiration"); 113 System.exit(1); 114 } catch (RollbackException e) { 115 try { 116 System.out.println("\t- after rollback status : "+ getStatusName(utc.getStatus())); 117 } catch (Exception ex) { 118 System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown"); 119 System.out.println("Exception message :"+e.getMessage()); 120 e.printStackTrace(); 121 System.exit(1); 122 } 123 } catch (Exception e) { 124 System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown"); 125 System.out.println("Exception message :"+e.getMessage()); 126 e.printStackTrace(); 127 System.exit(1); 128 } 129 130 System.out.println("\nBasic example is OK."); 131 } 132 133 public static String getStatusName(int status) { 134 String statusName = null; 135 try { 136 Field [] flds = Status .class.getDeclaredFields(); 137 for (int i=0; i<flds.length; i++) { 138 if (flds[i].getInt(null) == status) 139 statusName = flds[i].getName(); 140 } 141 } catch (Exception e) { 142 statusName = "invalid status value!"; 143 } 144 return statusName; 145 } 146 } 147 | Popular Tags |