1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction; 26 27 import java.sql.SQLException ; 28 29 import javax.annotation.Resource; 30 import javax.ejb.Remote ; 31 import javax.ejb.SessionContext ; 32 import javax.ejb.Stateful ; 33 import javax.ejb.TransactionManagement ; 34 import javax.ejb.TransactionManagementType ; 35 import javax.naming.NamingException ; 36 import javax.transaction.HeuristicMixedException ; 37 import javax.transaction.HeuristicRollbackException ; 38 import javax.transaction.NotSupportedException ; 39 import javax.transaction.RollbackException ; 40 import javax.transaction.Status ; 41 import javax.transaction.SystemException ; 42 import javax.transaction.UserTransaction ; 43 44 import org.objectweb.easybeans.tests.common.db.TableManager; 45 import org.objectweb.easybeans.tests.common.exception.TransactionException; 46 import org.objectweb.easybeans.tests.common.helper.TransactionHelper; 47 48 54 @Stateful (name="SFSBBeanManagedTransaction") 55 @Remote (ItfBeanManagedTransaction.class) 56 @TransactionManagement (value = TransactionManagementType.BEAN) 57 public class SFSBBeanManagedTransaction implements ItfBeanManagedTransaction { 58 59 62 @Resource 63 private SessionContext sessionContext; 64 65 68 private UserTransaction utx = null; 69 70 73 private TableManager tableManager = null; 74 75 79 private boolean bolOnlyCreateTrans = false; 80 81 96 public void dropTableWithBeginCommitTransaction() throws SQLException , NamingException , SystemException , 97 NotSupportedException , HeuristicRollbackException , RollbackException , HeuristicMixedException , 98 TransactionException { 99 utx.begin(); 100 try { 101 if (!bolOnlyCreateTrans) { 102 tableManager.deleteTable(TABLE); 103 } 104 utx.commit(); 105 } catch (Exception e) { 106 utx.rollback(); 107 throw new TransactionException("Error during commit.", e); 108 } 109 110 } 111 112 127 public void dropTableWithoutBeginTransaction() throws SQLException , NamingException , SystemException , 128 NotSupportedException , HeuristicRollbackException , RollbackException , HeuristicMixedException , 129 TransactionException { 130 try { 131 if (!bolOnlyCreateTrans) { 132 tableManager.deleteTable(TABLE); 133 } 134 utx.commit(); 135 } catch (Exception e) { 136 utx.rollback(); 137 throw new TransactionException("Error during commit.", e); 138 } 139 } 140 141 156 public void insertTableWithBeginCommitTransaction() throws SQLException , NamingException , SystemException , 157 NotSupportedException , HeuristicRollbackException , RollbackException , HeuristicMixedException , 158 TransactionException { 159 160 utx.begin(); 161 try { 162 if (!bolOnlyCreateTrans) { 163 tableManager.insertTable(TABLE); 164 } 165 utx.commit(); 166 } catch (Exception e) { 167 utx.rollback(); 168 throw new TransactionException("Error during commit.", e); 169 } 170 } 171 172 180 public void insertTableWithoutCommitTransaction() throws SQLException , NamingException , SystemException , 181 NotSupportedException { 182 utx.begin(); 183 if (!bolOnlyCreateTrans) { 184 tableManager.insertTable(TABLE); 185 } 186 } 187 188 203 public void insertTableWithNestedTrans() throws SQLException , NamingException , SystemException , 204 NotSupportedException , HeuristicRollbackException , RollbackException , HeuristicMixedException , 205 TransactionException { 206 utx.begin(); 208 UserTransaction utxNested = TransactionHelper.getUserTransaction(); 210 211 utxNested.begin(); 213 if (!bolOnlyCreateTrans) { 215 tableManager.insertTable(TABLE); 216 } 217 utxNested.commit(); 219 utx.commit(); 221 222 } 223 224 240 public void insertTableWithNewTransaction() throws SQLException , NamingException , SystemException , 241 NotSupportedException , HeuristicRollbackException , RollbackException , HeuristicMixedException , 242 TransactionException { 243 UserTransaction utxNested = TransactionHelper.getUserTransaction(); 245 246 utxNested.begin(); 248 try { 249 if (!bolOnlyCreateTrans) { 251 tableManager.insertTable(TABLE); 252 } 253 utxNested.commit(); 256 } catch (Exception e) { 257 utxNested.rollback(); 258 if (this.getTransactionStatus() != Status.STATUS_NO_TRANSACTION) { 259 utx.rollback(); 260 } 261 throw new TransactionException("Error during commit.", e); 262 } 263 } 264 265 273 public void insertTableWithBeginRollback() throws SQLException , NamingException , SystemException , 274 NotSupportedException { 275 utx.begin(); 276 if (!bolOnlyCreateTrans) { 277 tableManager.insertTable(TABLE); 278 } 279 utx.rollback(); 280 } 281 282 288 public void setRollback() throws IllegalStateException , SecurityException , SystemException { 289 utx.rollback(); 290 } 291 292 299 public void startup(final boolean callOnlyTransaction, final String dbName) throws NamingException , SQLException { 300 utx = TransactionHelper.getUserTransaction(); 301 bolOnlyCreateTrans = callOnlyTransaction; 302 tableManager = new TableManager(dbName); 303 304 } 305 306 311 public int getTransactionStatus() throws SystemException { 312 return utx.getStatus(); 313 } 314 315 320 public void setRollbackOnly() throws NamingException , IllegalStateException { 321 sessionContext.setRollbackOnly(); 322 } 323 324 329 public void getRollbackOnly() throws NamingException , IllegalStateException { 330 sessionContext.getRollbackOnly(); 331 } 332 } 333 | Popular Tags |