a better session handling
[mir.git] / source / mir / core / service / storage / StorageService.java
index efa2e50..fb81d7f 100755 (executable)
@@ -45,24 +45,24 @@ import net.sf.hibernate.expression.Expression;
  * 
  * StorageService
  * @author idefix
- * @version $Id: StorageService.java,v 1.1 2003/08/17 19:11:49 idfx Exp $
+ * @version $Id: StorageService.java,v 1.2 2003/08/19 00:42:38 idfx Exp $
  */
 public abstract class StorageService {
        private Class objectClass;
-       private SessionFactory factory;
-       
+       private SessionHolder sessionHolder;
+
        public StorageService(Class objectClass, SessionFactory factory){
                this.objectClass = objectClass;
-               this.factory = factory;
+               sessionHolder = new SessionHolder(factory);
        }
-
+       
        public List list(int offset, int limit){
                return list(offset, limit, null);
        }
        
        public List list(int offset, int limit, Expression expression){
                try {
-                       Session session = factory.openSession();
+                       Session session = sessionHolder.currentSession();
                        Transaction transaction = session.beginTransaction();
                        Criteria criteria = session.createCriteria(objectClass);
                        if(expression != null){
@@ -73,20 +73,18 @@ public abstract class StorageService {
                                .setMaxResults(limit);
                        List returnList = criteria.list();
                        transaction.commit();
-                       session.close();
                        return returnList;
                } catch (HibernateException e) {
                        throw new StorageServiceFailure(e);
-               }
+               } 
        }
        
        public Object load(Integer id){
                try {
-                       Session session = factory.openSession();
+                       Session session = sessionHolder.currentSession();
                        Transaction transaction = session.beginTransaction();
                        Object returnObject = session.load(objectClass, id);
                        transaction.commit();
-                       session.close();
                        return returnObject;
                } catch (HibernateException e) {
                        throw new StorageServiceFailure(e);
@@ -95,10 +93,10 @@ public abstract class StorageService {
        
        public Integer add(Object newObject){
                try {
-                       Session session = factory.openSession();
+                       Session session = sessionHolder.currentSession();
                        Transaction transaction = session.beginTransaction();
                        Integer newid = (Integer)session.save(newObject);
-                       session.close();
+                       transaction.commit();
                        return newid;
                } catch (HibernateException e) {
                        throw new StorageServiceFailure(e);
@@ -107,10 +105,10 @@ public abstract class StorageService {
        
        public void update(Object toUpdate){
                try {
-                       Session session = factory.openSession();
+                       Session session = sessionHolder.currentSession();
                        Transaction transaction = session.beginTransaction();
                        session.update(toUpdate);
-                       session.close();
+                       transaction.commit();
                } catch (HibernateException e) {
                        throw new StorageServiceFailure(e);
                }                       
@@ -118,13 +116,17 @@ public abstract class StorageService {
        
        public void delete(Object toDelete){
                try {
-                       Session session = factory.openSession();
+                       Session session = sessionHolder.currentSession();
                        Transaction transaction = session.beginTransaction();
                        session.delete(toDelete);
-                       session.close();
+                       transaction.commit();
                } catch (HibernateException e) {
                        throw new StorageServiceFailure(e);
                }                       
        }
        
+       public void finalize() throws Throwable {
+               sessionHolder.closeSession();
+               super.finalize();       
+       }
 }