1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.store; |
7 | |
8 | import org.h2.message.DbException; |
9 | |
10 | /** |
11 | * Represents an in-doubt transaction (a transaction in the prepare phase). |
12 | */ |
13 | public class PageStoreInDoubtTransaction implements InDoubtTransaction { |
14 | |
15 | private final PageStore store; |
16 | private final int sessionId; |
17 | private final int pos; |
18 | private final String transactionName; |
19 | private int state; |
20 | |
21 | /** |
22 | * Create a new in-doubt transaction info object. |
23 | * |
24 | * @param store the page store |
25 | * @param sessionId the session id |
26 | * @param pos the position |
27 | * @param transaction the transaction name |
28 | */ |
29 | public PageStoreInDoubtTransaction(PageStore store, int sessionId, int pos, |
30 | String transaction) { |
31 | this.store = store; |
32 | this.sessionId = sessionId; |
33 | this.pos = pos; |
34 | this.transactionName = transaction; |
35 | this.state = IN_DOUBT; |
36 | } |
37 | |
38 | @Override |
39 | public void setState(int state) { |
40 | switch(state) { |
41 | case COMMIT: |
42 | store.setInDoubtTransactionState(sessionId, pos, true); |
43 | break; |
44 | case ROLLBACK: |
45 | store.setInDoubtTransactionState(sessionId, pos, false); |
46 | break; |
47 | default: |
48 | DbException.throwInternalError("state="+state); |
49 | } |
50 | this.state = state; |
51 | } |
52 | |
53 | @Override |
54 | public String getState() { |
55 | switch(state) { |
56 | case IN_DOUBT: |
57 | return "IN_DOUBT"; |
58 | case COMMIT: |
59 | return "COMMIT"; |
60 | case ROLLBACK: |
61 | return "ROLLBACK"; |
62 | default: |
63 | throw DbException.throwInternalError("state="+state); |
64 | } |
65 | } |
66 | |
67 | @Override |
68 | public String getTransactionName() { |
69 | return transactionName; |
70 | } |
71 | |
72 | } |