Thread Safety of EntityManager when dependency injection - spring

Basically EntityManager in JPA is not thread safe because of persistence context.
So we have to use EntityManager with EntityManagerFactory or #PersistenceContext annotation to make EntityManager per each thread.
Recently, I find something interesting.
When using EntityManager with constructor injection, it seems to be same mechanism with #PersistenceContext annotation.
Both of two mechanism uses same proxy bean, which is LocalContainerEntityManagerFactoryBean.
Dependency injection with constructor

Dependency injection with #PersistenceContext
Context
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/>
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
I have not found some official docs about this.
Only see some posts on blogs about this.
Here is my questions.
Is it thread-safe to use EntityManager by constructor injection?
Is there an official docs about shared EntityManager proxy or LocalContainerEntityManagerFactoryBean?
I make sure before using EntityManager through constructor injection.

Related

Using Spring-Actuator in combination with ObjectDB

I am implementing a program in Spring-Boot using ObjectDB. To actually use ObjectDB I have followed this approach https://www.objectdb.com/forum/2523 which is working perfectly.
However, as soon as I want to use Spring-Actuator then I am getting the following errors:
Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration': Injection of autowired dependencies failed; nested exception is com.objectdb.o.UserException: Unsupported unwrap(org.hibernate.SessionFactory.class) for EntityManagerFactory
Any ideas on how to solve this error?
I am using the exact same code as in the link. I have added Spring-Actuator in the pom like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
The failure is occurring because Spring Boot is trying to unwrap the JPA EntityManagerFactory to get Hibernate's SessionFactory. It's attempting to do this as you have Hibernate on the classpath (it's a dependency of spring-boot-starter-data-jpa). According to the JPA specification, unwrap should throw a PersistenceException "if the provider does not support the call". Spring Boot catches PersistenceException in case unwrap isn't supported. Unfortunately, ObjectDB is throwing com.objectdb.o.UserException which isn't spec compliant. I'd recommend raising this as an ObjectDB bug.
You can avoid the problem by excluding Hibernate from the classpath. This will stop Spring Boot from attempting to unwrap Hibernate's SessionFactory, preventing the ObjectDB bug from occurring. You can do so by adding an exclusion to the spring-boot-starter-data-jpa dependency in your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>

Jackson databinding issue with Maven

I'm facing an issue where I have a maven project with Java POJOs.
import lombok.Builder;
import lombok.Getter;
/**
* User DTO.
*/
#Builder
#Getter
public class UserDto {
private String username;
}
I'm have added this maven dependency to another Spring boot based project.
I'm getting the following exception during the application tests.
org.springframework.core.codec.CodecException: Type definition error:
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.models.UserDto (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
However, whenever I add the spring boot starter parent dependency to the maven project I see no errors and the application works as expected.
Note: Also, the project works by adding a default constructor to the pojo. But my question is why would it work with the Spring boot starter added?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Jackson Object Mapper not working when extended configuration provided but working when providing class level/field level annotations in Spring Boot

The below object mapper configuration is not working when I add jjwt security to spring boot application.
#Configuration
public class CustomObjectMapper extends ObjectMapper {
/**
* Default serial version id generated.
*/
private static final long serialVersionUID = 1L;
public CustomObjectMapper() {
this.setSerializationInclusion(Include.NON_EMPTY);
this.registerModule(new ThreeTenModule());
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
}
Security dependencies added here
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
where as the below Jackson annotations are working on class/field levels.
#JsonInclude(Include.NON_EMPTY)
Why the bean configured custom object mapper not been used for serialization & deserialization? Any other libraries configured object mapper overriding my custom mapper?
After a long investigation, i have noticed #EnableWebMvc annotated configuration bean available in one dependent library. And got to know from here that #EnableWebMvc disables Spring Boot's MVC auto-configuration, thus giving complete control to provide customer MVC configuration. HTTP Message Convertors will also be included in Spring MVC component which in turn disables my custom jackson object mapper configuration.
PS: As jjwt imports jackson databind dependency by default, it fell in my suspect list. Feel good that i could RCA. Thanks.

spring core version updation in spring boot parent starter 1.5.10.RELEASE

Here is the maven dependency I have
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
what I'm trying to achieve is, change the spring core from 4.3.14.RELEASE to 4.3.4.RELEASE. Yes, by default spring boot starter, is using spring core 4.3.14 which can be extended to 5.0.7.RELEASE for that I tried excluding spring-core and including with 4.3.4.RELEASE
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
When i tried to build it throws the following expection
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChainProxySecurityConfigurer' parameter 1; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.convert.support.DefaultConversionService.getSharedInstance()Lorg/springframework/core/convert/ConversionService;
Manually adding each and every dependency is not possible because the application is using a hell lot of dependencies and compatibility with other dependencies would be another tough job.
So is there any workaround for this?
NOTE:- tried to work with spring framework-bom and spring IO and yet didn't find any solution maybe I missing something. Examples of those are welcomed.
You shouldn't really do this. Spring Framework > Spring Boot > Spring Cloud > Data Flow all manage these dependencies for a reason. You should let spring handle the dependnecies and upgrade your implementations as needed.
https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot
Answers this in more detail.
It is a reasonable thing to want to do this, but it should be done
with caution, because newer versions of transitive dependencies can
easily break features that rely on the older version in Spring
Boot. When you do this, and apply one of the fixes below, you are
divorcing yourself from the dependency management of Spring Boot and
saying “hey, I know what I am doing, trust me.” Unfortunately,
sometimes you need to do this in order to take advantage of new
features in third party libraries. If you don’t need the new version
of Reactor (or whatever other external transitive dependency you
need), then don’t do this, just stick to the happy path and let Spring
Boot manage the dependencies.

JMX on Spring Boot project

I have annotated a class as follows:
#ManagedResource(objectName="com.myproject.bean.jmx:name=JMXSolrIndexerBean",
description="Index Solr Operations")
public class JMXSolrIndexerBean {
....
}
My pom has the following dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
I can't find my MBean in the JConsole... are there any configuration steps I am missing?
Two things:
You don't need the spring-integrtation-jmx dependency to make that work, the actuator starter is enough
Your class needs to be a spring bean if you want Spring Boot to auto-detect JMX annotation on them. So adding #Component on your JMXSolrIndexerBean is all that's needed as long as it is located in a package that is processed by component scan
In other words, that class of yours is just a pojo that spring know nothings about. #ManagedResource is not a stereotype that turns that class in a Spring Bean.

Resources