Spring Data JpaRepository throws LazyInitializationException when using method getById() but not when using findById() - spring-boot

in my Spring Boot (version 2.5.4) I have a service that executes one of its methods by a ExecutorService (in new Thread). in this new Thread I access some of my repositories (JpaRepositorys). I see different behavior in JpaRepository's getById() and findById(), I searched but did not found any.
this is my entity:
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Entity
public class LimoSet {
#Id
#Column(name = "_id")
private String id;
#Column(name = "_status")
private String status;
#OneToMany(mappedBy = "set", fetch = FetchType.EAGER)
private Set<Limo> limos = new LinkedHashSet<>();
#Column(name = "_statistics")
private String statistics;
}
this is repository:
#Repository
public interface LimoSetRepository extends JpaRepository<LimoSet, String> {
}
and this is the service:
#Service
#Transactional
public class GeneratorService {
private final static Logger logger = LoggerFactory.getLogger(GeneratorService.class);
private final LimoSetRepository setRepository;
private final ExecutorService executor = Executors.newFixedThreadPool(3);;
public void generate(Options opts) {
.
.
.
Callable<String> task = () -> {
try {
this.runGenerate(opts);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
var set = setRepository.getById(opts.getName());
set.setStatus(e.getMessage());
setRepository.save(set);
}
return "ok";
};
executor.submit(task);
}
void runGenerate(Options opts) throws JsonProcessingException {
.
.
.
var set = setRepository.findById(opts.getName()).get(); //this is ok
var set = setRepository.getById(opts.getName()); //this throws LazyInitializationException
set.setStatus("GENERATED"); //the Exception is reported in this line
setRepository.save(set);
}
}
why findById() works but getById() does not?

There are a few factors which sum up to the LazyInitializationException.
For primers:
get acquainted with the different semantics of findById and getById
findById fetches entire object
getById fetches object proxy
See: How do find and getReference EntityManager methods work when using JPA and Hibernate
When loading a Proxy, you need to be aware that a LazyInitializationException can be thrown if you try to access the Proxy reference after the EntityManager is closed.
Secondly, you need to understand why runGenerate runs with no active transaction, even if the class is marked as #Transactional.
(Note: no active transaction is the root cause of closed EntityManager)
To make sure that the transaction is not active, see Detecting If a Spring Transaction Is Active:
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
Why parent transaction from the test is not propagated
runGenerate is run via executor on a separate thread
active transaction is kept as a thread-local, so transactions do not propagate across threads.
Why #Transactional on runGenerate has no effect?
Spring (by default) uses CGLIB proxies to enable aspects.
CGLIB proxies has 2 requirements:
the proxied method must be public
you must call the method via the proxy (not via the actual object)
None of these conditions is met.
Why the transaction is active in the test itself
Spring provides a special TestExecutionListener called TransactionalTestExecutionListener, which checks if the test method is marked as #Transactional, and starts transaction if needed
How to fix
extract runGenerate to a service, mark it as public and #Transactional
autowire the new service into your test
more generally, take note what parts of your task operate under transactional context (catch block of your task is not transactional)
also more generally, learn about dirty checking. once you have your methods to run under transactional context, the save calls will be redundant.

Related

Spring Data Redis. JPA Repository findBy sometimes fails to fetch existing record

I see some weird case. Sometimes my findBy...() method returns null instead of some object inserted and fetched successfully before. After that the needed object fetches fine. In other words sometimes the search is not working.
Spring Boot edition: 1.5.2.RELEASE
spring-boot-starter-data-redis: 1.5.22.RELEASE
"maxmemory-policy" setting is set to "noeviction"
My obj declaration:
#RedisHash("session")
public class Session implements Serializable {
#Id
private String id;
#Indexed
private Long internalChatId;
#Indexed
private boolean active;
#Indexed
private String chatId;
}
JPA Repository:
#Repository
public interface SessionRepository extends CrudRepository<Session, String> {
Session findByInternalChatIdAndActive(Long internalChatId, Boolean isActive);
}
Redis config:
#Bean
public LettuceConnectionFactory redisConnectionFactory(
RedisProperties redisProperties) {
return new LettuceConnectionFactory(
redisProperties.getRedisHost(),
redisProperties.getRedisPort());
}
#Bean
public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
return template;
}
Thanx in advance for any assist.
We have recently seen similar behavior. In our scenario, we can have multiple threads that read and write to the same repository. Our null return occurs when one thread is doing a save to an object while another is doing a findById for that same object. The findById will occasionally fail. It appears that the save implementation does a delete followed by an add; if the findById gets in during the delete, the null result is returned.
We've had good luck so far in our test programs that can reproduce the null return using a Java Semaphore to gate all access (read, write, delete) to the repository. When the repository access methods are all gated by the same semaphore, we have not seen a null return. Our next step is to try adding the synchronized keyword to the methods in the class that access the repository (as an alternative to using the Semaphore).
This should not happen I don't what is reason. But you can use Option class and if it returns null at least you can avoid exception.
Something like:
Optional<Session> findByInternalChatIdAndActive(Long internalChatId, Boolean isActive);

