Author: hlship
Date: Thu Jan 24 13:59:14 2008
New Revision: 615043

URL: http://svn.apache.org/viewvc?rev=615043&view=rev
Log:
TAPESTRY-1850: Hibernate Sessions are not being closed at the end of the request

Modified:
    
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
    
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/HibernateSessionManagerImpl.java
    
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/conf.apt

Modified: 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java?rev=615043&r1=615042&r2=615043&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
 Thu Jan 24 13:59:14 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 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.
@@ -14,11 +14,16 @@
 
 package org.apache.tapestry.hibernate;
 
-import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 
-/** Modifies the Hibernate configuration in some way before the [EMAIL 
PROTECTED] SessionFactory} is created.
+/**
+ * Defines the interface for a chain-of-command that updates Hibernate 
configuration in some way before the [EMAIL PROTECTED]
+ * org.hibernate.SessionFactory} is created.
  */
-public interface HibernateConfigurer {
-       void configure(Configuration configuration);
+public interface HibernateConfigurer
+{
+    /**
+     * Passed the configuration so as to make changes.
+     */
+    void configure(Configuration configuration);
 }

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=615043&r1=615042&r2=615043&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
 Thu Jan 24 13:59:14 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 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.
@@ -14,11 +14,6 @@
 
 package org.apache.tapestry.hibernate;
 
-import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
-
-import java.util.Collection;
-import java.util.List;
-
 import org.apache.tapestry.internal.InternalConstants;
 import org.apache.tapestry.internal.hibernate.DefaultHibernateConfigurer;
 import org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl;
@@ -26,6 +21,7 @@
 import org.apache.tapestry.internal.hibernate.PackageNameHibernateConfigurer;
 import org.apache.tapestry.internal.services.ClassNameLocator;
 import org.apache.tapestry.ioc.Configuration;
+import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
 import org.apache.tapestry.ioc.OrderedConfiguration;
 import org.apache.tapestry.ioc.annotations.Inject;
 import org.apache.tapestry.ioc.annotations.InjectService;
@@ -38,37 +34,42 @@
 import org.hibernate.Transaction;
 import org.slf4j.Logger;
 
+import java.util.Collection;
+import java.util.List;
+
 public class HibernateModule
 {
 
-    public static HibernateEntityPackageManager build(final Collection<String> 
packageNames) {
-       return new HibernateEntityPackageManager() {
-                       public Collection<String> getPackageNames() {
-                               return packageNames;
-                       }
-       };
+    public static HibernateEntityPackageManager build(final Collection<String> 
packageNames)
+    {
+        return new HibernateEntityPackageManager()
+        {
+            public Collection<String> getPackageNames()
+            {
+                return packageNames;
+            }
+        };
     }
-    
+
     /**
-     * Contributes the package "&lt;root&gt;.entities" to the configuration, 
so that it will be
-     * scanned for annotated entity classes.
+     * Contributes the package "<em>root-package</em>.entities" to the 
configuration, so that it will be scanned for
+     * annotated entity classes.
      */
     public static void 
contributeHibernateEntityPackageManager(Configuration<String> configuration,
 
-    @Inject
-    @Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM)
-    String appRootPackage)
+                                                               @Inject
+                                                               
@Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM)
+                                                               String 
appRootPackage)
     {
         configuration.add(appRootPackage + ".entities");
     }
 
     /**
-     * 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] Transaction} is created
+     * initially, and is committed at the end of the request.
      */
     @Scope(PERTHREAD_SCOPE)
-    public static HibernateSessionManager build(HibernateSessionSource 
sessionSource,
-            ThreadCleanupHub threadCleanupHub)
+    public static HibernateSessionManager build(HibernateSessionSource 
sessionSource, ThreadCleanupHub threadCleanupHub)
     {
         HibernateSessionManagerImpl service = new 
HibernateSessionManagerImpl(sessionSource);
 
@@ -77,52 +78,45 @@
         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);
     }
 
     /**
-     * Contributes the [EMAIL PROTECTED] #build(HibernateSessionManager, 
PropertyShadowBuilder) Session}
-     * service.
+     * Contributes the [EMAIL PROTECTED] #build(HibernateSessionManager, 
PropertyShadowBuilder) Session} service.
      */
     public static void contributeAlias(Configuration<AliasContribution> 
configuration,
 
-    @InjectService("Session")
-    Session session)
+                                       @InjectService("Session")
+                                       Session session)
     {
         configuration.add(AliasContribution.create(Session.class, session));
     }
