1 16 17 package org.springframework.orm.hibernate3; 18 19 import javax.transaction.SystemException ; 20 import javax.transaction.Transaction ; 21 import javax.transaction.TransactionManager ; 22 23 import org.hibernate.FlushMode; 24 import org.hibernate.HibernateException; 25 import org.hibernate.JDBCException; 26 import org.hibernate.Session; 27 import org.hibernate.SessionFactory; 28 import org.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 46 class SpringSessionSynchronization extends TransactionSynchronizationAdapter implements Ordered { 47 48 private final SessionHolder sessionHolder; 49 50 private final SessionFactory sessionFactory; 51 52 private final SQLExceptionTranslator jdbcExceptionTranslator; 53 54 private final boolean newSession; 55 56 60 private boolean hibernateTransactionCompletion = false; 61 62 private Transaction jtaTransaction; 63 64 private boolean holderActive = true; 65 66 67 public SpringSessionSynchronization( 68 SessionHolder sessionHolder, SessionFactory sessionFactory, 69 SQLExceptionTranslator jdbcExceptionTranslator, boolean newSession) { 70 71 this.sessionHolder = sessionHolder; 72 this.sessionFactory = sessionFactory; 73 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 74 this.newSession = newSession; 75 76 TransactionManager jtaTm = 78 SessionFactoryUtils.getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession()); 79 if (jtaTm != null) { 80 this.hibernateTransactionCompletion = true; 81 try { 85 this.jtaTransaction = jtaTm.getTransaction(); 86 } 87 catch (SystemException ex) { 88 throw new DataAccessResourceFailureException("Could not access JTA transaction", ex); 89 } 90 } 91 } 92 93 94 public int getOrder() { 95 return SessionFactoryUtils.SESSION_SYNCHRONIZATION_ORDER; 96 } 97 98 public void suspend() { 99 if (this.holderActive) { 100 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 101 } 102 } 103 104 public void resume() { 105 if (this.holderActive) { 106 TransactionSynchronizationManager.bindResource(this.sessionFactory, this.sessionHolder); 107 } 108 } 109 110 public void beforeCommit(boolean readOnly) throws DataAccessException { 111 if (!readOnly) { 112 Session session = null; 113 if (this.jtaTransaction != null) { 116 session = this.sessionHolder.getSession(this.jtaTransaction); 117 } 118 if (session == null) { 119 session = this.sessionHolder.getSession(); 120 } 121 if (!session.getFlushMode().lessThan(FlushMode.COMMIT)) { 124 try { 125 SessionFactoryUtils.logger.debug("Flushing Hibernate Session on transaction synchronization"); 126 session.flush(); 127 } 128 catch (HibernateException ex) { 129 if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { 130 JDBCException jdbcEx = (JDBCException) ex; 131 throw this.jdbcExceptionTranslator.translate( 132 "Hibernate flushing: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); 133 } 134 throw SessionFactoryUtils.convertHibernateAccessException(ex); 135 } 136 } 137 } 138 } 139 140 public void beforeCompletion() { 141 if (this.jtaTransaction != null) { 142 Session session = this.sessionHolder.removeSession(this.jtaTransaction); 145 if (session != null) { 146 if (this.sessionHolder.isEmpty()) { 147 if (TransactionSynchronizationManager.hasResource(this.sessionFactory)) { 149 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 156 } 157 this.holderActive = false; 158 } 159 if (session != this.sessionHolder.getSession()) { 162 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, this.sessionFactory); 163 } 164 else if (this.sessionHolder.getPreviousFlushMode() != null) { 165 session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); 167 } 168 return; 169 } 170 } 171 if (this.newSession) { 173 TransactionSynchronizationManager.unbindResource(this.sessionFactory); 175 this.holderActive = false; 176 if (this.hibernateTransactionCompletion) { 177 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(this.sessionHolder.getSession(), this.sessionFactory); 182 } 183 } 184 else if (this.sessionHolder.getPreviousFlushMode() != null) { 185 this.sessionHolder.getSession().setFlushMode(this.sessionHolder.getPreviousFlushMode()); 187 } 188 } 189 190 public void afterCompletion(int status) { 191 if (!this.hibernateTransactionCompletion || !this.newSession) { 192 Session session = this.sessionHolder.getSession(); 196 if (session instanceof SessionImplementor) { 199 ((SessionImplementor) session).afterTransactionCompletion(status == STATUS_COMMITTED, null); 200 } 201 if (this.newSession) { 204 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, this.sessionFactory); 205 } 206 } 207 if (!this.newSession && status != STATUS_COMMITTED) { 208 this.sessionHolder.getSession().clear(); 211 } 212 if (this.sessionHolder.doesNotHoldNonDefaultSession()) { 213 this.sessionHolder.setSynchronizedWithTransaction(false); 214 } 215 } 216 217 } 218 | Popular Tags |