How to make DataJpaTest flush save automatically?

I have an Employee entity with the following column:
#Entity
class Employee {
#Column(name = "first_name", length = 14)
private String firstName;
and I have a Spring JPA Repository for it:
#Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
In test/resources/application.properties I have the following so that I use an in-memory h2 database with tables auto-generated:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
I was expecting this test to fail, since the firstName is longer than what is allowed:
#DataJpaTest
public class EmployeeRepositoryTest {
#Autowired
private EmployeeRepository employeeRepository;
#Test
public void mustNotSaveFirstNameLongerThan14() {
Employee employee = new Employee();
employee.setFirstName("koraykoraykoray"); // 15 characters!
employeeRepository.save(employee);
}
}
And I was surprised to see this test was not failing, however the following does fail:
#DataJpaTest
public class EmployeeRepositoryTest {
#Autowired
private EmployeeRepository employeeRepository;
#Test
public void testMustNotSaveFirstNameLongerThan14() {
Employee employee = new Employee();
employee.setFirstName("koraykoraykoray"); // 15 characters!
employeeRepository.save(employee);
employeeRepository.findAll();
}
}
with the stacktrace:
Caused by: org.h2.jdbc.JdbcSQLDataException: Value too long for column "FIRST_NAME VARCHAR(14)": "'koraykoraykoray' (15)"; SQL statement:
The only difference is the second test has the additional employeeRepository.findAll(); statement, which forces Hibernate to flush as far as I understand.
This does not feel right to me, I would much rather want the test to fail immediately for save.
I can also have
#Autowired
private TestEntityManager testEntityManager;
and call
testEntityManager.flush();
but again, this does not feel correct either.. How do I make this test fail without any workaround or additional statements?
The easiest option in your case is configure #Transactional annotation, forcing to send database all changes in your tests (it can be used only in specific ones):
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.jupiter.api.Assertions.assertThrows;
#Transactional(propagation = Propagation.NOT_SUPPORTED)
#DataJpaTest
public class EmployeeRepositoryTest {
#Autowired
private EmployeeRepository employeeRepository;
#Test
public void mustNotSaveFirstNameLongerThan14() {
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("koraykoraykoray"); // 15 characters!
assertThrows(DataIntegrityViolationException.class, () -> {
employeeRepository.save(employee);
});
}
#Test
public void mustSaveFirstNameShorterThan14() {
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("koraykor"); // 8 characters!
employeeRepository.save(employee);
}
}
PD: I have added a simple Integer property as PK of Employee entity due to your repository definition.
You can see the results in the following picture:
You could use JpaRepository<T,ID> instead of CrudRepository<T,ID>. Something like:
#Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer>
Then you can use its saveAndFlush() method anywhere you need to send data immediately:
#Test
public void mustNotSaveFirstNameLongerThan14() {
Employee employee = new Employee();
employee.setFirstName("koraykoraykoray"); // 15 characters!
employeeRepository.saveAndFlush(employee);
}
And in code where you would like to have optimization you still can use save() method.
Thanks doctore for your answer, I had the similar problem as OP and your solution has helped. I decided to dig a little and figure out why it works, should someone else have this problem.
With #DataJpaTest annotated test class, your class implicitly becomes #Transactional with default propagation type Propagation.REQUIRED. That means every test method is also #Transactional with same default configuration. Now, all CRUD methods in CrudRepository are also #Transactional, but it has nothing to do with #DataJpaTest - they are transactional due to implementation. Whoa, that's a lot of transactions!
As soon as you annotate your whole class (or just a test method) with #Transactional(propagation = Propagation.NOT_SUPPORTED), your test method(s) are no longer #Transactional. However, inner methods of your test method(s), that is, CRUD operations from CrudRepository, remain transactional, meaning that they will have their own transaction scopes. Because of that, they will be committed to database immediately after execution, because by default (in Spring Boot, which users HikariCP connection pool), auto commits are turned on. Auto commits happen after every SQL query. And thus tests pass as you'd expect.
I like to visualize things, so here is the visualization of the whole process:
I hope this was helpful. URLs from the diagram:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions
https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html#disable_auto_commit
https://github.com/brettwooldridge/HikariCP/blob/dev/src/main/java/com/zaxxer/hikari/HikariConfig.java#L126
https://dzone.com/articles/spring-boot-transactions-tutorial-understanding-tr (not from diagram, but explains transaction very well!)
The #Commit can do the job ( it was added since 4.2)
#Test
#Commit
public void mustNotSaveFirstNameLongerThan14() {
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("koraykoraykoray"); // 15 characters!
assertThrows(DataIntegrityViolationException.class, () -> {
employeeRepository.save(employee);
});
}

Unable to initialize lazy-loaded relationship inside of `#Transactional` method

I have a set of simple models like this (getters and setters omitted for brevity):
#Entity
public class Customer {
#Id
private Integer id;
}
#Entity
public class Order {
#Id
private Integer id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "customer_id")
private Customer customer;
}
I am trying to load an Order using a Spring JPA repository with a findById method, including the customer.
First I tried this:
#Transactional
Optional<Order> findById(Integer id) {
return repository.findById(id);
}
But when I tried to access Customer I got a LazyInitializationException: could not initialize proxy - no Session. So after referring to some other questions, I updated my method to be a bit uglier, but to explicitly call Hibernate.initialize:
#Transactional
Optional<Order> findById(Integer id) {
return repository.findById(id)
.map( order -> {
Hibernate.initialize(order.getCustomer());
return order;
);
}
But I still get org.hibernate.LazyInitializationException: could not initialize proxy - no Session. repository is a regular CrudRepository which provides the findById method out-of-the-box.
How can I initialize this lazily loaded child entity? My understanding is that the #Transactional indicates that I should still be within the transaction for the entirety of this method call. The only thing further downstream is the repository itself, which is just an interface, so I'm not sure how else to go about forcing the load of this child entity.
The Order entity and everything else in it is retrieved properly from the database; it's only when I try to get the lazy-loaded child entities that we start having issues.
The only way I managed to get this working was to write a custom hql method in the Repository using a left join fetch. While that works, it clutters up my repository with a method that is pretty much a duplicate of another and which I'm pretty sure I'm not actually supposed to need (so I would rather not do it this way.)
Spring-Boot 2.1.4.RELEASE, Spring 5.1.6.RELEASE, Hibernate 5.3.7.Final.
You have to define the method as public. See "Method visibility and #Transactional" in the spring docs.
This should work:
#Transactional
public Optional<Order> findById(Integer id) {
Optional<Order> order = repository.findById(id);
order.ifPresent(o -> Hibernate.initialize(o.getCustomer()));
return order;
}

Transaction does not retrieve on time data

I have a project with many services. I'm using pure jpa with jersey in my project.
I use entityManager, entityTransaction and entityManagerFactory in this way: for each service I get an EntityManager from DBManager.getEntityManager() and get and entityTransaction from its entityManager
public class DBManager {
public static EntityManager getEntityManager() {
return Persistence.createEntityManagerFactory("projectDataSource").getEntityManager();
}
In each method that need transaction, first I check transaction != null and transaction is not active
Then I begin the transaction.
I evict cache and after all of this I do my job: in this example I get all active users from database, and my service that returns users list to client.
One of my services is : #getUsersService
public class GetUsersServiceImpl {
private static EntityManager entiyManager = DBManager.getEntityManager();
private static EntityTransaction entityTransaction = entityManager.getTransaction();
public List<User> getUsers()
if (transaction != null && !transaction.isActive())
transaction.begin()
entityManager.getEntityMangerFactory().getCache().evictAll();
return entityManger.createQuery("SELECT u FROM USER u WHERE u.isActive = true").getResultList();
}
This code works but not always!
I have another service that changes the users; this user also works and changes user data in database, but after calling change service when I call getUsers service, it retrieves old database data
Why does this happen?
Is it correct the way I use entityManager and entityTransaction ...

OptimisticLockException not thrown when version has changed

I've created a simple EJB application that uses JPA for persistence and have a problem whereby optimistic locking is not functioning as I would have expected.
The application contains a class named Site which defines the model for a table named SITE in the database. The SITE table contains a column named ROW_VERSION which is referenced in the Site class using the #version annotation.
Whenever the record is updated, the ROW_VERSION is incremented by 1. So far, so good.
The problem arises when the row has changed in the time between the application reading the row using the EntityManager find method and the row being updated by the EntityManager merge method. As the ROW_VERSION for the row has been incremented by 1 and therefore is not the same as when the EntityManager find method was called, I would expect an OptimisticLockException to be thrown, but instead the changes are written to the table and in turn overwriting the changes made by the other process.
The application is running on WebSphere 8.5 and is using OpenJPA provided by the container.
Have I mis-understood how optimistic locking is supposed to work or is there something else that I need to do to make the OptimisticLockException occur?
The Site class is as follows:
#Entity
#Table(name="SITE")
public class Site {
#Id
#Column(name="SITE_ID")
private int id;
#Column(name="SITE_NAME")
private String siteName;
#Column(name="SITE_ADDRESS")
private String address;
#Column(name="ROW_VERSION")
#Version
private long rowVersion;
//getters and setters
}
The application makes uses of the Generic DAO wrapper class to invoke the EntityManager methods. The contents of the class are as follows:
public abstract class GenericDAO<T> {
private final static String UNIT_NAME = "Test4EJB";
#PersistenceContext(unitName = UNIT_NAME)
private EntityManager em;
private Class<T> entityClass;
public GenericDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
public T update(T entity) {
return em.merge(entity);
}
public T find(int entityID) {
return em.find(entityClass, entityID);
}
//other methods
}
Update - I've done some more investigation and have found this http://pic.dhe.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=%2Fcom.ibm.websphere.nd.multiplatform.doc%2Finfo%2Fae%2Fae%2Fcejb_genversionID.html but even when I've added the #VersionColumn and #VersionStrategy annotations I still cannot get the OptimisticLockException to be thrown.

Resources