Author: agilliland
Date: Wed Apr 19 10:32:24 2006
New Revision: 395316

URL: http://svn.apache.org/viewcvs?rev=395316&view=rev
Log:
some tweaks to make sure that we don't need to use a jdbc connection for 
requests that don't actually use a session.


Modified:
    
incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java

Modified: 
incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
URL: 
http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java?rev=395316&r1=395315&r2=395316&view=diff
==============================================================================
--- 
incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
 (original)
+++ 
incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
 Wed Apr 19 10:32:24 2006
@@ -3,7 +3,6 @@
  */
 package org.roller.business.hibernate;
 
-import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -12,6 +11,7 @@
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;
 import org.roller.RollerException;
 import org.roller.pojos.Assoc;
@@ -58,6 +58,8 @@
      */
     protected Session getSession() {
         
+        log.debug("Opening Hibernate Session");
+        
         // get Hibernate Session and make sure we are in a transaction
         // this will join existing Session/Transaction if they exist
         Session session = sessionFactory.getCurrentSession();
@@ -81,18 +83,33 @@
     
     /**
      * Release database session, rollback any uncommitted changes.
+     *
+     * IMPORTANT: we don't want to open a transaction and force the use of a
+     * jdbc connection just to close the session and do a rollback, so this
+     * method must be sensitive about how the release is triggered.
+     *
+     * In particular we don't want to use our custom getSession() method which
+     * automatically begins a transaction.  Instead we get a Session and check
+     * if there is already an active transaction that needs to be rolled back.
+     * If not then we can close the Session without ever getting a jdbc
+     * connection, which is important for scalability.
      */
     protected void release() {
         
         try {
-            Session session = getSession();
+            Session session = sessionFactory.getCurrentSession();
             
             if(session != null && session.isOpen()) {
                 
                 log.debug("Closing Hibernate Session");
                 
                 try {
-                    session.getTransaction().rollback();
+                    Transaction tx = session.getTransaction();
+                    
+                    if(tx != null && tx.isActive()) {
+                        log.debug("Forcing rollback on active transaction");
+                        tx.rollback();
+                    }
                 } catch(Throwable t) {
                     log.error("ERROR doing Hibernate rollback", t);
                 } finally {


Reply via email to