[Java] MongoClient via JNDI - ClassCastException

482 views
Skip to first unread message

Jens Heider

unread,
Jun 22, 2017, 8:50:17 AM6/22/17
to mongodb-user
I've tried to inject the MongoClient via JNDI into my Java class like described here: http://mongodb.github.io/mongo-java-driver/3.3/driver/reference/connecting/jndi/

@Resource(name = "java:global/MongoClient")
private MongoClient mongoClient;

Unfortunately,  i'm getting a
java.lang.IllegalArgumentException: Can not set com.mongodb.MongoClient field [...] to com.mongodb.MongoClient

For me the classes seems to be identically. I'm using the same mongodb java driver version in wildfly module as well as in my application.
When changing the field to be just an `Object`, the injection is working, but then I'm getting a
java.lang.ClassCastException: com.mongodb.MongoClient cannot be cast to com.mongodb.MongoClient

Could anybody give me a hint, what might be the problem?

Thanks in advance :)

Abhijit Das

unread,
Jul 6, 2017, 8:23:17 AM7/6/17
to mongodb-user
Hi Jens,

Did u managed to solve this issue?

Thanks in advance.

Jeff Yemin

unread,
Jul 10, 2017, 9:35:38 AM7/10/17
to mongodb-user
This look like a classpath issue.  If com.mongodb.MongoClient can't be cast to com.mongodb.MongoClient then there there must be two instances of that class in two different class loaders. The JVM spec requires that those are treated as different classes.

One way to debug this is to enable verbose class loading (java -verbose:class) and determine where MongoClient is being loaded from.  Then make sure the driver jar file is only available from one location.


Regards,
Jeff

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/b9d57c56-481b-40e0-8f3e-250804277007%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Quentin Tirroloni

unread,
Jul 20, 2017, 6:23:43 PM7/20/17
to mongodb-user
Thanks a lot for your clues !!!
I had the same issue

java.lang.IllegalArgumentException: Can not set com.mongodb.MongoClient field [...] to com.mongodb.MongoClient

I was using MongoDB Driver (The driver includes a JNDI ObjectFactory implementation, MongoClientFactory, that returns MongoClient instances based on a connection string.)
http://mongodb.github.io/mongo-java-driver/3.3/driver/reference/connecting/jndi/

My problem was that I didn't used in a good way the module...


In my ear pom.xml we have to add the module in the manifest entry
<plugin>
               
<groupId>org.apache.maven.plugins</groupId>
               
<artifactId>maven-ear-plugin</artifactId>
               
<configuration>
                   
<filtering>true</filtering>
                   
<filters />
                   
<archive>
                       
<manifestEntries>
                               
<Dependencies>org.mongodb,org.slf4j</Dependencies>
                       
</manifestEntries>
                   
</archive>
               
</configuration>
</plugin>


Then in the Java Code, we have to use the following annotation


@Resource(name = "java:global/MongoClient")
private MongoClient mongoClient;

Quentin
Reply all
Reply to author
Forward
0 new messages