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