1 16 17 package org.springframework.orm.hibernate; 18 19 import javax.transaction.SystemException ; 20 import javax.transaction.Transaction ; 21 import javax.transaction.TransactionManager ; 22 23 import net.sf.hibernate.FlushMode; 24 import net.sf.hibernate.HibernateException; 25 import net.sf.hibernate.JDBCException; 26 import net.sf.hibernate.Session; 27 import net.sf.hibernate.SessionFactory; 28 import net.sf.hibernate.engine.SessionImplementor; 29 30 import org.springframework.core.Ordered; 31 import org.springframework.dao.DataAccessException; 32 import org.springframework.dao.DataAccessResourceFailureException; 33 import org.springframework.jdbc.support.SQLExceptionTranslator; 34 import org.springframework.transaction.support.TransactionSynchronizationAdapter; 35 import org.springframework.transaction.support.TransactionSynchronizationManager; 36 37 45 class SpringSessionSynchronization extends TransactionSynchronizationAdapter implements Ordered { 46 47 private final SessionHolder sessionHolder; 48 49 private final SessionFactory sessionFactory; 50 51 private final SQLExceptionTranslator jdbcExceptionTranslator; 52 53 private final boolean newSession; 54 55 59 private boolean hibernateTransactionCompletion = false; 60 61 private Transaction jtaTransaction; 62 63 private boolean holderActive = true; 64 65 66 public SpringSessionSynchronization( 67 SessionHolder sessionHolder, SessionFactory sessionFactory, 68 SQLExceptionTranslator jdbcExceptionTranslator, boolean newSession) { 69 70 this.sessionHolder = sessionHolder; 71 this.sessionFactory = sessionFactory; 72 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 73 this.newSession = newSession; 74 75 TransactionManager jtaTm = 77 SessionFactoryUtils.getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession()); 78 if (jtaTm != null) { 79 this.hibernateTransactionCompletion = true; 80 try { 84 this.jtaTransaction = jtaTm.getTransaction(); 85 } 86 catch (SystemException ex) { 87 throw new DataAccessResourceFailureException("Could not access JTA transaction", ex); 88 } 89 } 90 } 91 92 93 public int getOrder() { 94 return SessionFactoryUtils.SESSION_SYNCHRONIZATION_ORDER; 95 } 96 97 public void suspend() { 98 if (this.holderActive) { 99 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 100 } 101 } 102 103 public void resume() { 104 if (this.holderActive) { 105 TransactionSynchronizationManager.bindResource(this.sessionFactory, this.sessionHolder); 106 } 107 } 108 109 public void beforeCommit(boolean readOnly) throws DataAccessException { 110 if (!readOnly) { 111 SessionFactoryUtils.logger.debug("Flushing Hibernate Session on transaction synchronization"); 113 Session session = null; 114 if (this.jtaTransaction != null) { 117 session = this.sessionHolder.getSession(this.jtaTransaction); 118 } 119 if (session == null) { 120 session = this.sessionHolder.getSession(); 121 } 122 if (!session.getFlushMode().equals(FlushMode.NEVER)) { 124 try { 125 session.flush(); 126 } 127 catch (JDBCException ex) { 128 if (this.jdbcExceptionTranslator != null) { 129 throw this.jdbcExceptionTranslator.translate( 130 "Hibernate transaction synchronization: " + ex.getMessage(), null, ex.getSQLException()); 131 } 132 else { 133 throw new HibernateJdbcException(ex); 134 } 135 } 136 catch (HibernateException ex) { 137 throw SessionFactoryUtils.convertHibernateAccessException(ex); 138 } 139 } 140 } 141 } 142 143 public void beforeCompletion() { 144 if (this.jtaTransaction != null) { 145 Session session = this.sessionHolder.removeSession(this.jtaTransaction); 148 if (session != null) { 149 if (this.sessionHolder.isEmpty()) { 150 if (TransactionSynchronizationManager.hasResource(this.sessionFactory)) { 152 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 159 } 160 this.holderActive = false; 161 } 162 if (session != this.sessionHolder.getSession()) { 165 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, this.sessionFactory); 166 } 167 else if (this.sessionHolder.getPreviousFlushMode() != null) { 168 session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); 170 } 171 return; 172 } 173 } 174 if (this.newSession) { 176 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 178 this.holderActive = false; 179 if (this.hibernateTransactionCompletion) { 180 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(this.sessionHolder.getSession(), this.sessionFactory); 185 } 186 } 187 else if (this.sessionHolder.getPreviousFlushMode() != null) { 188 this.sessionHolder.getSession().setFlushMode(this.sessionHolder.getPreviousFlushMode()); 190 } 191 } 192 193 public void afterCompletion(int status) { 194 if (!this.hibernateTransactionCompletion || !this.newSession) { 195 Session session = this.sessionHolder.getSession(); 199 if (session instanceof SessionImplementor) { 202 ((SessionImplementor) session).afterTransactionCompletion(status == STATUS_COMMITTED); 203 } 204 if (this.newSession) { 207 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, this.sessionFactory); 208 } 209 } 210 if (!this.newSession && status != STATUS_COMMITTED) { 211 this.sessionHolder.getSession().clear(); 214 } 215 if (this.sessionHolder.doesNotHoldNonDefaultSession()) { 216 this.sessionHolder.setSynchronizedWithTransaction(false); 217 } 218 } 219 220 } 221 | Popular Tags |