1 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 ; 22 import java.sql.Statement ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.util.List ; 26 import java.util.Iterator ; 27 28 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 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 commands) throws PersistenceException 131 { 132 boolean result = false; 133 Statement 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 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 sql = null; 156 try 157 { 158 if (null != commands) 159 { 160 Iterator iter = commands.iterator(); 161 while (iter.hasNext()) 162 { 163 164 sql = (String )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 se) 175 { 176 String 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 se) 199 { 200 m_log.warn("Failed to close statement", se); 201 } 202 stmt = null; 203 } 204 return result; 205 206 } 207 208 213 public boolean validate(String expression, boolean checkResultSize) throws PersistenceException 214 { 215 boolean result = false; 216 Connection conn = null; 217 Statement stmt = null; 218 ResultSet rs = null; 219 try 220 { 221 222 conn = m_session.connection(); 223 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 = true; 235 } 236 237 } 238 catch (SQLException se) 239 { 240 if (checkResultSize) 242 { 243 String 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 String 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 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 |