KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > BaseTransactionalContextInterceptor


1 package org.jboss.cache.interceptors;
2
3 import org.jboss.cache.CacheSPI;
4 import org.jboss.cache.GlobalTransaction;
5 import org.jboss.cache.InvocationContext;
6 import org.jboss.cache.TransactionEntry;
7 import org.jboss.cache.TransactionTable;
8 import org.jboss.cache.config.Option;
9
10 import javax.transaction.Status JavaDoc;
11 import javax.transaction.SystemException JavaDoc;
12 import javax.transaction.Transaction JavaDoc;
13 import javax.transaction.TransactionManager JavaDoc;
14
15 /**
16  * Class providing some base functionality around manipulating transactions and global transactions withing invocation contexts.
17  *
18  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
19  */

20 public abstract class BaseTransactionalContextInterceptor extends Interceptor
21 {
22    protected TransactionTable txTable;
23    protected TransactionManager JavaDoc txManager;
24
25    public void setCache(CacheSPI cache)
26    {
27       super.setCache(cache);
28       txManager = cache.getTransactionManager();
29       txTable = cache.getTransactionTable();
30    }
31
32    protected void copyInvocationScopeOptionsToTxScope(InvocationContext ctx)
33    {
34       // notify the transaction entry that this override is in place.
35
TransactionEntry entry = txTable.get(ctx.getGlobalTransaction());
36       if (entry != null)
37       {
38          Option txScopeOption = new Option();
39          txScopeOption.setCacheModeLocal(ctx.getOptionOverrides() != null && ctx.getOptionOverrides().isCacheModeLocal());
40          entry.setOption(txScopeOption);
41       }
42    }
43
44    protected void setTransactionalContext(Transaction tx, GlobalTransaction gtx)
45    {
46       InvocationContext ctx = cache.getInvocationContext();
47       log.trace("Setting up transactional context.");
48       if (log.isTraceEnabled()) log.trace("Setting tx as " + tx + " and gtx as " + gtx);
49       ctx.setTransaction(tx);
50       ctx.setGlobalTransaction(gtx);
51    }
52
53    /**
54     * Returns true if transaction is rolling back, false otherwise
55     */

56    protected boolean isRollingBack(Transaction tx)
57    {
58       if (tx == null) return false;
59       int status = -1;
60       try
61       {
62          status = tx.getStatus();
63          return status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK;
64       }
65       catch (SystemException JavaDoc e)
66       {
67          log.error("failed getting transaction status", e);
68          return false;
69       }
70    }
71 }
72
Popular Tags