Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
33 | PointedEar | 1 | /* |
2 | * Hibernate, Relational Persistence for Idiomatic Java |
||
3 | * |
||
4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as |
||
5 | * indicated by the @author tags or express copyright attribution |
||
6 | * statements applied by the authors. All third-party contributions are |
||
7 | * distributed under license by Red Hat Middleware LLC. |
||
8 | * |
||
9 | * This copyrighted material is made available to anyone wishing to use, modify, |
||
10 | * copy, or redistribute it subject to the terms and conditions of the GNU |
||
11 | * Lesser General Public License, as published by the Free Software Foundation. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
||
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
||
16 | * for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Lesser General Public License |
||
19 | * along with this distribution; if not, write to: |
||
20 | * Free Software Foundation, Inc. |
||
21 | * 51 Franklin Street, Fifth Floor |
||
22 | * Boston, MA 02110-1301 USA |
||
23 | * |
||
24 | */ |
||
25 | package org.hibernate; |
||
26 | |||
27 | import java.io.Serializable; |
||
28 | import java.util.HashMap; |
||
29 | import java.util.Map; |
||
30 | |||
31 | /** |
||
32 | * Represents a flushing strategy. The flush process synchronizes |
||
33 | * database state with session state by detecting state changes |
||
34 | * and executing SQL statements. |
||
35 | * |
||
36 | * @see Session#setFlushMode(FlushMode) |
||
37 | * @see Query#setFlushMode(FlushMode) |
||
38 | * @see Criteria#setFlushMode(FlushMode) |
||
39 | * |
||
40 | * @author Gavin King |
||
41 | */ |
||
42 | public final class FlushMode implements Serializable { |
||
43 | private static final Map INSTANCES = new HashMap(); |
||
44 | |||
45 | private final int level; |
||
46 | private final String name; |
||
47 | |||
48 | private FlushMode(int level, String name) { |
||
49 | this.level = level; |
||
50 | this.name = name; |
||
51 | } |
||
52 | |||
53 | public String toString() { |
||
54 | return name; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * The {@link Session} is never flushed unless {@link Session#flush} |
||
59 | * is explicitly called by the application. This mode is very |
||
60 | * efficient for read only transactions. |
||
61 | * |
||
62 | * @deprecated use {@link #MANUAL} instead. |
||
63 | */ |
||
64 | public static final FlushMode NEVER = new FlushMode( 0, "NEVER" ); |
||
65 | |||
66 | /** |
||
67 | * The {@link Session} is only ever flushed when {@link Session#flush} |
||
68 | * is explicitly called by the application. This mode is very |
||
69 | * efficient for read only transactions. |
||
70 | */ |
||
71 | public static final FlushMode MANUAL = new FlushMode( 0, "MANUAL" ); |
||
72 | |||
73 | /** |
||
74 | * The {@link Session} is flushed when {@link Transaction#commit} |
||
75 | * is called. |
||
76 | */ |
||
77 | public static final FlushMode COMMIT = new FlushMode(5, "COMMIT"); |
||
78 | |||
79 | /** |
||
80 | * The {@link Session} is sometimes flushed before query execution |
||
81 | * in order to ensure that queries never return stale state. This |
||
82 | * is the default flush mode. |
||
83 | */ |
||
84 | public static final FlushMode AUTO = new FlushMode(10, "AUTO"); |
||
85 | |||
86 | /** |
||
87 | * The {@link Session} is flushed before every query. This is |
||
88 | * almost always unnecessary and inefficient. |
||
89 | */ |
||
90 | public static final FlushMode ALWAYS = new FlushMode(20, "ALWAYS"); |
||
91 | |||
92 | public boolean lessThan(FlushMode other) { |
||
93 | return this.level<other.level; |
||
94 | } |
||
95 | |||
96 | static { |
||
97 | INSTANCES.put( NEVER.name, NEVER ); |
||
98 | INSTANCES.put( MANUAL.name, MANUAL ); |
||
99 | INSTANCES.put( AUTO.name, AUTO ); |
||
100 | INSTANCES.put( ALWAYS.name, ALWAYS ); |
||
101 | INSTANCES.put( COMMIT.name, COMMIT ); |
||
102 | } |
||
103 | |||
104 | public static boolean isManualFlushMode(FlushMode mode) { |
||
105 | return MANUAL.level == mode.level; |
||
106 | } |
||
107 | |||
108 | private Object readResolve() { |
||
109 | return INSTANCES.get( name ); |
||
110 | } |
||
111 | |||
112 | public static FlushMode parse(String name) { |
||
113 | return ( FlushMode ) INSTANCES.get( name ); |
||
114 | } |
||
115 | } |