1 16 17 package org.springframework.orm.toplink; 18 19 import oracle.toplink.exceptions.ConcurrencyException; 20 import oracle.toplink.exceptions.ConversionException; 21 import oracle.toplink.exceptions.DatabaseException; 22 import oracle.toplink.exceptions.OptimisticLockException; 23 import oracle.toplink.exceptions.QueryException; 24 import oracle.toplink.exceptions.TopLinkException; 25 import oracle.toplink.sessions.Session; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.dao.ConcurrencyFailureException; 30 import org.springframework.dao.DataAccessException; 31 import org.springframework.dao.DataAccessResourceFailureException; 32 import org.springframework.dao.TypeMismatchDataAccessException; 33 import org.springframework.transaction.support.TransactionSynchronizationAdapter; 34 import org.springframework.transaction.support.TransactionSynchronizationManager; 35 import org.springframework.util.Assert; 36 37 48 public abstract class SessionFactoryUtils { 49 50 private static final Log logger = LogFactory.getLog(SessionFactoryUtils.class); 51 52 53 72 public static Session getSession(SessionFactory sessionFactory, boolean allowCreate) 73 throws DataAccessResourceFailureException, IllegalStateException { 74 75 try { 76 return doGetSession(sessionFactory, allowCreate); 77 } 78 catch (TopLinkException ex) { 79 throw new DataAccessResourceFailureException("Could not open TopLink Session", ex); 80 } 81 } 82 83 99 public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate) 100 throws TopLinkException, IllegalStateException { 101 102 Assert.notNull(sessionFactory, "No SessionFactory specified"); 103 104 SessionHolder sessionHolder = 105 (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); 106 if (sessionHolder != null) { 107 return sessionHolder.getSession(); 108 } 109 110 if (!allowCreate && !TransactionSynchronizationManager.isSynchronizationActive()) { 111 throw new IllegalStateException ("No TopLink Session bound to thread, " + 112 "and configuration does not allow creation of non-transactional one here"); 113 } 114 115 logger.debug("Creating TopLink Session"); 116 Session session = sessionFactory.createSession(); 117 118 if (TransactionSynchronizationManager.isSynchronizationActive()) { 119 logger.debug("Registering new Spring transaction synchronization for new TopLink Session"); 120 sessionHolder = new SessionHolder(session); 123 sessionHolder.setSynchronizedWithTransaction(true); 124 TransactionSynchronizationManager.registerSynchronization( 125 new SessionSynchronization(sessionHolder, sessionFactory)); 126 TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder); 127 } 128 129 return session; 130 } 131 132 140 public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory) { 141 if (sessionFactory == null) { 142 return false; 143 } 144 SessionHolder sessionHolder = 145 (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); 146 return (sessionHolder != null && session == sessionHolder.getSession()); 147 } 148 149 155 public static DataAccessException convertTopLinkAccessException(TopLinkException ex) { 156 if (ex instanceof DatabaseException) { 157 return new TopLinkJdbcException((DatabaseException) ex); 160 } 161 if (ex instanceof OptimisticLockException) { 162 return new TopLinkOptimisticLockingFailureException((OptimisticLockException) ex); 163 } 164 if (ex instanceof QueryException) { 165 return new TopLinkQueryException((QueryException) ex); 166 } 167 if (ex instanceof ConcurrencyException) { 168 return new ConcurrencyFailureException(ex.getMessage(), ex); 169 } 170 if (ex instanceof ConversionException) { 171 return new TypeMismatchDataAccessException(ex.getMessage(), ex); 172 } 173 return new TopLinkSystemException(ex); 175 } 176 177 184 public static void releaseSession(Session session, SessionFactory sessionFactory) { 185 if (session == null) { 186 return; 187 } 188 if (!isSessionTransactional(session, sessionFactory)) { 190 doRelease(session); 191 } 192 } 193 194 198 private static void doRelease(Session session) { 199 if (session != null) { 200 logger.debug("Closing TopLink Session"); 201 try { 202 session.release(); 203 } 204 catch (TopLinkException ex) { 205 logger.debug("Could not close TopLink Session", ex); 206 } 207 catch (Throwable ex) { 208 logger.debug("Unexpected exception on closing TopLink Session", ex); 209 } 210 } 211 } 212 213 214 219 private static class SessionSynchronization extends TransactionSynchronizationAdapter { 220 221 private final SessionHolder sessionHolder; 222 223 private final SessionFactory sessionFactory; 224 225 private boolean holderActive = true; 226 227 private SessionSynchronization(SessionHolder sessionHolder, SessionFactory sessionFactory) { 228 this.sessionHolder = sessionHolder; 229 this.sessionFactory = sessionFactory; 230 } 231 232 public void suspend() { 233 if (this.holderActive) { 234 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 235 } 236 } 237 238 public void resume() { 239 if (this.holderActive) { 240 TransactionSynchronizationManager.bindResource(this.sessionFactory, this.sessionHolder); 241 } 242 } 243 244 public void beforeCompletion() { 245 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 246 this.holderActive = false; 247 } 248 249 public void afterCompletion(int status) { 250 releaseSession(this.sessionHolder.getSession(), this.sessionFactory); 251 } 252 } 253 254 } 255 | Popular Tags |