[JAVA-4956] MongoClientFactory creates MongoClientImpl, not MongoClient (using JNDI) Created: 02/May/23 Updated: 06/Jul/23 Resolved: 06/Jul/23 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | Configuration |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | Jason McLaren | Assignee: | Jeffrey Yemin |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Documentation Changes Summary: | 1. What would you like to communicate to the user about this feature? |
| Description |
SummaryI'm attempting to create com.mongodb.client.MongoClient instances using JNDI. The com.mongodb.client.MongoClientFactory is creating com.mongodb.client.internal.MongoClientImpl instances instead. This used to work with the legacy driver (using com.mongodb.MongoClient and com.mongodb.client.jndi.MongoClientFactory). Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).MongoDB Java driver 4.9.1 (also tried 4.3.4 and 4.6.1) MongoDB 4.4 (Atlas hosted by mongodb.com), replica set Tomcat 9.0.73 Jersey 1.19.3 Java 8 How to ReproduceMy server.xml contains the following:
My context.xml contains the following:
I copy the following files to the Tomcat global lib directory before loading the application:
In my Java application, I use the following code to obtain a MongoClient:
When I load the application in Tomcat, everything seems fine, but when I send an http request to the server, I get the following error in the Tomcat logs:
Additional BackgroundUsing this code to load the MongoClient via JNDI worked with mongo-java-driver version 3.12.8 and MongoDB 4.2, using the `com.mongodb.client.jndi.MongoClientFactory` resource factory to create the `com.mongodb.client.MongoClient` resource. I have guessed at some of the configuration above, because the JNDI documentation appears to be out of date as of driver version 4.3 (the oldest version still online). I filed a separate bug about that here: https://jira.mongodb.org/browse/DOCS-16093 I tried a number of variations but couldn't get it to work. If I'm using it incorrectly, I would be happy to be corrected. |
| Comments |
| Comment by Tom Selander [ 06/Jul/23 ] |
|
jmclaren@legendpower.com Let's close it out for now, and when this comes back up feel free to file another ticket later in the year. If you make note of this ticket number, you can reference this ticket when it comes time to refile. |
| Comment by Jason McLaren [ 29/Jun/23 ] |
|
I still need to solve this problem, but it may get pushed out to later this year, as I have some more urgent priorities at work. Would it be better to close this ticket and re-open a new one later when I have a test case, or to leave it open? |
| Comment by Tom Selander [ 26/Jun/23 ] |
|
jmclaren@legendpower.com Hey Jason, have you had a chance to look into this at all? Please advise whether this is something that you still plan on looking at right now to get a reproducible example, or if we can close this out? |
| Comment by Jason McLaren [ 31/May/23 ] |
|
Just responding to the bot to keep this alive. I will look into why there might be two different class loaders. I'll also see if I can remove enough code to produce a minimal reproducible example. |
| Comment by PM Bot [ 30/May/23 ] |
|
Hi jmclaren@legendpower.com! If this is still an issue for you, please open Jira to review the latest status and provide your feedback. Thanks! |
| Comment by Jeffrey Yemin [ 22/May/23 ] |
|
jmclaren@legendpower.com I'm moving this back to Waiting for Reporter, as I don't see a path forward for resolving this without a full reproduction scenario. |
| Comment by Jeffrey Yemin [ 10/May/23 ] |
|
jmclaren@legendpower.com sorry for the SPAM. I'm not sure what we can do about this, as it looks from the source code of ResourceLinkFactory that this should work as designed. I suspect that the issue is that ResourceLinkFactory uses a different ClassLoader than MongoClientFactory, and that's why the check !expectedClazz.isAssignableFrom(result.getClass()) is returning false (since a class loaded by one class loader is not necessarily assignable from a class loaded by a different class loader). I'm not, however, sure how you would test this hypothesis in the context of your application. |
| Comment by Jason McLaren [ 10/May/23 ] |
|
What additional details are needed? |
| Comment by PM Bot [ 10/May/23 ] |
|
Hey jmclaren@legendpower.com, We need additional details to investigate the problem. If this is still an issue for you, please provide the requested information. |
| Comment by Jeffrey Yemin [ 02/May/23 ] |
|
I'm not all that familiar with Tomcat but one thing I can clarify is that com.mongodb.client.internal.MongoClientImpl is an internal class that implements the com.mongodb.client.MongoClient interface, so I would expect this to work. The legacy driver probably works because com.mongodb.MongoClient is a class, not an interface. I looked at the Tomcat documentation for GlobalNamingResource, and saw that the example uses the type javax.sql.DataSource, which is also an interface, so it doesn't seem likely that interfaces are not supported at all. And here's a link to the source code: https://github.com/apache/tomcat/blob/9.0.73/java/org/apache/naming/factory/ResourceLinkFactory.java#L155-L160. It looks like it is doing the right thing, so it's not clear why Class#isAssignableFrom would return false. The only thing I can think of is some sort of ClassLoader issue |
| Comment by Service Account: DBX TPM [ 02/May/23 ] |
|
Hi jmclaren@legendpower.com, thank you for reporting this issue! The team will look into it and get back to you soon. |