Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
924 | dev | 1 | package ak.hostadmiral.util; |
919 | dev | 2 | |
3 | import net.sf.hibernate.*; |
||
4 | import net.sf.hibernate.cfg.*; |
||
5 | |||
6 | public class HibernateUtil |
||
7 | { |
||
8 | private static Configuration configuration; |
||
9 | private static SessionFactory sessionFactory; |
||
10 | private static final ThreadLocal hibernateBean = new ThreadLocal(); |
||
11 | |||
12 | public static Configuration getConfiguration() |
||
13 | throws HibernateException |
||
14 | { |
||
15 | if(configuration == null) |
||
16 | configuration = new Configuration(); |
||
17 | |||
18 | return configuration; |
||
19 | } |
||
20 | |||
21 | public static SessionFactory getSessionFactory() |
||
22 | throws HibernateException |
||
23 | { |
||
24 | if(sessionFactory == null) |
||
25 | sessionFactory = getConfiguration().configure().buildSessionFactory(); |
||
26 | |||
27 | return sessionFactory; |
||
28 | } |
||
29 | |||
30 | private static HibernateBean currentBean() |
||
31 | throws HibernateException |
||
32 | { |
||
33 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
34 | |||
35 | if(hb == null) { |
||
36 | hb = new HibernateBean(); |
||
37 | hb.session = getSessionFactory().openSession(); |
||
38 | hibernateBean.set(hb); |
||
39 | } |
||
40 | return hb; |
||
41 | } |
||
42 | |||
43 | public static Session currentSession() |
||
44 | throws HibernateException |
||
45 | { |
||
46 | return currentBean().session; |
||
47 | } |
||
48 | |||
49 | public static void closeSession() |
||
50 | throws HibernateException, ModelException |
||
51 | { |
||
52 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
53 | |||
54 | if(hb == null) |
||
55 | throw new ModelException("No session found for this thread"); |
||
56 | |||
57 | hibernateBean.set(null); |
||
58 | hb.session.close(); |
||
59 | } |
||
60 | |||
61 | public static void beginTransaction() |
||
62 | throws HibernateException, ModelException |
||
63 | { |
||
64 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
65 | |||
66 | if(hb != null && hb.transaction != null) |
||
67 | throw new ModelException("Transaction is already open"); |
||
68 | |||
69 | currentBean().transaction = currentSession().beginTransaction(); |
||
70 | } |
||
71 | |||
72 | public static boolean isTransactionOpen() |
||
73 | throws HibernateException, ModelException |
||
74 | { |
||
75 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
76 | |||
77 | return (hb != null) && (hb.transaction != null); |
||
78 | } |
||
79 | |||
80 | public static void commitTransaction() |
||
81 | throws HibernateException, ModelException |
||
82 | { |
||
83 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
84 | |||
85 | if(hb == null || hb.transaction == null) |
||
86 | throw new ModelException("No open transaction"); |
||
87 | |||
88 | hb.transaction.commit(); |
||
89 | hb.transaction = null; |
||
90 | } |
||
91 | |||
92 | public static void rollbackTransaction() |
||
93 | throws HibernateException, ModelException |
||
94 | { |
||
95 | HibernateBean hb = (HibernateBean)hibernateBean.get(); |
||
96 | |||
97 | if(hb == null || hb.transaction == null) |
||
98 | throw new ModelException("No open transaction"); |
||
99 | |||
100 | hb.transaction.rollback(); |
||
101 | hb.transaction = null; |
||
102 | } |
||
103 | |||
104 | static class HibernateBean |
||
105 | { |
||
106 | public Session session; |
||
107 | public Transaction transaction; |
||
108 | } |
||
109 | } |