View Javadoc

1   /*
2    * joey-test and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/11/27 10:41:55
6    */
7   package org.asyrinx.joey.test.dao;
8   
9   import javax.transaction.HeuristicMixedException;
10  import javax.transaction.HeuristicRollbackException;
11  import javax.transaction.InvalidTransactionException;
12  import javax.transaction.NotSupportedException;
13  import javax.transaction.RollbackException;
14  import javax.transaction.Status;
15  import javax.transaction.SystemException;
16  import javax.transaction.Transaction;
17  import javax.transaction.TransactionManager;
18  
19  /***
20   * トランザクションを
21   * 
22   * @author takeshi
23   */
24  public abstract class DBTestCaseTx extends DBTestCase {
25  
26      /***
27       * @param name
28       */
29      public DBTestCaseTx(String name) {
30          super(name);
31      }
32  
33      /*
34       * (non-Javadoc)
35       * 
36       * @see org.asyrinx.joey.test.dao.DBTestCase#afterSetupContainer()
37       */
38      protected void afterSetupContainer() throws Throwable {
39          super.afterSetupContainer();
40          final TransactionManager manager = (TransactionManager) getContainer().getComponent(TransactionManager.class);
41          if (manager == null)
42              throw new NullPointerException("TransactionManager was not found. check your j2ee.dicon or like that");
43          this.setTransactionManager(manager);
44      }
45  
46      /*
47       * (non-Javadoc)
48       * 
49       * @see org.asyrinx.joey.test.dao.DBTestCase#executeRunBare()
50       */
51      protected void executeRunBare() throws Throwable {
52          boolean began = false;
53          if (!hasTransaction()) {
54              begin();
55              began = true;
56          }
57          try {
58              super.executeRunBare();
59              if (began) {
60                  commit();
61              }
62          } catch (Throwable t) {
63              if (began) {
64                  rollback();
65              }
66              throw t;
67          }
68      }
69  
70      //
71  
72      private TransactionManager transactionManager;
73  
74      public final TransactionManager getTransactionManager() {
75          return transactionManager;
76      }
77  
78      public final void setTransactionManager(TransactionManager transactionManager) {
79          this.transactionManager = transactionManager;
80      }
81  
82      public final boolean hasTransaction() throws SystemException {
83          return transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION;
84      }
85  
86      public final Transaction getTransaction() throws SystemException {
87          return transactionManager.getTransaction();
88      }
89  
90      public final void begin() throws NotSupportedException, SystemException {
91          transactionManager.begin();
92      }
93  
94      public final void commit() throws SecurityException, IllegalStateException, RollbackException,
95              HeuristicMixedException, HeuristicRollbackException, SystemException {
96          transactionManager.commit();
97      }
98  
99      public final void rollback() throws IllegalStateException, SecurityException, SystemException {
100         if (hasTransaction()) {
101             transactionManager.rollback();
102         }
103     }
104 
105     public final Transaction suspend() throws SystemException {
106         return transactionManager.suspend();
107     }
108 
109     public final void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException,
110             SystemException {
111         transactionManager.resume(transaction);
112     }
113 
114 }