Source code: org/jbpm/util/db/DbSession.java
1 package org.jbpm.util.db;
2
3 import java.io.*;
4 import java.util.*;
5 import org.apache.log4j.*;
6 import net.sf.hibernate.*;
7 import net.sf.hibernate.type.*;
8 import org.jbpm.util.*;
9
10 /**
11 * This class wraps the Hibernate Session.
12 * It adds the guarantee that if a method throws an exception, the session will already be closed.
13 * Furthermore, this wrapper adds 2 convenience-methods findOne(...) and iterateOne(...) that checks if exactly one object is returned and extracts that single object from the collection.
14 */
15 public class DbSession {
16
17 /* package private */ DbSession( Session session ) {
18 this.session = session;
19 }
20
21 public Collection filter(Object collection, String filter) {
22 Collection c = null;
23 try {
24 c = session.filter( collection, filter );
25 } catch ( Throwable t ) {
26 handleDatabaseException( t, "filter(collection," + filter + ")");
27 }
28 return c;
29 }
30
31 public Collection filter(Object collection, String filter, Object[] values, Type[] types) {
32 Collection c = null;
33 try {
34 c = session.filter( collection, filter, values, types );
35 } catch ( Throwable t ) {
36 handleDatabaseException( t, "filter(collection," + filter + "," + ArrayUtil.toString(values) + ",types)");
37 }
38 return c;
39 }
40
41 public Collection filter(Object collection, String filter, Object value, Type type){
42 Collection c = null;
43 try {
44 c = session.filter( collection, filter, value, type );
45 } catch ( Throwable t ) {
46 handleDatabaseException( t, "filter(collection," + filter + ",value,type)");
47 }
48 return c;
49 }
50
51 public Query getNamedQuery(String queryName) {
52 Query q = null;
53 try {
54 q = session.getNamedQuery( queryName );
55 } catch ( Throwable t ) {
56 handleDatabaseException( t, "getNamedQuery(" + queryName + ")");
57 }
58 return q;
59 }
60
61 public Query createFilter(Object collection, String queryString) {
62 Query q = null;
63 try {
64 q = session.createFilter(collection,queryString);
65 } catch ( Throwable t ) {
66 handleDatabaseException( t, "createFilter(collection," + queryString + ")");
67 }
68 return q;
69 }
70
71 public Query createQuery(String queryString) {
72 Query q = null;
73 try {
74 q = session.createQuery(queryString);
75 } catch ( Throwable t ) {
76 handleDatabaseException( t, "createQuery(" + queryString + ")");
77 }
78 return q;
79 }
80
81 public Object load(Class theClass, Serializable id) {
82 Object o = null;
83 try {
84 o = session.load(theClass,id);
85 } catch ( net.sf.hibernate.ObjectNotFoundException e ) {
86 throw new ObjectNotFoundException( theClass, "id=" + id.toString() );
87 } catch ( Throwable t ) {
88 handleDatabaseException( t, "load(" + theClass.getName() + "," + id.toString() + ")");
89 }
90 return o;
91 }
92
93 public Object load(Class theClass, Serializable id, LockMode lockMode) {
94 Object o = null;
95 try {
96 o = session.load(theClass,id,lockMode);
97 } catch ( net.sf.hibernate.ObjectNotFoundException e ) {
98 throw new ObjectNotFoundException( theClass, "id=" + id.toString() );
99 } catch ( Throwable t ) {
100 handleDatabaseException( t, "load(" + theClass.getName() + "," + id.toString() + "," + lockMode + ")");
101 }
102 return o;
103 }
104
105 public void load(Object object, Serializable id) {
106 try {
107 session.load(object,id);
108 } catch ( net.sf.hibernate.ObjectNotFoundException e ) {
109 throw new ObjectNotFoundException( object.getClass(), "id=" + id.toString() );
110 } catch ( Throwable t ) {
111 handleDatabaseException( t, "load(" + object + "," + id + ")");
112 }
113 }
114
115 public List find(String query) {
116 List l = null;
117 try {
118 // log.debug( "find-query(" + query + ")" );
119 l = session.find(query);
120 } catch ( Throwable t ) {
121 handleDatabaseException( t, "find(" + query + ")");
122 }
123 return l;
124 }
125
126 public List find(String query, Object[] values, Type[] types) {
127 List l = null;
128 try {
129 // log.debug( "find-query(" + query + "), parameters(" + ArrayUtil.toString( values ) + ")" );
130 l = session.find(query,values,types);
131 } catch ( Throwable t ) {
132 handleDatabaseException( t, "find(" + query + "," + ArrayUtil.toString(values) + ",types)");
133 }
134 return l;
135 }
136
137 public List find(String query, Object value, Type type) {
138 List l = null;
139 try {
140 // log.debug( "find-query(" + query + "), parameter(" + value + ")" );
141 l = session.find(query,value,type);
142 } catch ( Throwable t ) {
143 handleDatabaseException( t, "find(" + query + ",value,type)");
144 }
145 return l;
146 }
147
148 public Object findOne(String query) {
149 List l = find(query);
150 if ( l.size() == 0 ) {
151 throw new ObjectNotFoundException( query );
152 } else if ( l.size() > 1 ) {
153 throw new NotUniqueException( query, l.size() );
154 }
155 return l.get(0);
156 }
157
158 public Object findOne(String query, Object[] values, Type[] types) {
159 List l = find(query,values,types);
160 if ( l.size() == 0 ) {
161 throw new ObjectNotFoundException( query, values );
162 } else if ( l.size() > 1 ) {
163 throw new NotUniqueException( query, values, l.size() );
164 }
165 return l.get(0);
166 }
167
168 public Object findOne(String query, Object value, Type type) {
169 List l = find(query,value,type);
170 if ( l.size() == 0 ) {
171 throw new ObjectNotFoundException( query, value );
172 } else if ( l.size() > 1 ) {
173 throw new NotUniqueException( query, value, l.size() );
174 }
175 return l.get(0);
176 }
177
178 public Iterator iterate(String query) {
179 Iterator i = null;
180 try {
181 // log.debug( "iterate-query(" + query + ")" );
182 i = session.iterate(query);
183 } catch ( Throwable t ) {
184 handleDatabaseException( t, "iterate(" + query + ")");
185 }
186 return i;
187 }
188
189 public Iterator iterate(String query, Object[] values, Type[] types) {
190 Iterator i = null;
191 try {
192 // log.debug( "iterate-query(" + query + "), parameters(" + ArrayUtil.toString( values ) + ")" );
193 i = session.iterate(query,values,types);
194 } catch ( Throwable t ) {
195 handleDatabaseException( t, "iterate(" + query + "," + ArrayUtil.toString(values) + ",types)");
196 }
197 return i;
198 }
199
200 public Iterator iterate(String query, Object value, Type type) {
201 Iterator i = null;
202 try {
203 // log.debug( "iterate-query(" + query + "), parameters(" + value + ")" );
204 i = session.iterate(query,value,type);
205 } catch ( Throwable t ) {
206 handleDatabaseException( t, "iterate(" + query + "," + value + ",type)");
207 }
208 return i;
209 }
210
211 public Object iterateOne(String query) {
212 Iterator i = iterate(query);
213 if ( ! i.hasNext() ) {
214 throw new ObjectNotFoundException( query );
215 }
216 Object o = i.next();
217 if ( i.hasNext() ) {
218 int nbrOfObjectsFound = 1;
219 while ( i.hasNext() ) {
220 i.next();
221 nbrOfObjectsFound++;
222 }
223 throw new NotUniqueException( query, nbrOfObjectsFound );
224 }
225 return o;
226 }
227
228 public Object iterateOne(String query, Object[] values, Type[] types) {
229 Iterator i = iterate(query,values,types);
230 if ( ! i.hasNext() ) {
231 throw new ObjectNotFoundException( query, values );
232 }
233 Object o = i.next();
234 if ( i.hasNext() ) {
235 int nbrOfObjectsFound = 1;
236 while ( i.hasNext() ) {
237 i.next();
238 nbrOfObjectsFound++;
239 }
240 throw new NotUniqueException( query, values, nbrOfObjectsFound );
241 }
242 return o;
243 }
244
245 public Object iterateOne(String query, Object value, Type type) {
246 Iterator i = iterate(query,value,type);
247 if ( ! i.hasNext() ) {
248 throw new ObjectNotFoundException( query, value );
249 }
250 Object o = i.next();
251 if ( i.hasNext() ) {
252 int nbrOfObjectsFound = 1;
253 while ( i.hasNext() ) {
254 i.next();
255 nbrOfObjectsFound++;
256 }
257 throw new NotUniqueException( query, value, nbrOfObjectsFound );
258 }
259 return o;
260 }
261
262 public LockMode getCurrentLockMode(Object object) {
263 LockMode l = null;
264 try {
265 l = session.getCurrentLockMode(object);
266 } catch ( Throwable t ) {
267 handleDatabaseException( t, "getCurrentLockMode(" + object + ")");
268 }
269 return l;
270 }
271
272 public void lock(Object object, LockMode lockMode) {
273 try {
274 session.lock(object,lockMode);
275 } catch ( Throwable t ) {
276 handleDatabaseException( t, "load(" + object + "," + lockMode + ")");
277 }
278 }
279
280 public FlushMode getFlushMode() {
281 FlushMode f = null;
282 try {
283 f = session.getFlushMode();
284 } catch ( Throwable t ) {
285 handleDatabaseException( t, "getFlushMode()");
286 }
287 return f;
288 }
289
290 public void setFlushMode(FlushMode flushMode) {
291 try {
292 session.setFlushMode(flushMode);
293 } catch ( Throwable t ) {
294 handleDatabaseException( t, "setFlushMode(" + flushMode + ")");
295 }
296 }
297
298 public Serializable getIdentifier(Object object) {
299 Serializable s = null;
300 try {
301 s = session.getIdentifier(object);
302 } catch ( Throwable t ) {
303 handleDatabaseException( t, "getIdentifier(" + object + ")");
304 }
305 return s;
306 }
307
308 public boolean isConnected() {
309 boolean b = false;
310 try {
311 b = session.isConnected();
312 } catch ( Throwable t ) {
313 handleDatabaseException( t, "isConnected()");
314 }
315 return b;
316 }
317
318 public boolean isOpen() {
319 boolean b = false;
320 try {
321 b = session.isOpen();
322 } catch ( Throwable t ) {
323 handleDatabaseException( t, "isOpen()");
324 }
325 return b;
326 }
327
328 public void delete(Object object) {
329 try {
330 session.delete(object);
331 } catch ( Throwable t ) {
332 handleDatabaseException( t, "delete(" + object + ")");
333 }
334 }
335
336 public int delete(String query) {
337 int i = -1;
338 try {
339 i = session.delete(query);
340 } catch ( Throwable t ) {
341 handleDatabaseException( t, "delete(" + query + ")");
342 }
343 return i;
344 }
345
346 public int delete(String query, Object[] values, Type[] types) {
347 int i = -1;
348 try {
349 i = session.delete(query,values,types);
350 } catch ( Throwable t ) {
351 handleDatabaseException( t, "delete(" + query + "," + ArrayUtil.toString(values) + ",types)");
352 }
353 return i;
354 }
355
356 public int delete(String query, Object value, Type type) {
357 int i = -1;
358 try {
359 i = session.delete(query,value,type);
360 } catch ( Throwable t ) {
361 handleDatabaseException( t, "delete(" + query + ",value,type)");
362 }
363 return i;
364 }
365
366 public Serializable save(Object object) {
367 Serializable s = null;
368 try {
369 s = session.save(object);
370 } catch ( Throwable t ) {
371 handleDatabaseException( t, "save(" + object + ")");
372 }
373 return s;
374 }
375
376 public void save(Object object, Serializable id) {
377 try {
378 session.save(object,id);
379 } catch ( Throwable t ) {
380 handleDatabaseException( t, "save(" + object + "," + id + ")");
381 }
382 }
383
384 public void saveOrUpdate(Object object) {
385 try {
386 session.saveOrUpdate(object);
387 } catch ( Throwable t ) {
388 handleDatabaseException( t, "saveOrUpdate(" + object + ")");
389 }
390 }
391
392 public void update(Object object) {
393 try {
394 session.update(object);
395 } catch ( Throwable t ) {
396 handleDatabaseException( t, "update(" + object + ")");
397 }
398 }
399
400 public void update(Object object, Serializable id) {
401 try {
402 session.update(object,id);
403 } catch ( Throwable t ) {
404 handleDatabaseException( t, "update(" + object + "," + id + ")");
405 }
406 }
407
408 public void flush() {
409 try {
410 session.flush();
411 } catch ( Throwable t ) {
412 handleDatabaseException( t, "flush()");
413 }
414 }
415
416 public void close() {
417 try {
418 if ( session != null ) session.close();
419 } catch ( Throwable t ) {
420 handleDatabaseException( t, "close()");
421 }
422 }
423
424 private void handleDatabaseException( Throwable t, String operation ) {
425 t.printStackTrace();
426
427 if ( session != null ) {
428 try {
429 session.close();
430 session = null;
431 } catch( Throwable t2 ) {
432 log.error( "couldn't close the database session properly" );
433 }
434 }
435
436 if ( t instanceof DbException ) {
437 throw (DbException) t;
438 } else {
439 throw new DbException( t.getClass().getName() + " while performing database operation '" + operation + "' : " + t.getMessage() );
440 }
441
442 }
443
444 private Session session = null;
445 private static final Logger log = Logger.getLogger(DbSession.class);
446 }