1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.domain.dao;
17
18
19 /***
20 * <code>InheritableThreadLocal</code> which indicates whether a {@link Dao}
21 * implementation should be forced to return a detached instance.
22 *
23 * <p>A detached instance is one which is no longer associated with the ORM
24 * mapper and changes will therefore not be transparently persisted to the database.
25 *
26 * <p>Not all <code>Dao</code> implementations support the concept of detached
27 * instances.
28 *
29 * @author Ben Alex
30 * @version $Id: DetachmentContextHolder.java,v 1.4 2005/11/17 00:55:47 benalex Exp $
31 *
32 * @see java.lang.InheritableThreadLocal
33 */
34 public class DetachmentContextHolder {
35
36
37 private static InheritableThreadLocal<Boolean> contextHolder = new InheritableThreadLocal<Boolean>();
38
39
40
41 /***
42 * Sets whether or not detached domain object instances should be returned
43 * within the current thread of execution.
44 *
45 * @param alwaysReturnDetached if true then detached instances should be returned.
46 */
47 public static void setForceReturnOfDetachedInstances(boolean alwaysReturnDetached) {
48 contextHolder.set(new Boolean(alwaysReturnDetached));
49 }
50
51 /***
52 * Returns the boolean value detachment policy which has been set for the current
53 * thread (defaults to false).
54 *
55 */
56 public static boolean isForceReturnOfDetachedInstances() {
57 if (contextHolder.get() == null) {
58 contextHolder.set(Boolean.FALSE);
59 }
60
61 return contextHolder.get().booleanValue();
62 }
63 }