This is an automated email from the ASF dual-hosted git repository.
remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 5d7745084c Allow avoiding using a comp/env prefix on the datasource
5d7745084c is described below
commit 5d7745084c8cdb082bf279b07be1617c782d544d
Author: remm <[email protected]>
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 3a0806cd79..7711ed453c 100644
--- a/java/org/apache/catalina/session/DataSourceStore.java
+++ b/java/org/apache/catalina/session/DataSourceStore.java
@@ -37,6 +37,8 @@ import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.catalina.Container;
+import org.apache.catalina.Server;
+import org.apache.catalina.Service;
import org.apache.catalina.Session;
import org.apache.juli.logging.Log;
@@ -541,9 +543,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();
@@ -619,23 +619,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(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(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(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 5a712fe7ea..6a1d6f7824 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -221,6 +221,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: [email protected]
For additional commands, e-mail: [email protected]