-    
-    public static HibernateSessionSource build(Logger log, 
List<HibernateConfigurer> config) {
-       return new HibernateSessionSourceImpl(log, config);
-    }
-    
-    /** Adds the following configurers:
-     * <ul>
-     * <li>Default - performs default hibernate configuration</li>
-     * <li>PackageName - loads entities by package name</li>
-     * </ul>
+
+    public static HibernateSessionSource build(Logger log, 
List<HibernateConfigurer> config)
+    {
+        return new HibernateSessionSourceImpl(log, config);
+    }
+
+    /**
+     * 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>
      */
     public static void 
contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer> 
config,
-               final ClassNameLocator classNameLocator,
-               final HibernateEntityPackageManager packageManager) 
+                                                        final ClassNameLocator 
classNameLocator,
+                                                        final 
HibernateEntityPackageManager packageManager)
     {
-       config.add("Default", new DefaultHibernateConfigurer());        
-       config.add("PackageName", new 
PackageNameHibernateConfigurer(packageManager, classNameLocator));
+        config.add("Default", new DefaultHibernateConfigurer());
+        config.add("PackageName", new 
PackageNameHibernateConfigurer(packageManager, classNameLocator));
     }
-    
+
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionManagerImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionManagerImpl.java?rev=615043&r1=615042&r2=615043&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionManagerImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionManagerImpl.java
 Thu Jan 24 13:59:14 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 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.
@@ -50,9 +50,15 @@
         return _session;
     }
 
+    /**
+     * Commits the transaction at the end of the request, then closes the 
session. The transaction commit should largely
+     * be a no-op, since it's expected that [EMAIL PROTECTED] #commit()} will 
have been invoked after any real changes.
+     */
     public void threadDidCleanup()
     {
         _transaction.commit();
+
+        _session.close();
     }
 
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java?rev=615043&r1=615042&r2=615043&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
 Thu Jan 24 13:59:14 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 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.
@@ -21,23 +21,28 @@
 import org.hibernate.cfg.AnnotationConfiguration;
 import org.hibernate.cfg.Configuration;
 
-/** Adds entity classes from a given set of packages to the configuration.
+/**
+ * Adds entity classes from a given set of packages to the configuration.
  */
-public final class PackageNameHibernateConfigurer implements 
HibernateConfigurer {
-       private final HibernateEntityPackageManager _packageManager;
-       private final ClassNameLocator _classNameLocator;
-       
-       public PackageNameHibernateConfigurer(HibernateEntityPackageManager 
packageManager, ClassNameLocator classNameLocator) {
-               super();
-               _packageManager = packageManager;
-               _classNameLocator = classNameLocator;
-       }
-
-       public void configure(Configuration configuration) {
-               Defense.cast(configuration, AnnotationConfiguration.class, 
"configuration");
-               AnnotationConfiguration cfg = 
(AnnotationConfiguration)configuration;
+public final class PackageNameHibernateConfigurer implements 
HibernateConfigurer
+{
+    private final HibernateEntityPackageManager _packageManager;
+
+    private final ClassNameLocator _classNameLocator;
+
+    public PackageNameHibernateConfigurer(HibernateEntityPackageManager 
packageManager,
+                                          ClassNameLocator classNameLocator)
+    {
+        _packageManager = packageManager;
+        _classNameLocator = classNameLocator;
+    }
+
+    public void configure(Configuration configuration)
+    {
+        Defense.cast(configuration, AnnotationConfiguration.class, 
"configuration");
+        AnnotationConfiguration cfg = (AnnotationConfiguration) configuration;
 
-               ClassLoader contextClassLoader = 
Thread.currentThread().getContextClassLoader();
+        ClassLoader contextClassLoader = 
Thread.currentThread().getContextClassLoader();
 
         for (String packageName : _packageManager.getPackageNames())
         {
@@ -48,6 +53,7 @@
                 try
                 {
                     Class entityClass = 
contextClassLoader.loadClass(className);
+
                     cfg.addAnnotatedClass(entityClass);
                 }
                 catch (ClassNotFoundException ex)
@@ -56,6 +62,6 @@
                 }
             }
         }
-       }
+    }
 
 }

Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/conf.apt
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/conf.apt?rev=615043&r1=615042&r2=615043&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/conf.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/conf.apt Thu Jan 
24 13:59:14 2008
@@ -35,7 +35,7 @@
   
   By default, the package <application-root-package>.entities is scanned as 
described above. If you have additional packages containing 
   entities, you must 
-  
{{{http://tapestry.apache.org/tapestry5/tapestry-ioc/configuration.html}contribute}}
 them to the tapestry.hibernate.HibernateEntityPackageManager 
+  {{{../tapestry-ioc/configuration.html}contribute}} them to the 
tapestry.hibernate.HibernateEntityPackageManager 
   service configuration.
   
   Example:


Reply via email to