Skip to main content
summaryrefslogtreecommitdiffstats
blob: a339960c24a41e7d1c2261dfd13f15152b364b18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.internal.server;

import org.eclipse.emf.cdo.internal.server.bundle.OM;
import org.eclipse.emf.cdo.server.StoreThreadLocal;
import org.eclipse.emf.cdo.spi.server.InternalRepository;
import org.eclipse.emf.cdo.spi.server.InternalTransaction;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.concurrent.ConcurrentValue;
import org.eclipse.net4j.util.om.monitor.OMMonitor;
import org.eclipse.net4j.util.om.trace.ContextTracer;

/**
 * @author Simon McDuff
 * @since 2.0
 */
public class XATransactionCommitContext extends TransactionCommitContext
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_TRANSACTION, XATransactionCommitContext.class);

  private ConcurrentValue<CommitState> state = new ConcurrentValue<CommitState>(CommitState.STARTING);

  public XATransactionCommitContext(InternalTransaction transaction)
  {
    super(transaction);
  }

  public ConcurrentValue<CommitState> getState()
  {
    return state;
  }

  @Override
  public void preWrite()
  {
    super.preWrite();
    StoreThreadLocal.setAccessor(null);
  }

  @Override
  public void commit(OMMonitor monitor)
  {
    StoreThreadLocal.setAccessor(getAccessor());
    try
    {
      super.commit(monitor);
    }
    finally
    {
      StoreThreadLocal.setAccessor(null);
    }
  }

  @Override
  public void write(OMMonitor monitor)
  {
    StoreThreadLocal.setAccessor(getAccessor());
    try
    {
      super.write(monitor);
    }
    finally
    {
      StoreThreadLocal.setAccessor(null);
    }
  }

  @Override
  public void postCommit(boolean success)
  {
    StoreThreadLocal.setAccessor(getAccessor());
    InternalRepository repository = getTransaction().getRepository();
    repository.getCommitManager().remove(this);
    super.postCommit(success);
  }

  @Override
  public synchronized void rollback(String message)
  {
    super.rollback(message);

    // Change the state to unblock call.
    state.set(CommitState.ROLLED_BACK);
  }

  /**
   * Wait until another thread fills ID mapping for external objects.
   */
  @Override
  public void applyIDMappings(OMMonitor monitor)
  {
    if (TRACER.isEnabled())
    {
      TRACER.format("Notify phase2 to fill ID mapping."); //$NON-NLS-1$
    }

    state.set(CommitState.APPLY_ID_MAPPING);
    if (TRACER.isEnabled())
    {
      TRACER.format("Waiting for phase2 to be completed before continueing."); //$NON-NLS-1$
    }

    try
    {
      state.acquire(PHASEAPPLYMAPPING_DONE);
    }
    catch (InterruptedException ex)
    {
      throw WrappedException.wrap(ex);
    }

    if (TRACER.isEnabled())
    {
      TRACER.format("Received signal to continue."); //$NON-NLS-1$
    }

    super.applyIDMappings(monitor);
  }

  /**
   * Object to test if the process is at ApplyIDMapping
   */
  final public static Object PHASEAPPLYMAPPING = new Object()
  {
    @Override
    public int hashCode()
    {
      return CommitState.APPLY_ID_MAPPING.hashCode();
    }

    @Override
    public boolean equals(Object object)
    {
      if (object == CommitState.ROLLED_BACK)
      {
        throw new RuntimeException("RolledBack"); //$NON-NLS-1$
      }

      return CommitState.APPLY_ID_MAPPING == object;
    }
  };

  /**
   * Object to test if the process did applyIDMapping
   */
  final public static Object PHASEAPPLYMAPPING_DONE = new Object()
  {
    @Override
    public int hashCode()
    {
      return CommitState.APPLY_ID_MAPPING_DONE.hashCode();
    }

    @Override
    public boolean equals(Object object)
    {
      if (object == CommitState.ROLLED_BACK)
      {
        throw new RuntimeException("RolledBack"); //$NON-NLS-1$
      }

      return CommitState.APPLY_ID_MAPPING_DONE == object;
    }
  };

  /**
   * @author Simon McDuff
   * @since 2.0
   */
  public enum CommitState
  {
    STARTING, APPLY_ID_MAPPING, APPLY_ID_MAPPING_DONE, ROLLED_BACK
  }
}

Back to the top