Subversion Repositories general

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
924 dev 1
package ak.hostadmiral.util;
919 dev 2
 
1015 dev 3
import java.util.Collection;
4
 
919 dev 5
import net.sf.hibernate.*;
6
import net.sf.hibernate.cfg.*;
7
 
8
public class HibernateUtil
9
{
1015 dev 10
	public static final int DATABASE_VERSION = 1;
11
 
919 dev 12
	private static       Configuration  configuration;
13
	private static       SessionFactory sessionFactory;
14
	private static final ThreadLocal    hibernateBean = new ThreadLocal();
1015 dev 15
    private static       boolean        validated     = false;
919 dev 16
 
1015 dev 17
    private static void validate()
18
		throws HibernateException, ModelException
19
    {
20
    	synchronized(HibernateUtil.class) {
21
    		if(validated) return;
22
 
23
			Collection versions = currentSession().find("from DatabaseVersion");
24
 
25
            if(versions == null || versions.size() == 0)
26
            	throw new ModelException("Database structure version not found");
27
 
28
            if(versions.size() > 1)
29
            	throw new ModelException("Too much entries in database structure version table");
30
 
31
            int version = ((DatabaseVersion)versions.iterator().next()).getVersion();
32
            if(version != DATABASE_VERSION)
33
            	throw new ModelException("Expected database structure version "
34
            		+ DATABASE_VERSION + ", found " + version);
35
 
36
    		validated = true;
37
    	}
38
    }
39
 
919 dev 40
	public static Configuration getConfiguration()
41
		throws HibernateException
42
	{
43
		if(configuration == null)
44
			configuration = new Configuration();
45
 
46
		return configuration;
47
	}
48
 
49
	public static SessionFactory getSessionFactory()
50
		throws HibernateException
51
	{
52
		if(sessionFactory == null)
53
			sessionFactory = getConfiguration().configure().buildSessionFactory();
54
 
55
		return sessionFactory;
56
	}
57
 
58
	private static HibernateBean currentBean()
59
		throws HibernateException
60
	{
61
		HibernateBean hb = (HibernateBean)hibernateBean.get();
62
 
63
		if(hb == null) {
64
			hb = new HibernateBean();
65
			hb.session = getSessionFactory().openSession();
66
			hibernateBean.set(hb);
67
		}
68
		return hb;
69
	}
70
 
71
	public static Session currentSession()
72
		throws HibernateException
73
	{
74
		return currentBean().session;
75
	}
76
 
77
	public static void closeSession()
78
		throws HibernateException, ModelException
79
	{
80
		HibernateBean hb = (HibernateBean)hibernateBean.get();
81
 
82
		if(hb == null)
83
			throw new ModelException("No session found for this thread");
84
 
85
		hibernateBean.set(null);
86
		hb.session.close();
87
	}
88
 
89
	public static void beginTransaction()
90
		throws HibernateException, ModelException
91
	{
92
		HibernateBean hb = (HibernateBean)hibernateBean.get();
93
 
94
		if(hb != null && hb.transaction != null)
95
			throw new ModelException("Transaction is already open");
96
 
97
		currentBean().transaction = currentSession().beginTransaction();
1015 dev 98
 
99
		// validate database structure version
100
    	if(!validated) // just try to speed up by avoiding synchronization
101
			validate();
919 dev 102
	}
103
 
104
	public static boolean isTransactionOpen()
105
		throws HibernateException, ModelException
106
	{
107
		HibernateBean hb = (HibernateBean)hibernateBean.get();
108
 
109
		return (hb != null) && (hb.transaction != null);
110
	}
111
 
112
	public static void commitTransaction()
113
		throws HibernateException, ModelException
114
	{
115
		HibernateBean hb = (HibernateBean)hibernateBean.get();
116
 
117
		if(hb == null || hb.transaction == null)
118
			throw new ModelException("No open transaction");
119
 
120
		hb.transaction.commit();
121
		hb.transaction = null;
122
	}
123
 
124
	public static void rollbackTransaction()
125
		throws HibernateException, ModelException
126
	{
127
		HibernateBean hb = (HibernateBean)hibernateBean.get();
128
 
129
		if(hb == null || hb.transaction == null)
130
			throw new ModelException("No open transaction");
131
 
132
		hb.transaction.rollback();
133
		hb.transaction = null;
134
	}
135
 
136
	static class HibernateBean
137
	{
138
		public Session     session;
139
		public Transaction transaction;
140
	}
141
}