KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > TransactionPersistorImpl


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.persistence.sleepycat;
5
6 import com.sleepycat.bind.EntryBinding;
7 import com.sleepycat.je.Cursor;
8 import com.sleepycat.je.CursorConfig;
9 import com.sleepycat.je.Database;
10 import com.sleepycat.je.DatabaseEntry;
11 import com.sleepycat.je.DatabaseException;
12 import com.sleepycat.je.LockMode;
13 import com.sleepycat.je.OperationStatus;
14 import com.tc.object.tx.ServerTransactionID;
15 import com.tc.objectserver.gtx.GlobalTransactionDescriptor;
16 import com.tc.objectserver.persistence.api.PersistenceTransaction;
17 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
18 import com.tc.objectserver.persistence.api.TransactionPersistor;
19 import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor.SleepycatPersistorBase;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 class TransactionPersistorImpl extends SleepycatPersistorBase implements TransactionPersistor {
26
27   private final Database db;
28   private final CursorConfig cursorConfig;
29   private final EntryBinding gtxBinding;
30   private final EntryBinding stxIDBinding;
31   private final PersistenceTransactionProvider ptp;
32
33   public TransactionPersistorImpl(Database db, EntryBinding stxIDBinding, EntryBinding gtxBinding,
34                                   PersistenceTransactionProvider ptp) {
35     this.db = db;
36     this.stxIDBinding = stxIDBinding;
37     this.gtxBinding = gtxBinding;
38     this.ptp = ptp;
39     this.cursorConfig = new CursorConfig();
40     this.cursorConfig.setReadCommitted(true);
41   }
42
43   public Collection JavaDoc loadAllGlobalTransactionDescriptors() {
44     Cursor cursor = null;
45     PersistenceTransaction tx = null;
46     try {
47       Collection JavaDoc rv = new HashSet JavaDoc();
48       tx = ptp.newTransaction();
49       cursor = this.db.openCursor(pt2nt(tx), cursorConfig);
50       DatabaseEntry key = new DatabaseEntry();
51       DatabaseEntry value = new DatabaseEntry();
52       while (OperationStatus.SUCCESS.equals(cursor.getNext(key, value, LockMode.DEFAULT))) {
53         rv.add(gtxBinding.entryToObject(value));
54       }
55       cursor.close();
56       tx.commit();
57       return rv;
58     } catch (DatabaseException e) {
59       abortOnError(cursor, tx);
60       throw new DBException(e);
61     }
62   }
63
64   public void saveGlobalTransactionDescriptor(PersistenceTransaction tx, GlobalTransactionDescriptor gtx) {
65     DatabaseEntry key = new DatabaseEntry();
66     DatabaseEntry value = new DatabaseEntry();
67     stxIDBinding.objectToEntry(gtx.getServerTransactionID(), key);
68     gtxBinding.objectToEntry(gtx, value);
69     try {
70       this.db.put(pt2nt(tx), key, value);
71     } catch (DatabaseException e) {
72       throw new DBException(e);
73     }
74   }
75
76   public void deleteAllByServerTransactionID(PersistenceTransaction tx, Collection JavaDoc toDelete) {
77     DatabaseEntry key = new DatabaseEntry();
78     for (Iterator JavaDoc i = toDelete.iterator(); i.hasNext();) {
79       ServerTransactionID stxID = (ServerTransactionID) i.next();
80       stxIDBinding.objectToEntry(stxID, key);
81       try {
82         db.delete(pt2nt(tx), key);
83       } catch (DatabaseException e) {
84         throw new DBException(e);
85       }
86     }
87   }
88
89 }
Popular Tags