|
[Springframework-cvs] spring/src/org/springframework/orm/hibernate3
HibernateTransactionManager.java, 1.40, 1.41
From: Juergen Hoeller <jhoeller@us...> - 2008-05-07 10:55
|
Update of /cvsroot/springframework/spring/src/org/springframework/orm/hibernate3
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv32700/src/org/springframework/orm/hibernate3
Modified Files:
HibernateTransactionManager.java
Log Message:
added "hibernateManagedSession" flag to HibernateTransactionManager, for working with a custom CurrentSessionContext
Index: HibernateTransactionManager.java
===================================================================
RCS file: /cvsroot/springframework/spring/src/org/springframework/orm/hibernate3/HibernateTransactionManager.java,v
retrieving revision 1.40
retrieving revision 1.41
diff -C2 -d -r1.40 -r1.41
*** HibernateTransactionManager.java 5 May 2008 12:02:28 -0000 1.40
--- HibernateTransactionManager.java 7 May 2008 10:55:41 -0000 1.41
***************
*** 37,40 ****
--- 37,41 ----
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
+ import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.jdbc.datasource.DataSourceUtils;
***************
*** 141,144 ****
--- 142,147 ----
private boolean prepareConnection = true;
+ private boolean hibernateManagedSession = false;
+
private boolean earlyFlushBeforeCommit = false;
***************
*** 266,269 ****
--- 269,300 ----
/**
+ * Set whether to operate on a Hibernate-managed Session instead of a
+ * Spring-managed Session, that is, whether to obtain the Session through
+ * Hibernate's {@link org.hibernate.SessionFactory#getCurrentSession()}
+ * instead of {@link org.hibernate.SessionFactory#openSession()} (with a Spring
+ * {@link org.springframework.transaction.support.TransactionSynchronizationManager}
+ * check preceding it).
+ * <p>Default is "false", i.e. using a Spring-managed Session: taking the current
+ * thread-bound Session if available (e.g. in an Open-Session-in-View scenario),
+ * creating a new Session for the current transaction otherwise.
+ * <p>Switch this flag to "true" in order to enforce use of a Hibernate-managed Session.
+ * Note that this requires {@link org.hibernate.SessionFactory#getCurrentSession()}
+ * to always return a proper Session when called for a Spring-managed transaction;
+ * transaction begin will fail if the <code>getCurrentSession()</code> call fails.
+ * <p>This mode will typically be used in combination with a custom Hibernate
+ * {@link org.hibernate.context.CurrentSessionContext} implementation that stores
+ * Sessions in a place other than Spring's TransactionSynchronizationManager.
+ * It may also be used in combination with Spring's Open-Session-in-View support
+ * (using Spring's default {@link SpringSessionContext}), in which case it subtly
+ * differs from the Spring-managed Session mode: The pre-bound Session will <i>not</i>
+ * receive a <code>clear()</code> call (on rollback) or a <code>disconnect()</code>
+ * call (on transaction completion) in such a scenario; this is rather left up
+ * to a custom CurrentSessionContext implementation (if desired).
+ */
+ public void setHibernateManagedSession(boolean hibernateManagedSession) {
+ this.hibernateManagedSession = hibernateManagedSession;
+ }
+
+ /**
* Set whether to perform an early flush before proceeding with a commit.
* <p>Default is "false", performing an implicit flush as part of the
***************
*** 412,416 ****
SessionFactoryUtils.toString(sessionHolder.getSession()) + "] for Hibernate transaction");
}
! txObject.setSessionHolder(sessionHolder, false);
}
--- 443,461 ----
SessionFactoryUtils.toString(sessionHolder.getSession()) + "] for Hibernate transaction");
}
! txObject.setSessionHolder(sessionHolder);
! }
! else if (this.hibernateManagedSession) {
! try {
! Session session = getSessionFactory().getCurrentSession();
! if (logger.isDebugEnabled()) {
! logger.debug("Found Hibernate-managed Session [" +
! SessionFactoryUtils.toString(session) + "] for Spring-managed transaction");
! }
! txObject.setExistingSession(session);
! }
! catch (HibernateException ex) {
! throw new DataAccessResourceFailureException(
! "Could not obtain Hibernate-managed Session for Spring-managed transaction", ex);
! }
}
***************
*** 425,429 ****
protected boolean isExistingTransaction(Object transaction) {
! return ((HibernateTransactionObject) transaction).hasTransaction();
}
--- 470,476 ----
protected boolean isExistingTransaction(Object transaction) {
! HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
! return (txObject.hasSpringManagedTransaction() ||
! (this.hibernateManagedSession && txObject.hasHibernateManagedTransaction()));
}
***************
*** 450,454 ****
"] for Hibernate transaction");
}
! txObject.setSessionHolder(new SessionHolder(newSession), true);
}
--- 497,501 ----
"] for Hibernate transaction");
}
! txObject.setSession(newSession);
}
***************
*** 482,491 ****
}
! if (definition.isReadOnly() && txObject.isNewSessionHolder()) {
// Just set to NEVER in case of a new Session for this transaction.
session.setFlushMode(FlushMode.NEVER);
}
! if (!definition.isReadOnly() && !txObject.isNewSessionHolder()) {
// We need AUTO or COMMIT for a non-read-only transaction.
FlushMode flushMode = session.getFlushMode();
--- 529,538 ----
}
! if (definition.isReadOnly() && txObject.isNewSession()) {
// Just set to NEVER in case of a new Session for this transaction.
session.setFlushMode(FlushMode.NEVER);
}
! if (!definition.isReadOnly() && !txObject.isNewSession()) {
// We need AUTO or COMMIT for a non-read-only transaction.
FlushMode flushMode = session.getFlushMode();
***************
*** 537,541 ****
catch (Exception ex) {
! if (txObject.isNewSessionHolder()) {
try {
if (session.getTransaction().isActive()) {
--- 584,588 ----
catch (Exception ex) {
! if (txObject.isNewSession()) {
try {
if (session.getTransaction().isActive()) {
***************
*** 556,560 ****
protected Object doSuspend(Object transaction) {
HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
! txObject.setSessionHolder(null, false);
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
--- 603,607 ----
protected Object doSuspend(Object transaction) {
HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
! txObject.setSessionHolder(null);
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
***************
*** 635,639 ****
}
finally {
! if (!txObject.isNewSessionHolder()) {
// Clear all pending inserts/updates/deletes in the Session.
// Necessary for pre-bound Sessions, to avoid inconsistent state.
--- 682,686 ----
}
finally {
! if (!txObject.isNewSession() && !this.hibernateManagedSession) {
// Clear all pending inserts/updates/deletes in the Session.
// Necessary for pre-bound Sessions, to avoid inconsistent state.
***************
*** 679,683 ****
}
! if (txObject.isNewSessionHolder()) {
if (logger.isDebugEnabled()) {
logger.debug("Closing Hibernate Session [" + SessionFactoryUtils.toString(session) +
--- 726,730 ----
}
! if (txObject.isNewSession()) {
if (logger.isDebugEnabled()) {
logger.debug("Closing Hibernate Session [" + SessionFactoryUtils.toString(session) +
***************
*** 694,698 ****
session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
}
! session.disconnect();
}
txObject.getSessionHolder().clear();
--- 741,747 ----
session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
}
! if (!this.hibernateManagedSession) {
! session.disconnect();
! }
}
txObject.getSessionHolder().clear();
***************
*** 782,788 ****
private boolean newSessionHolder;
! public void setSessionHolder(SessionHolder sessionHolder, boolean newSessionHolder) {
this.sessionHolder = sessionHolder;
! this.newSessionHolder = newSessionHolder;
}
--- 831,852 ----
private boolean newSessionHolder;
! private boolean newSession;
!
! public void setSession(Session session) {
! this.sessionHolder = new SessionHolder(session);
! this.newSessionHolder = true;
! this.newSession = true;
! }
!
! public void setExistingSession(Session session) {
! this.sessionHolder = new SessionHolder(session);
! this.newSessionHolder = true;
! this.newSession = false;
! }
!
! public void setSessionHolder(SessionHolder sessionHolder) {
this.sessionHolder = sessionHolder;
! this.newSessionHolder = false;
! this.newSession = false;
}
***************
*** 795,802 ****
}
! public boolean hasTransaction() {
return (this.sessionHolder != null && this.sessionHolder.getTransaction() != null);
}
public void setRollbackOnly() {
getSessionHolder().setRollbackOnly();
--- 859,874 ----
}
! public boolean isNewSession() {
! return this.newSession;
! }
!
! public boolean hasSpringManagedTransaction() {
return (this.sessionHolder != null && this.sessionHolder.getTransaction() != null);
}
+ public boolean hasHibernateManagedTransaction() {
+ return (this.sessionHolder != null && this.sessionHolder.getSession().getTransaction().isActive());
+ }
+
public void setRollbackOnly() {
getSessionHolder().setRollbackOnly();
|
| Thread | Author | Date |
|---|---|---|
| [Springframework-cvs] spring/src/org/springframework/orm/hibernate3 HibernateTransactionManager.java, 1.40, 1.41 | Juergen Hoeller <jhoeller@us...> |