Data is not persisted in database using Spring Data Repository - DevDummy

Latest

Coding Hacks, Shared ...

Thursday, October 05, 2017

Data is not persisted in database using Spring Data Repository



In a Spring Data project, if the data seems not being persisted in database using Spring Data Repositories, that is strange but it happens.

Surprisingly it never throws an error, even a hidden one and the flow continues as usual even returning the id of the saved object.

Mostly this leads to a false conclusion that the transaction should be rolled-back at last. But it is not true. 

Transaction is committed, but the data is not persisted!

In such a situation, just check whether you have used "ResourcelessTransactionManager" as the Transaction manager of your Spring bean definitions. 

 <bean id="transactionManager"      class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

"ResourcelessTransactionManager" exactly acts as a regular transaction manager except persisting data. It makes a dry run, but not save the data. Also this is fine to work with Spring Data NoSQL frameworks.

Solution


In such a case replace above with the relevant transaction manager. If it is Spring Data JPA, as follows,

<beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>



No comments:

Post a Comment