This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 589c8a8c29 Allow avoiding using a comp/env prefix on the datasource
589c8a8c29 is described below

commit 589c8a8c296a7f064aba35b8a2198d263bda9bdf
Author: remm <r...@apache.org>
AuthorDate: Tue Dec 3 11:14:42 2024 +0100

    Allow avoiding using a comp/env prefix on the datasource
    
    Use a direct lookup on the <GlobalNamingResources>.
---
 .../apache/catalina/session/DataSourceStore.java   | 50 +++++++++++++++-------
 webapps/docs/changelog.xml                         |  5 +++
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/java/org/apache/catalina/session/DataSourceStore.java 
b/java/org/apache/catalina/session/DataSourceStore.java
index 197debeb40..b1de1e6a75 100644
--- a/java/org/apache/catalina/session/DataSourceStore.java
+++ b/java/org/apache/catalina/session/DataSourceStore.java
@@ -36,7 +36,10 @@ import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import javax.sql.DataSource;
 
+import org.apache.catalina.Container;
 import org.apache.catalina.Globals;
+import org.apache.catalina.Server;
+import org.apache.catalina.Service;
 import org.apache.catalina.Session;
 import org.apache.juli.logging.Log;
 
@@ -284,9 +287,7 @@ public class DataSourceStore extends JDBCStore {
                 }
 
                 try {
-                    // If sessions already exist in DB, remove and insert 
again.
-                    // TODO:
-                    // * Check if ID exists in database and if so use UPDATE.
+                    // Remove session if it exists and insert again.
                     remove(session.getIdInternal(), _conn);
 
                     bos = new ByteArrayOutputStream();
@@ -339,23 +340,40 @@ public class DataSourceStore extends JDBCStore {
     protected Connection open() throws SQLException {
         if (dataSourceName != null && dataSource == null) {
             org.apache.catalina.Context context = getManager().getContext();
-            ClassLoader oldThreadContextCL = null;
             if (getLocalDataSource()) {
-                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) {
-                context.getLogger().error(sm.getString(getStoreName() + 
".wrongDataSource", this.dataSourceName), e);
-            } finally {
-                if (getLocalDataSource()) {
+                ClassLoader oldThreadContextCL = 
context.bind(Globals.IS_SECURITY_ENABLED, null);
+                try {
+                    Context envCtx = (Context) (new 
InitialContext()).lookup("java:comp/env");
+                    this.dataSource = (DataSource) 
envCtx.lookup(this.dataSourceName);
+                } catch (NamingException e) {
+                    
context.getLogger().error(sm.getString("dataSourceStore.wrongDataSource", 
this.dataSourceName), e);
+                } finally {
                     context.unbind(Globals.IS_SECURITY_ENABLED, 
oldThreadContextCL);
                 }
+            } else {
+                try {
+                    // This should be the normal way to lookup for the global 
in the global context (no comp/env)
+                    Service service = Container.getService((Container) 
context);
+                    if (service != null) {
+                        Server server = service.getServer();
+                        if (server != null) {
+                            Context namingContext = 
server.getGlobalNamingContext();
+                            this.dataSource = (DataSource) 
namingContext.lookup(dataSourceName);
+                        }
+                    }
+                } catch (NamingException e) {
+                    // Ignore, try another way for compatibility
+                }
+                if (this.dataSource == null) {
+                    try {
+                        Context envCtx = (Context) (new 
InitialContext()).lookup("java:comp/env");
+                        this.dataSource = (DataSource) 
envCtx.lookup(this.dataSourceName);
+                    } catch (NamingException e) {
+                        
context.getLogger().error(sm.getString("dataSourceStore.wrongDataSource", 
this.dataSourceName), e);
+                    }
+                }
             }
+
         }
 
         if (dataSource != null) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 84effc445c..90fcf214aa 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -203,6 +203,11 @@
         the default servlet. Provided by Chenjp in pull request <pr>778</pr>.
         (markt)
       </fix>
+      <fix>
+        Harmonize <code>DataSourceStore</code> lookup in the global resources
+        to optionally avoid the <code>comp/env</code> prefix which is usually
+        not used there. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to