Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=61366
Add a new attribute, localDataSource, to the JDBCStore that allows the Store to be configured to use a DataSource defined by the web application rather than the default of using a globally defined DataSource.
Patch provided by Jonathan Horowitz.
This closes #71

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1803828 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/java/org/apache/catalina/session/JDBCStore.java b/java/org/apache/catalina/session/JDBCStore.java
index 0caa293..6b8cec6 100644
--- a/java/org/apache/catalina/session/JDBCStore.java
+++ b/java/org/apache/catalina/session/JDBCStore.java
@@ -108,6 +108,11 @@
     protected String dataSourceName = null;
 
     /**
+     * Context local datasource.
+     */
+    private boolean localDataSource = false;
+
+    /**
      * DataSource to use
      */
     protected DataSource dataSource = null;
@@ -455,6 +460,23 @@
         return this.dataSourceName;
     }
 
+    /**
+     * @return if the datasource will be looked up in the webapp JNDI Context.
+     */
+    public boolean getLocalDataSource() {
+        return localDataSource;
+    }
+
+    /**
+     * Set to {@code true} to cause the datasource to be looked up in the webapp
+     * JNDI Context.
+     *
+     * @param localDataSource the new flag value
+     */
+    public void setLocalDataSource(boolean localDataSource) {
+      this.localDataSource = localDataSource;
+    }
+
 
     // --------------------------------------------------------- Public Methods
 
@@ -868,16 +890,26 @@
             return dbConnection;
 
         if (dataSourceName != null && dataSource == null) {
+            org.apache.catalina.Context context = getManager().getContext();
+            ClassLoader oldThreadContextCL = null;
+            if (localDataSource) {
+                oldThreadContextCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
+            }
+
             Context initCtx;
             try {
                 initCtx = new InitialContext();
                 Context envCtx = (Context) initCtx.lookup("java:comp/env");
                 this.dataSource = (DataSource) envCtx.lookup(this.dataSourceName);
             } catch (NamingException e) {
-                manager.getContext().getLogger().error(
+                context.getLogger().error(
                         sm.getString(getStoreName() + ".wrongDataSource",
                                 this.dataSourceName), e);
-           }
+            } finally {
+                if (localDataSource) {
+                    context.unbind(Globals.IS_SECURITY_ENABLED, oldThreadContextCL);
+                }
+            }
         }
 
         if (dataSource != null) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 20e774b..870e249 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -66,6 +66,13 @@
         web.xml and similar locations that may legitimately contain characters
         that are not permitted by RFC 3986. (markt)
       </fix>
+      <add>
+        <bug>61366</bug>: Add a new attribute, <code>localDataSource</code>, to
+        the <code>JDBCStore</code> that allows the Store to be configured to use
+        a DataSource defined by the web application rather than the default of
+        using a globally defined DataSource. Patch provided by Jonathan
+        Horowitz. (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">
diff --git a/webapps/docs/config/manager.xml b/webapps/docs/config/manager.xml
index 9fad3fb..89e60f8 100644
--- a/webapps/docs/config/manager.xml
+++ b/webapps/docs/config/manager.xml
@@ -422,6 +422,12 @@
       <p>Java class name of the JDBC driver to be used.</p>
     </attribute>
 
+    <attribute name="localDataSource" required="false">
+      <p>This allows the Store to use a DataSource defined for the Context
+      rather than a global DataSource. If not specified, the default is
+      <code>false</code>: use a global DataSource.</p>
+    </attribute>
+
     <attribute name="sessionAppCol" required="false">
       <p>Name of the database column, contained in the specified session table,
       that contains the Engine, Host, and Web Application Context name in the