1 | /* |
2 | * Copyright 2006-2010 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.springframework.batch.item.database; |
17 | |
18 | import java.util.Collection; |
19 | import java.util.List; |
20 | import java.util.Map; |
21 | |
22 | import org.hibernate.Query; |
23 | import org.hibernate.ScrollMode; |
24 | import org.hibernate.ScrollableResults; |
25 | import org.hibernate.Session; |
26 | import org.hibernate.SessionFactory; |
27 | import org.hibernate.StatelessSession; |
28 | import org.springframework.batch.item.database.orm.HibernateQueryProvider; |
29 | import org.springframework.beans.factory.InitializingBean; |
30 | import org.springframework.util.Assert; |
31 | import org.springframework.util.StringUtils; |
32 | |
33 | /** |
34 | * Internal shared state helper for hibernate readers managing sessions and |
35 | * queries. |
36 | * |
37 | * @author Dave Syer |
38 | * |
39 | */ |
40 | public class HibernateItemReaderHelper<T> implements InitializingBean { |
41 | |
42 | private SessionFactory sessionFactory; |
43 | |
44 | private String queryString = ""; |
45 | |
46 | private String queryName = ""; |
47 | |
48 | private HibernateQueryProvider queryProvider; |
49 | |
50 | private boolean useStatelessSession = true; |
51 | |
52 | private StatelessSession statelessSession; |
53 | |
54 | private Session statefulSession; |
55 | |
56 | /** |
57 | * @param queryName name of a hibernate named query |
58 | */ |
59 | public void setQueryName(String queryName) { |
60 | this.queryName = queryName; |
61 | } |
62 | |
63 | /** |
64 | * @param queryString HQL query string |
65 | */ |
66 | public void setQueryString(String queryString) { |
67 | this.queryString = queryString; |
68 | } |
69 | |
70 | /** |
71 | * @param queryProvider Hibernate query provider |
72 | */ |
73 | public void setQueryProvider(HibernateQueryProvider queryProvider) { |
74 | this.queryProvider = queryProvider; |
75 | } |
76 | |
77 | /** |
78 | * Can be set only in uninitialized state. |
79 | * |
80 | * @param useStatelessSession <code>true</code> to use |
81 | * {@link StatelessSession} <code>false</code> to use standard hibernate |
82 | * {@link Session} |
83 | */ |
84 | public void setUseStatelessSession(boolean useStatelessSession) { |
85 | Assert.state(statefulSession == null && statelessSession == null, |
86 | "The useStatelessSession flag can only be set before a session is initialized."); |
87 | this.useStatelessSession = useStatelessSession; |
88 | } |
89 | |
90 | /** |
91 | * @param sessionFactory hibernate session factory |
92 | */ |
93 | public void setSessionFactory(SessionFactory sessionFactory) { |
94 | this.sessionFactory = sessionFactory; |
95 | } |
96 | |
97 | public void afterPropertiesSet() throws Exception { |
98 | |
99 | Assert.state(sessionFactory != null, "A SessionFactory must be provided"); |
100 | |
101 | if (queryProvider == null) { |
102 | Assert.notNull(sessionFactory, "session factory must be set"); |
103 | Assert.state(StringUtils.hasText(queryString) ^ StringUtils.hasText(queryName), |
104 | "queryString or queryName must be set"); |
105 | } |
106 | // making sure that the appropriate (Hibernate) query provider is set |
107 | else { |
108 | Assert.state(queryProvider != null, "Hibernate query provider must be set"); |
109 | } |
110 | |
111 | } |
112 | |
113 | /** |
114 | * Get a cursor over all of the results, with the forward-only flag set. |
115 | * |
116 | * @param fetchSize the fetch size to use retrieving the results |
117 | * @param parameterValues the parameter values to use (or null if none). |
118 | * |
119 | * @return a forward-only {@link ScrollableResults} |
120 | */ |
121 | public ScrollableResults getForwardOnlyCursor(int fetchSize, Map<String, Object> parameterValues) { |
122 | Query query = createQuery(); |
123 | if (parameterValues != null) { |
124 | query.setProperties(parameterValues); |
125 | } |
126 | return query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY); |
127 | } |
128 | |
129 | /** |
130 | * Open appropriate type of hibernate session and create the query. |
131 | */ |
132 | public Query createQuery() { |
133 | |
134 | if (useStatelessSession) { |
135 | if (statelessSession == null) { |
136 | statelessSession = sessionFactory.openStatelessSession(); |
137 | } |
138 | if (queryProvider != null) { |
139 | queryProvider.setStatelessSession(statelessSession); |
140 | } |
141 | else { |
142 | if (StringUtils.hasText(queryName)) { |
143 | return statelessSession.getNamedQuery(queryName); |
144 | } |
145 | else { |
146 | return statelessSession.createQuery(queryString); |
147 | } |
148 | } |
149 | } |
150 | else { |
151 | if (statefulSession == null) { |
152 | statefulSession = sessionFactory.openSession(); |
153 | } |
154 | if (queryProvider != null) { |
155 | queryProvider.setSession(statefulSession); |
156 | } |
157 | else { |
158 | if (StringUtils.hasText(queryName)) { |
159 | return statefulSession.getNamedQuery(queryName); |
160 | } |
161 | else { |
162 | return statefulSession.createQuery(queryString); |
163 | } |
164 | } |
165 | } |
166 | |
167 | // If queryProvider is set use it to create a query |
168 | return queryProvider.createQuery(); |
169 | |
170 | } |
171 | |
172 | /** |
173 | * Scroll through the results up to the item specified. |
174 | * |
175 | * @param cursor the results to scroll over |
176 | */ |
177 | public void jumpToItem(ScrollableResults cursor, int itemIndex, int flushInterval) { |
178 | for (int i = 0; i < itemIndex; i++) { |
179 | cursor.next(); |
180 | if (i % flushInterval == 0 && !useStatelessSession) { |
181 | statefulSession.clear(); // Clears in-memory cache |
182 | } |
183 | } |
184 | } |
185 | |
186 | /** |
187 | * Close the open session (stateful or otherwise). |
188 | */ |
189 | public void close() { |
190 | if (statelessSession != null) { |
191 | statelessSession.close(); |
192 | statelessSession = null; |
193 | } |
194 | if (statefulSession != null) { |
195 | statefulSession.close(); |
196 | statefulSession = null; |
197 | } |
198 | } |
199 | |
200 | /** |
201 | * Read a page of data, clearing the existing session (if necessary) first, |
202 | * and creating a new session before executing the query. |
203 | * |
204 | * @param page the page to read (starting at 0) |
205 | * @param pageSize the size of the page or maximum number of items to read |
206 | * @param fetchSize the fetch size to use |
207 | * @param parameterValues the parameter values to use (if any, otherwise |
208 | * null) |
209 | * @return a collection of items |
210 | */ |
211 | public Collection<? extends T> readPage(int page, int pageSize, int fetchSize, Map<String, Object> parameterValues) { |
212 | |
213 | clear(); |
214 | |
215 | Query query = createQuery(); |
216 | if (parameterValues != null) { |
217 | query.setProperties(parameterValues); |
218 | } |
219 | @SuppressWarnings("unchecked") |
220 | List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize).list(); |
221 | return result; |
222 | |
223 | } |
224 | |
225 | /** |
226 | * Clear the session if stateful. |
227 | */ |
228 | public void clear() { |
229 | if (statefulSession != null) { |
230 | statefulSession.clear(); |
231 | } |
232 | } |
233 | |
234 | } |