File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\InDoubtTransaction.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store;
8
9 import org.h2.message.DbException;
10
11 /**
12 * Represents an in-doubt transaction (a transaction in the prepare phase).
13 */
14 public class InDoubtTransaction {
15
16 /**
17 * The transaction state meaning this transaction is not committed yet, but
18 * also not rolled back (in-doubt).
19 */
20 public static final int IN_DOUBT = 0;
21
22 /**
23 * The transaction state meaning this transaction is committed.
24 */
25 public static final int COMMIT = 1;
26
27 /**
28 * The transaction state meaning this transaction is rolled back.
29 */
30 public static final int ROLLBACK = 2;
31
32 // TODO 2-phase-commit: document sql statements and metadata table
33
34 private final PageStore store;
35 private final int sessionId;
36 private final int pos;
37 private final String transaction;
38 private int state;
39
40 /**
41 * Create a new in-doubt transaction info object.
42 *
43 * @param store the page store
44 * @param sessionId the session id
45 * @param pos the position
46 * @param transaction the transaction name
47 */
48 public InDoubtTransaction(PageStore store, int sessionId, int pos, String transaction) {
49 this.store = store;
50 this.sessionId = sessionId;
51 this.pos = pos;
52 this.transaction = transaction;
53 this.state = IN_DOUBT;
54 }
55
56 /**
57 * Change the state of this transaction.
58 * This will also update the transaction log.
59 *
60 * @param state the new state
61 */
62 public void setState(int state) {
63 switch(state) {
64 case COMMIT:
65 store.setInDoubtTransactionState(sessionId, pos, true);
66 break;
67 case ROLLBACK:
68 store.setInDoubtTransactionState(sessionId, pos, false);
69 break;
70 default:
71 DbException.throwInternalError("state="+state);
72 }
73 this.state = state;
74 }
75
76 /**
77 * Get the state of this transaction as a text.
78 *
79 * @return the transaction state text
80 */
81 public String getState() {
82 switch(state) {
83 case IN_DOUBT:
84 return "IN_DOUBT";
85 case COMMIT:
86 return "COMMIT";
87 case ROLLBACK:
88 return "ROLLBACK";
89 default:
90 throw DbException.throwInternalError("state="+state);
91 }
92 }
93
94 /**
95 * Get the name of the transaction.
96 *
97 * @return the transaction name
98 */
99 public String getTransaction() {
100 return transaction;
101 }
102
103 }
104