You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/04/13 01:09:44 UTC

svn commit: r393645 - /incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java

Author: agilliland
Date: Wed Apr 12 16:09:43 2006
New Revision: 393645

URL: http://svn.apache.org/viewcvs?rev=393645&view=rev
Log:
adding methods for storing or removing collections of objects.
make XXXAndCommit() methods catch Throwables rather than just HibernateExceptions.


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=393645&r1=393644&r2=393645&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 12 16:09:43 2006
@@ -3,6 +3,7 @@
  */
 package org.roller.business.hibernate;
 
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -11,7 +12,6 @@
 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;
@@ -87,7 +87,7 @@
                 
                 log.debug("Closing open hibernate session");
                 
-                // TODO BACKEND: should we do a rollback() first?
+                // TODO BACKEND: should we do a rollback()?
                 //session.getTransaction().rollback();
                 
                 session.close();
@@ -161,11 +161,56 @@
             
             // commit changes
             getSession().getTransaction().commit();
-        } catch (HibernateException ex) {
+        } catch (Throwable ex) {
             
             try {
                 getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    
+    /**
+     * Store a collection of objects.
+     */
+    protected void store(Collection objects) throws RollerException {
+        
+        Object obj = null;
+        Iterator items = objects.iterator();
+        while(items.hasNext()) {
+            obj = items.next();
+            
+            this.store((PersistentObject) obj);
+        }
+    }
+    
+    
+    /**
+     * Store a collection of objects and then commit.
+     * 
+     * If the commit fails then we rollback and release the session.
+     */
+    protected void storeAndCommit(Collection objects) throws RollerException {
+        
+        try {
+            // begin transaction
+            getSession().beginTransaction();
+            
+            this.store(objects);
+            
+            // commit changes
+            getSession().getTransaction().commit();
+        } catch (Throwable ex) {
+            
+            try {
+                getSession().getTransaction().rollback();
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }
             
@@ -187,6 +232,7 @@
         
         Session session = getSession();
         
+        // TODO BACKEND: it seems really inefficient to load and then delete.
         PersistentObject obj = (PersistentObject) session.load(clazz,id);
         
         session.delete(obj);
@@ -194,7 +240,9 @@
     
     
     /**
-     * Remove object and commit.  If the commit fails then we rollback and release.
+     * Remove object and commit.
+     * 
+     * If the commit fails then we rollback and release.
      */
     protected void removeAndCommit(String id, Class clazz) throws RollerException {
         
@@ -210,11 +258,56 @@
             
             // commit changes
             getSession().getTransaction().commit();
-        } catch (HibernateException ex) {
+        } catch (Throwable ex) {
+            
+            try {
+                getSession().getTransaction().rollback();
+            } catch(Throwable he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    
+    /**
+     * Remove a collection of objects.
+     */
+    protected void remove(Collection ids, Class clazz) throws RollerException {
+        
+        String id = null;
+        Iterator items = ids.iterator();
+        while(items.hasNext()) {
+            id = (String) items.next();
+            
+            this.remove(id, clazz);
+        }
+    }
+    
+    
+    /**
+     * Remove a collection of objects and then commit.
+     * 
+     * If the commit fails then we rollback and release the session.
+     */
+    protected void removeAndCommit(Collection ids, Class clazz) throws RollerException {
+        
+        try {
+            // begin transaction
+            getSession().beginTransaction();
+            
+            this.remove(ids, clazz);
+            
+            // commit changes
+            getSession().getTransaction().commit();
+        } catch (Throwable ex) {
             
             try {
                 getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }
             
@@ -245,7 +338,9 @@
     
     
     /**
-     * Remove object and commit.  If the commit fails then we rollback and release.
+     * Remove object and commit.
+     * 
+     * If the commit fails then we rollback and release.
      */
     protected void removeAndCommit(PersistentObject obj) throws RollerException {
         
@@ -261,11 +356,56 @@
             
             // commit changes
             getSession().getTransaction().commit();
-        } catch (HibernateException ex) {
+        } catch (Throwable ex) {
+            
+            try {
+                getSession().getTransaction().rollback();
+            } catch(Throwable he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    
+    /**
+     * Remove a collection of objects.
+     */
+    protected void remove(Collection objects) throws RollerException {
+        
+        Object obj = null;
+        Iterator items = objects.iterator();
+        while(items.hasNext()) {
+            obj = items.next();
+            
+            this.remove((PersistentObject) obj);
+        }
+    }
+    
+    
+    /**
+     * Remove a collection of objects and then commit.
+     * 
+     * If the commit fails then we rollback and release the session.
+     */
+    protected void removeAndCommit(Collection objects) throws RollerException {
+        
+        try {
+            // begin transaction
+            getSession().beginTransaction();
+            
+            this.remove(objects);
+            
+            // commit changes
+            getSession().getTransaction().commit();
+        } catch (Throwable ex) {
             
             try {
                 getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }