KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > pm > HibernatePesistenceSession


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.setup.impl.pm;
10
11
12 import org.jboss.portal.setup.pm.PersistenceSession;
13 import org.jboss.portal.setup.pm.PersistenceException;
14 import org.apache.log4j.Logger;
15 import org.hibernate.Transaction;
16 import org.hibernate.Session;
17 import org.hibernate.HibernateException;
18 import org.hibernate.dialect.Dialect;
19
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.Statement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
30  * Date: Apr 25, 2005; Time: 6:26:46 PM
31  * @since JBoss portal 2.0
32  * Class org.jboss.portal.setup.impl.pm.HibernatePesistenceSession
33  */

34 public class HibernatePesistenceSession implements PersistenceSession
35 {
36    private Dialect m_dialect;
37    private Session m_session;
38    private Transaction m_transaction = null;
39    private final Logger m_log;
40
41
42    HibernatePesistenceSession(Session session, Dialect dialect, Logger log)
43    {
44       m_session = session;
45       m_dialect = dialect;
46       m_log = log == null ? Logger.getLogger(HibernatePesistenceSession.class) : log;
47    }
48
49    public void close() throws PersistenceException
50    {
51       if (m_transaction != null)
52       {
53          //cannot close the session while transaction is open
54
throw new PersistenceException("Cannot close a session with open transaction!");
55       }
56       else
57       {
58          try
59          {
60             m_session.close();
61          }
62          catch (HibernateException he)
63          {
64             throw new PersistenceException("Failed to close persistence session!", he);
65          }
66       }
67    }
68
69    public void openTransaction() throws PersistenceException
70    {
71       if (m_transaction == null)
72       {
73          try
74          {
75             m_transaction = m_session.beginTransaction();
76          }
77          catch (HibernateException he)
78          {
79             throw new PersistenceException("Failed to setup transaction.", he);
80          }
81       }
82    }
83
84    public void commit() throws PersistenceException
85    {
86       try
87       {
88          if (m_session != null && m_session.isOpen())
89          {
90             m_session.flush();
91             if (m_transaction != null)
92             {
93                m_transaction.commit();
94             }
95          }
96          else
97          {
98             throw new PersistenceException("Cannot commit transaction with a closed session");
99          }
100       }
101       catch (HibernateException he)
102       {
103          throw new PersistenceException("Failed to commit transaction.", he);
104       }
105       finally
106       {
107          m_transaction = null;
108       }
109    }
110
111    public void rollback() throws PersistenceException
112    {
113       try
114       {
115          if (m_transaction != null)
116          {
117             m_transaction.rollback();
118          }
119       }
120       catch (HibernateException he)
121       {
122          throw new PersistenceException("Failed to rollback transaction.", he);
123       }
124       finally
125       {
126          m_transaction = null;
127       }
128    }
129
130    public boolean execute(List JavaDoc commands) throws PersistenceException
131    {
132       boolean result = false;
133       Statement JavaDoc stmt = null;
134       boolean trStarted = false;
135
136       try
137       {
138          if (m_transaction == null)
139          {
140             m_transaction = m_session.beginTransaction();
141             trStarted = true;
142          }
143
144          stmt = m_session.connection().createStatement();
145
146       }
147       catch (SQLException JavaDoc se)
148       {
149          throw new PersistenceException("Failed to create sql statement.", se);
150       }
151       catch (HibernateException he)
152       {
153          throw new PersistenceException("Failed to open hibernate session and setup transaction.");
154       }
155       String JavaDoc sql = null;
156       try
157       {
158          if (null != commands)
159          {
160             Iterator JavaDoc iter = commands.iterator();
161             while (iter.hasNext())
162             {
163
164                sql = (String JavaDoc)iter.next();
165                stmt.execute(sql);
166             }
167          }
168          result = true;
169          if (trStarted && null != m_transaction)
170          {
171             commit();
172          }
173       }
174       catch (SQLException JavaDoc se)
175       {
176          String JavaDoc msg = "Failed to execute sql statement: " + sql;
177          m_log.error(msg, se);
178          try
179          {
180             rollback();
181          }
182          catch (PersistenceException pe)
183          {
184             m_log.error("Failed to complete transaction!", pe);
185          }
186          throw new PersistenceException(msg, se);
187       }
188       finally
189       {
190
191          try
192          {
193             if (null != stmt)
194             {
195                stmt.close();
196             }
197          }
198          catch (SQLException JavaDoc se)
199          {
200             m_log.warn("Failed to close statement", se);
201          }
202          stmt = null;
203       }
204       return result;
205
206    }
207
208    /**
209     * @param expression sql expression to execute
210     * @return true if execution succeded without exceptions and result set was greater than one
211     * @throws PersistenceException
212     */

213    public boolean validate(String JavaDoc expression, boolean checkResultSize) throws PersistenceException
214    {
215       boolean result = false;
216       Connection JavaDoc conn = null;
217       Statement JavaDoc stmt = null;
218       ResultSet JavaDoc rs = null;
219       try
220       {
221
222          conn = m_session.connection();
223 //work around hibernate problem
224
if (conn.isClosed())
225          {
226             m_session.reconnect();
227             conn = m_session.connection();
228          }
229          stmt = conn.createStatement();
230          rs = stmt.executeQuery(expression);
231          if (!checkResultSize || (checkResultSize && rs.next()))
232          {
233 //result set is not empty - database have been initialized with data
234
result = true;
235          }
236
237       }
238       catch (SQLException JavaDoc se)
239       {
240 //assume schema does not exists if we don't need to check result size this is what we need
241
if (checkResultSize)
242          {
243             String JavaDoc msg = "Failed to execute a validation expression!";
244             m_log.error(msg, se);
245             throw new PersistenceException(msg, se);
246          }
247       }
248       catch (HibernateException he)
249       {
250 //failed to connect to dbloader, may be a sign of bigger problem
251
String JavaDoc msg = "Failed to execute a validation expression!";
252          m_log.error(msg, he);
253          throw new PersistenceException(msg, he);
254       }
255       finally
256       {
257          if (rs != null)
258          {
259             try
260             {
261                rs.close();
262             }
263             catch (SQLException JavaDoc se)
264             {
265                m_log.warn("Failed to close jdbc result set!", se);
266             }
267             rs = null;
268          }
269
270       }
271       return result;
272    }
273
274    public Session getSession()
275    {
276       return m_session;
277    }
278
279
280 }
281
Popular Tags