KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > AbstractContentiousRecyclingPoolingStressTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.test;
23
24 import javax.transaction.Status JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26
27 /**
28  * Abstract contentious pooling stress test.
29  *
30  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
31  * @version $Revision: 37406 $
32  */

33 public class AbstractContentiousRecyclingPoolingStressTest extends AbstractPoolingStressTest
34 {
35    TransactionState[] txStates;
36    
37    public void runConcurrentTest(ConcurrentRunnable[] runnables, ConcurrentTestCallback callback) throws Throwable JavaDoc
38    {
39       // HACK: The default config won't tweak this test
40
int transactions = getBeanCount();
41       int threads = getThreadCount();
42       if (transactions >= threads)
43          transactions = threads / 2;
44       if (transactions == 0)
45          transactions = 1;
46       
47       txStates = new TransactionState[transactions];
48       for (int i = 0; i < txStates.length; ++i)
49          txStates[i] = new TransactionState();
50       super.runConcurrentTest(runnables, callback);
51    }
52
53    public abstract class ContentiousRecyclingPoolingRunnable extends ConcurrentRunnable
54    {
55       TransactionState txState;
56       
57       public ContentiousRecyclingPoolingRunnable()
58       {
59       }
60
61       public void doStart() throws Throwable JavaDoc
62       {
63          txState = txStates[id % txStates.length];
64       }
65
66       public void doEnd() throws Throwable JavaDoc
67       {
68       }
69
70       public void doRun() throws Throwable JavaDoc
71       {
72          waitDone();
73          txState.begin();
74          try
75          {
76             doRun1();
77          }
78          catch (Throwable JavaDoc t)
79          {
80             setFailure(t);
81             tm.setRollbackOnly();
82             throw t;
83          }
84          finally
85          {
86             tm.suspend();
87             waitDone();
88             txState.commit();
89          }
90       }
91       
92       public abstract void doRun1() throws Throwable JavaDoc;
93    }
94
95    public class TransactionState
96    {
97       Transaction JavaDoc tx;
98
99       public TransactionState()
100       {
101       }
102       
103       public synchronized void begin() throws Throwable JavaDoc
104       {
105          if (tx == null)
106          {
107             tm.begin();
108             tx = tm.getTransaction();
109          }
110          else
111          {
112             tm.resume(tx);
113          }
114       }
115       
116       public synchronized void commit() throws Throwable JavaDoc
117       {
118          if (tx != null)
119          {
120             tm.resume(tx);
121             if (tm.getStatus() == Status.STATUS_MARKED_ROLLBACK)
122                tm.rollback();
123             else
124                tm.commit();
125             tx = null;
126          }
127       }
128    }
129    
130    public AbstractContentiousRecyclingPoolingStressTest(String JavaDoc name)
131    {
132       super(name);
133    }
134 }
135
Popular Tags