Skip to main content
summaryrefslogtreecommitdiffstats
blob: 174f55f5969ccdcd939c861d6ac82d304ef277ed (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Martin Taal 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:
 *    Martin Taal - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.cdo.server.internal.hibernate;

import org.eclipse.emf.cdo.server.IStoreWriter.CommitContext;
import org.eclipse.emf.cdo.server.internal.hibernate.bundle.OM;

import org.eclipse.net4j.internal.util.om.trace.ContextTracer;

import org.hibernate.Session;

/**
 * @author Martin Taal
 */
public class HibernateThreadContext
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HibernateThreadContext.class);

  private static ThreadLocal<CommitContext> commitContext = new ThreadLocal<CommitContext>();

  private static ThreadLocal<Session> session = new ThreadLocal<Session>();

  public static Session getSession()
  {
    Session result = session.get();
    if (result == null)
    {
      throw new IllegalStateException("Session not set");
    }

    return result;
  }

  public static void setSession(Session newSession)
  {
    if (newSession != null && session.get() != null)
    {
      throw new IllegalStateException("Session already set");
    }

    if (TRACER.isEnabled())
    {
      if (newSession == null)
      {
        TRACER.trace("Clearing session in threadlocal");
      }
      else
      {
        TRACER.trace("Setting session in threadlocal");
      }
    }

    session.set(newSession);
  }

  public static CommitContext getCommitContext()
  {
    CommitContext result = commitContext.get();
    if (result == null)
    {
      throw new IllegalStateException("CommitContext not set");
    }

    return result;
  }

  public static boolean isCommitContextSet()
  {
    return commitContext.get() != null;
  }

  public static void setCommitContext(CommitContext newCommitContext)
  {
    if (newCommitContext != null && commitContext.get() != null)
    {
      throw new IllegalStateException("CommitContext already set");
    }

    if (TRACER.isEnabled())
    {
      if (newCommitContext == null)
      {
        TRACER.trace("Clearing commitcontext in threadlocal");
      }
      else
      {
        TRACER.trace("Setting commitcontext in threadlocal");
      }
    }

    commitContext.set(newCommitContext);
  }
}

Back to the top