1 22 package org.jboss.test.aop.bean; 23 24 import org.jboss.tm.TxUtils; 25 26 import javax.naming.InitialContext ; 27 import javax.transaction.Transaction ; 28 import javax.transaction.TransactionManager ; 29 30 35 public class TxPOJO 36 { 37 TransactionManager tm; 38 39 public TxPOJO() throws Exception 40 { 41 tm = (TransactionManager )new InitialContext ().lookup("java:/TransactionManager"); 42 } 43 44 public void never() {} 45 46 public void callNever() throws Exception 47 { 48 boolean exceptionThrown = false; 49 tm.begin(); 50 try 51 { 52 never(); 53 } 54 catch (Exception ex) 55 { 56 exceptionThrown = true; 57 } 58 tm.commit(); 59 if (!exceptionThrown) throw new Exception ("failed on mandatory no tx call"); 60 } 61 62 public void notsupported() throws Exception 63 { 64 if (tm.getTransaction() != null) throw new Exception ("notsupported() method has tx set"); 65 } 66 67 public void callNotSupported() throws Exception 68 { 69 tm.begin(); 70 notsupported(); 71 tm.commit(); 72 } 73 74 public void supports(Transaction tx) throws Exception 75 { 76 Transaction tmTx = tm.getTransaction(); 77 if (tx != tmTx) throw new Exception ("supports didn't work"); 78 } 79 80 public boolean hasActiveTransaction() throws Exception 81 { 82 Transaction tx = tm.getTransaction(); 83 if (tx == null) 84 { 85 System.out.println("Transaction: is null"); 86 } else 88 { 89 System.out.println("Transaction: status " + tx.getStatus() + " of tx" + tx); 90 } 92 return TxUtils.isActive(tx); 93 } 94 95 public void callSupportsWithTx() throws Exception 96 { 97 tm.begin(); 98 Transaction tx = tm.getTransaction(); 99 supports(tx); 100 tm.commit(); 101 } 102 103 public void callSupportsWithoutTx() throws Exception 104 { 105 supports(null); 106 } 107 108 public void required() throws Exception 109 { 110 if (tm.getTransaction() == null) throw new Exception ("rquired() method has no tx set"); 111 } 112 113 114 public void requiresNew(Transaction tx) throws Exception 115 { 116 Transaction tmTx = tm.getTransaction(); 117 if (tx == tmTx 118 || (tx != null && tx.equals(tmTx))) throw new Exception ("transactions shouldn't be equal"); 119 if (tmTx == null) throw new Exception ("tx is null in RequiresNew"); 120 } 121 public void callRequiresNew() throws Exception 122 { 123 tm.begin(); 124 Transaction tx = tm.getTransaction(); 125 requiresNew(tx); 126 tm.commit(); 127 } 128 129 public void mandatory() {} 130 131 public void callMandatoryNoTx() throws Exception 132 { 133 boolean exceptionThrown = false; 134 try 135 { 136 mandatory(); 137 } 138 catch (Exception ex) 139 { 140 exceptionThrown = true; 141 } 142 if (!exceptionThrown) throw new Exception ("failed on mandatory no tx call"); 143 } 144 145 public void callMandatoryWithTx() throws Exception 146 { 147 tm.begin(); 148 mandatory(); 149 tm.commit(); 150 } 151 152 153 } 154 155 | Popular Tags |