KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > gtx > ServerGlobalTransactionManagerImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.gtx;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.object.gtx.GlobalTransactionID;
8 import com.tc.object.tx.ServerTransactionID;
9 import com.tc.objectserver.persistence.api.PersistenceTransaction;
10 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
11 import com.tc.objectserver.persistence.api.TransactionStore;
12 import com.tc.util.SequenceValidator;
13
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 public class ServerGlobalTransactionManagerImpl implements ServerGlobalTransactionManager {
20
21   private final TransactionStore transactionStore;
22   private final PersistenceTransactionProvider persistenceTransactionProvider;
23   private final SequenceValidator sequenceValidator;
24   private final Set JavaDoc resentServerTransactionIDs = new HashSet JavaDoc();
25
26   public ServerGlobalTransactionManagerImpl(SequenceValidator sequenceValidator, TransactionStore transactionStore,
27                                             PersistenceTransactionProvider ptxp) {
28     super();
29     this.sequenceValidator = sequenceValidator;
30     this.transactionStore = transactionStore;
31     this.persistenceTransactionProvider = ptxp;
32   }
33
34   public void shutdownClient(ChannelID channelID) {
35     this.sequenceValidator.remove(channelID);
36     PersistenceTransaction tx = this.persistenceTransactionProvider.newTransaction();
37     transactionStore.shutdownClient(tx, channelID);
38     tx.commit();
39   }
40
41   public boolean needsApply(ServerTransactionID stxID) {
42     GlobalTransactionDescriptor gtx = this.transactionStore.getTransactionDescriptor(stxID);
43     return (gtx == null);
44   }
45
46   private void assertNull(ServerTransactionID stxID, GlobalTransactionDescriptor gtx) {
47     if (gtx != null) throw new TransactionCommittedError("Transaction Already exists : " + stxID);
48   }
49
50   public void completeTransactions(PersistenceTransaction tx, Collection JavaDoc collection) {
51     if(collection.isEmpty()) return;
52     transactionStore.removeAllByServerTransactionID(tx, collection);
53   }
54
55   public void commit(PersistenceTransaction persistenceTransaction, ServerTransactionID stxID) {
56     GlobalTransactionDescriptor desc = transactionStore.getTransactionDescriptor(stxID);
57     assertNull(stxID, desc);
58     desc = transactionStore.createTransactionDescriptor(stxID);
59     transactionStore.commitTransactionDescriptor(persistenceTransaction, desc);
60   }
61
62   public void commitAll(PersistenceTransaction persistenceTransaction, Collection JavaDoc stxIDs) {
63     for (Iterator JavaDoc i = stxIDs.iterator(); i.hasNext();) {
64       ServerTransactionID stxID = (ServerTransactionID) i.next();
65       commit(persistenceTransaction, stxID);
66     }
67   }
68
69   public synchronized GlobalTransactionID getLowGlobalTransactionIDWatermark() {
70     if (resentServerTransactionIDs.isEmpty()) {
71       return transactionStore.getLeastGlobalTransactionID();
72     } else {
73       return GlobalTransactionID.NULL_ID;
74     }
75   }
76
77   public GlobalTransactionID createGlobalTransactionID(ServerTransactionID stxnID) {
78     return transactionStore.createGlobalTransactionID(stxnID);
79   }
80
81   public synchronized void addResentServerTransactionIDs(Collection JavaDoc stxIDs) {
82     resentServerTransactionIDs.addAll(stxIDs);
83   }
84
85   // TODO :: can be optimized to unregister once the set becomes size 0.
86
public synchronized void transactionCompleted(ServerTransactionID stxID) {
87     resentServerTransactionIDs.remove(stxID);
88   }
89
90   public void clearAllTransactionsFor(ChannelID client) {
91     for (Iterator JavaDoc iter = resentServerTransactionIDs.iterator(); iter.hasNext();) {
92       ServerTransactionID stxID = (ServerTransactionID) iter.next();
93       if (stxID.getChannelID().equals(client)) {
94         iter.remove();
95       }
96     }
97   }
98
99   public void transactionApplied(ServerTransactionID stxID) {
100     return;
101   }
102
103   public void incomingTransactions(ChannelID cid, Set JavaDoc serverTxnIDs) {
104     return;
105   }
106 }
107
Popular Tags