Spring #Transactional breaking application for embedded and standalone MongoDB - java

I am using Spring Data MongoDB 2.2.1.RELEASE for MongoDB access. And flapdoodle embed mongo 2.2.0 as embedded MongoDB for testing. This setup works fine. But recently, I have added support for Spring transactions. And since, MongoDB supports transactions only on replica sets. I have created a replica set locally on my machine and tested transaction scenarios. All good till now. But now when I run my unit tests, #Transactional annotation added to service methods is breaking the application with below error since the embedded MongoDB is not a replica set.
com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected
My question is how to configure my application so the #Transactional feature does not break my application when using embedded or standalone MongoDB?
Suggestions much appreciated. Thanks !!

It is possible to run a 1-node replica set. You may consider this especially in tests so that your test environment more closely resembles your production one.

Related

Where to deploy Spring boot application with H2 database

I am working on a web application which will serve as an SQL injection learning platform with multiple levels in spring boot and react. I am using PostreSQL as my main database, but I also wanted a lightweight solution for a vulnerable database on which the injections will occure. I was going to choose H2, but it isn't supported on Heroku where I wanted to deploy my application, as I worked with it in the past and it's already familiar.
So the question is: What other platform could I use to work with H2 or should I choose something else instead of H2, and if yes, what?
The way I handle this is to use spring-bean "profiles"
default
or
!default
I use H2 all the time for local-development mode (using 'default' spring bean profile to achieve this).
Now, we do not over-use spring-profiles. We have default and !default. And that is is.
All my JPA stuff works with H2.

How to implement spring boot Redis multi tenancy?

I am facing issues while creating a Multitenant application using spring boot and Redis. I have used the same solution which is mentioned in How to implement multitenancy for Redis in spring boot. But it didn't work. Is there a different way to implement Redis multitenancy with Spring boot.
I found the solution by adding a check if the data source not available in the cache then pull from the required source and create a connection. Also changed calling afterPropertiesSet() explicitly which is mentioned in How to implement multitenancy for redis in spring boot

Spring Data Elastic Search vs Java High Level REST Client

I'm new to Elastic search. We are building a Spring boot application with Elastic search.
For integrating my Spring boot application either we can use elasticsearch-rest-high-level-client or spring-boot-starter-data-elasticsearch.
Can anyone please elaborate on which option would be overall better and why?
spring-boot-starter-data-elasticsearch internally can use the transport(soon to be deprecated in ES 8.X) or rest-high-level-client Please refer elasticsearch client section for more information and how to configure them.
And from the same link :
Spring data Elasticsearch operates upon an Elasticsearch client that
is connected to a single Elasticsearch node or a cluster. Although the
Elasticsearch Client can be used to work with the cluster,
applications using Spring Data Elasticsearch normally use the higher
level abstractions of Elasticsearch Operations and Elasticsearch
Repositories.
Bottom line is that you can directly use rest-high-level client in your spring boot application but if you want more abstraction then you can use the spring-boot-starter-data-elasticsearch dependency and use its method which provides more abstraction although internally it would use the client offered by Elasticsearch.

Testing configuration of ReactiveMongoTemplate with java

I am using ReactiveMongoTemplate in a java/spring application to connect to a database. How would I go about testing whether the connection between the application and the database is actually configured properly so that data can be inserted into the database?
Is there also a way to tell which further steps I would need to take if the connection hasn't been fully established?
You can start a MongoDB instance during your application build and allow the tests to validate the connection logic. To make the setup portable you can either use emebdded MongoDB or Docker with testcontainers.
Do note that if you have a standalone MongoDB server in different environments (e.g. PROD and TEST) with different connection configuration you can't really prove that it works until you try it out. If there is an error in PROD configuration (e.g. TEST username used instead of PROD username) you will only see when the application runs on PROD. This can be fixed by using Kubernetes and immutable deployments.

How to use Cassandra in the Dropwizard

I tried to migrate the springboot application to the Dropwizard. However, I now have a problem that there is no framework in the Dropwizard that supports working with Cassandra databases, such as CRUD operations. Do any of you have any good suggestions?

Categories