1 16 package com.ibatis.dao.engine.transaction.toplink; 17 18 import oracle.toplink.exceptions.TopLinkException; 19 import oracle.toplink.sessions.Session; 20 import oracle.toplink.sessions.UnitOfWork; 21 import oracle.toplink.threetier.Server; 22 23 import com.ibatis.dao.client.DaoException; 24 import com.ibatis.dao.client.DaoTransaction; 25 26 34 public class ToplinkDaoTransaction implements DaoTransaction { 35 36 39 private Server server; 40 41 private Session session; 42 43 private UnitOfWork unitOfWork; 44 45 private boolean commmitted = false; 46 47 50 62 public ToplinkDaoTransaction(UnitOfWork uow, Server server) 63 throws DaoException { 64 65 if (server == null) { 67 68 throw new DaoException("Toplink Server not available"); 69 70 } 71 72 this.unitOfWork = uow; 74 this.server = server; 75 76 } 77 78 81 89 public Session getSession() throws DaoException { 90 91 try { 93 94 if (session == null) { 95 96 session = server.acquireClientSession(); 97 98 } 99 100 return session; 101 102 } catch (TopLinkException e) { 103 104 throw new DaoException("Error aquiring Session", e); 105 106 } 107 108 } 109 110 118 public UnitOfWork getUnitOfWork() throws DaoException { 119 120 try { 121 122 if (unitOfWork == null) { 123 124 unitOfWork = getSession().acquireUnitOfWork(); 125 126 } 127 128 return unitOfWork; 129 130 } catch (TopLinkException e) { 131 132 throw new DaoException("Error acquiring UnitOfWork.", e); 133 134 } 135 136 } 137 138 144 public void commit() throws DaoException { 145 146 if (commmitted) { 148 149 throw new DaoException("Transaction already committed"); 150 151 } 152 153 try { 155 156 if (unitOfWork != null) { 158 159 unitOfWork.commit(); 161 unitOfWork.release(); 162 session.release(); 163 164 } 165 166 } catch (TopLinkException e) { 167 168 throw new DaoException("Error committing transaction", e); 169 170 } 171 commmitted = true; 172 173 } 174 175 182 public void rollback() throws DaoException { 183 184 if (!commmitted) { 186 187 try { 188 189 if ((unitOfWork != null) && unitOfWork.isActive()) { 191 192 unitOfWork.revertAndResume(); 193 unitOfWork.release(); 194 session.release(); 195 196 } 197 198 } catch (TopLinkException e) { 199 200 throw new DaoException("Error rolling back transaction", e); 201 202 } 203 204 } 205 206 } 207 208 214 protected void finalize() throws Throwable { 215 216 super.finalize(); 217 218 if (unitOfWork.isActive()) { 220 221 commit(); 222 223 } 224 225 if ((session != null) && (session.isConnected())) { 227 228 session.release(); 229 230 } 231 232 } 233 234 } | Popular Tags |