Author: hlship
Date: Fri Feb 29 15:20:42 2008
New Revision: 632492

URL: http://svn.apache.org/viewvc?rev=632492&view=rev
Log:
TAPESTRY-1955: Hibernate SessionFactory close on webapp destroy

Modified:
    
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
    
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java

Modified: 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java?rev=632492&r1=632491&r2=632492&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
 Fri Feb 29 15:20:42 2008
@@ -1,4 +1,4 @@
-// Copyright 2007, 2008 The Apache Software Foundation
+// Copyright 2007 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -29,9 +29,9 @@
 import org.apache.tapestry.ioc.services.ClassNameLocator;
 import org.apache.tapestry.ioc.services.PerthreadManager;
 import org.apache.tapestry.ioc.services.PropertyShadowBuilder;
+import org.apache.tapestry.ioc.services.RegistryShutdownHub;
 import org.apache.tapestry.services.AliasContribution;
 import org.hibernate.Session;
-import org.hibernate.Transaction;
 import org.slf4j.Logger;
 
 import java.util.Collection;
@@ -52,8 +52,8 @@
     }
 
     /**
-     * Contributes the package "<em>root-package</em>.entities" to the 
configuration, so that it will be scanned for
-     * annotated entity classes.
+     * Contributes the package "&lt;root&gt;.entities" to the configuration, 
so that it will be scanned for annotated
+     * entity classes.
      */
     public static void 
contributeHibernateEntityPackageManager(Configuration<String> configuration,
 
@@ -65,8 +65,8 @@
     }
 
     /**
-     * The session manager manages sessions on a per-thread/per-request basis. 
A [EMAIL PROTECTED] Transaction} is created
-     * initially, and is committed at the end of the request.
+     * The session manager manages sessions on a per-thread/per-request basis. 
A [EMAIL PROTECTED] org.hibernate.Transaction} is
+     * created initially, and is committed at the end of the request.
      */
     @Scope(PERTHREAD_SCOPE)
     public static HibernateSessionManager build(HibernateSessionSource 
sessionSource, PerthreadManager perthreadManager)
@@ -78,15 +78,20 @@
         return service;
     }
 
-    public static Session build(HibernateSessionManager sessionManager, 
PropertyShadowBuilder propertyShadowBuilder)
+    public static Session build(HibernateSessionManager sessionManager,
+                                PropertyShadowBuilder propertyShadowBuilder)
     {
         // Here's the thing: the tapestry.hibernate.Session class doesn't have 
to be per-thread,
-        // since it will invoke getSession() on the HibernateSessionManager 
service (which is per-thread).
-        // On first invocation per request,
+        // since
+        // it will invoke getSession() on the HibernateSessionManager service 
(which is per-thread).
+        // On
+        // first invocation per request,
         // this forces the HSM into existence (which creates the session and 
begins the
-        // transaction). Thus we don't actually create
+        // transaction).
+        // Thus we don't actually create
         // a session until we first try to access it, then the session 
continues to exist for the
-        // rest of the request.
+        // rest
+        // of the request.
 
         return propertyShadowBuilder.build(sessionManager, "session", 
Session.class);
     }
@@ -102,14 +107,19 @@
         configuration.add(AliasContribution.create(Session.class, session));
     }
 
-    public static HibernateSessionSource build(Logger log, 
List<HibernateConfigurer> config)
+    public static HibernateSessionSource build(Logger logger, 
List<HibernateConfigurer> config,
+                                               RegistryShutdownHub hub)
     {
-        return new HibernateSessionSourceImpl(log, config);
+        HibernateSessionSourceImpl hss = new 
HibernateSessionSourceImpl(logger, config);
+
+        hub.addRegistryShutdownListener(hss);
+
+        return hss;
     }
 
     /**
-     * Adds the following configurers: <dl> <dt>Default</dt> <dd>Performs 
default hibernate configuration</dd>
-     * <dt>PackageName</dt> <dd>Loads entities by package name</dd> </ul>
+     * Adds the following configurers: <ul> <li>Default - performs default 
hibernate configuration</li> <li>PackageName
+     * - loads entities by package name</li> </ul>
      */
     public static void 
contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer> 
config,
                                                         final ClassNameLocator 
classNameLocator,

Modified: 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java?rev=632492&r1=632491&r2=632492&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
 Fri Feb 29 15:20:42 2008
@@ -14,17 +14,18 @@
 
 package org.apache.tapestry.internal.hibernate;
 
-import java.util.List;
-
 import org.apache.tapestry.hibernate.HibernateConfigurer;
 import org.apache.tapestry.hibernate.HibernateSessionSource;
+import org.apache.tapestry.ioc.services.RegistryShutdownListener;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.AnnotationConfiguration;
 import org.hibernate.cfg.Configuration;
 import org.slf4j.Logger;
 
-public class HibernateSessionSourceImpl implements HibernateSessionSource
+import java.util.List;
+
+public class HibernateSessionSourceImpl implements HibernateSessionSource, 
RegistryShutdownListener
 {
     private final SessionFactory _sessionFactory;
     private final Configuration _configuration;
@@ -35,8 +36,8 @@
 
         Configuration configuration = new AnnotationConfiguration();
 
-        for(HibernateConfigurer configurer : hibernateConfigurers)
-               configurer.configure(configuration);
+        for (HibernateConfigurer configurer : hibernateConfigurers)
+            configurer.configure(configuration);
 
         long configurationComplete = System.currentTimeMillis();
 
@@ -65,9 +66,14 @@
         return _sessionFactory;
     }
 
-       public Configuration getConfiguration() {
-               return _configuration;
-       }
-    
-    
+    public Configuration getConfiguration()
+    {
+        return _configuration;
+    }
+
+    public void registryDidShutdown()
+    {
+        _sessionFactory.close();
+    }
+
 }


Reply via email to