I'm currently working on a simple demo application to show example CRUD with MongoDB Hibernate OGM and Spring-Boot. I have a local MongoDB with 2 users.
use admin
db.createUser(
{
user: "useradmin",
pwd: "password,
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
use TestDB
db.createUser(
{
user: "test",
pwd: "test123",
roles: [ { role: "readWrite", db: "TestDB" } ]
}
)
My persistence.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ogm-mongodb"
transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="MONGODB" />
<property name="hibernate.ogm.datastore.database" value="TestDB" />
<property name="hibernate.ogm.datastore.create_database" value="true" />
<property name="hibernate.ogm.datastore.host" value="127.0.0.1" />
<property name="hibernate.ogm.datastore.port" value="27017" />
<property name="hibernate.ogm.datastore.username" value="useradmin"/>
<property name="hibernate.ogm.datastore.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
If I start my spring-Boot application and trigger the endpoint to write test data to the MongoDB i get an exception because the useradmin doesn't have read or write access to the TestDB. So far so good.
But if I change the username
and password
property in the persistence.xml
to test
and test123
I get an exception on startup of the spring app:
Unable to start datastore provider
Cause:
Unable to connect to MongoDB instance: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='test', source='admin', password=<hidden>, mechanismProperties={}}
I suspect the application tries to authenticate against admin
and not the TestDB
My pom.xml
dependencies are here:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.12.10</version>
</dependency>
<dependency>
<groupId>org.jboss.narayana.jta</groupId>
<artifactId>narayana-jta</artifactId>
<version>5.12.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.4</version>
<scope>test</scope>
</dependency>
The Error originates in my ExampleService
class according to the stacktrace
@Service
public class ExampleService {
EntityManagerFactory emf;
EntityManager em;
TransactionManager tm;
public ExampleService() {
emf = Persistence.createEntityManagerFactory("ogm-mongodb"); //Error here
em = emf.createEntityManager();
tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
}
...
Do I have an Error in my configuration or persistence.xml
? Thanks for any help in advance