01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/HibernateUtil.java $
03: * $Id: HibernateUtil.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the"License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.tool.assessment.util;
21:
22: import org.hibernate.*;
23: import org.hibernate.cfg.*;
24:
25: public class HibernateUtil {
26:
27: private static final SessionFactory sessionFactory;
28:
29: static {
30: try {
31: sessionFactory = new Configuration().configure()
32: .buildSessionFactory();
33: } catch (HibernateException ex) {
34: throw new RuntimeException(
35: "Exception building SessionFactory: "
36: + ex.getMessage(), ex);
37: }
38: }
39:
40: public static final ThreadLocal session = new ThreadLocal();
41:
42: public static Session currentSession() throws HibernateException {
43: Session s = (Session) session.get();
44: // Open a new Session, if this Thread has none yet
45: if (s == null) {
46: s = sessionFactory.openSession();
47: session.set(s);
48: }
49: return s;
50: }
51:
52: public static void closeSession() throws HibernateException {
53: Session s = (Session) session.get();
54: session.set(null);
55: if (s != null)
56: s.close();
57: }
58: }
|