This is an automated email from the ASF dual-hosted git repository. remm pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push: new 1d4f733fe4 Allow avoiding using a comp/env prefix on the datasource 1d4f733fe4 is described below commit 1d4f733fe458dbf95c86444d32e542ae3795c68f 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 | 49 +++++++++++++++------- webapps/docs/changelog.xml | 5 +++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/java/org/apache/catalina/session/DataSourceStore.java b/java/org/apache/catalina/session/DataSourceStore.java index 5384f324ca..d245c7c838 100644 --- a/java/org/apache/catalina/session/DataSourceStore.java +++ b/java/org/apache/catalina/session/DataSourceStore.java @@ -38,6 +38,8 @@ 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; @@ -542,9 +544,7 @@ public class DataSourceStore extends StoreBase { } 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(); @@ -620,23 +620,40 @@ public class DataSourceStore extends StoreBase { 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("dataSourceStore.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 9729bc7fd2..cd2a227490 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -207,6 +207,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