Rollback does not work using MongoTransactionManager - spring

Hello I am developping a back-end using Spring boot and MongoDB 4.0. In order to add transactions I have implemented the MongoTransactionManager as seen in the documentations spring mongo transactions
#Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
But when I annotate a method with #Transactional(rollbackFor = NullPointerException.class) it does not roll back for this exception.
For example the following test does not work.
Do you have any advices to fix this issue please ?
#Test
#Transactional(rollbackFor = NullPointerException.class)
public void testTransaction() {
try {
myRepo.deleteAll();
throw new NullPointerException();
} catch (
NullPointerException e) {
} finally {
assertThat(myRepo.findAll()).isNotEmpty();
}
}

Just figure it out that MongoTransactionManager does not work if you also register a Bean MongoTemplate.
Moreover, surprinsingly, #Transactional method does not work if it is a #Test method. You must extract the #Transactional method in a #Service.

Because you just caught your NPE and didn't do anything with it. For transaction being rollbacked your method should throw NPE out of the method.

Related

Spring Boot Transaction support using #transactional annotation not working with mongoDB, anyone have solution for this?

Spring Boot version - 2.4.4,
mongodb version - 4.4.4
In my project, I want to do entry in 2 different document of mongodb, but if one fails than it should do rollback. mongodb supports transaction after version 4.0 but only if you have at least one replica set.
In my case I don't have replica set and also cannot create it according to my project structure. I can't use transaction support of mongodb because no replica-set. So, I am using Spring Transaction.
According to spring docs, to use transaction in Spring Boot, you only need to use #transactional annotation and everything will work(i.e. rollback or commit).
I tried many things from many sources but it is not rollbacking transaction if one fail.
Demo code is here,
This is demo code, not actual project.
This is my service class.
#Service
public class UserService {
#Autowired
UserRepository userRepository;
#Autowired
UserDetailRepository userDetailRepository;
#Transactional(rollbackFor = Exception.class)
public ResponseEntity<JsonNode> createUser(SaveUserDetailRequest saveUserDetailRequest) {
try {
User _user = userRepository.save(new User(saveUserDetailRequest.getId(), saveUserDetailRequest.getFirstName(), saveUserDetailRequest.getLastName()));
UserDetail _user_detail = userDetailRepository.save(new UserDetail(saveUserDetailRequest.getPhone(), saveUserDetailRequest.getAddress()));
} catch (Exception m) {
System.out.print("Mongo Exception");
}
return new ResponseEntity<>(HttpStatus.OK);
}
}
Also tried below code but still not working,
#EnableTransactionManagement
#Configuration
#EnableMongoRepositories({ "com.test.transaction.repository" })
#ComponentScan({"com.test.transaction.service"})
public class Config extends AbstractMongoClientConfiguration{
private com.mongodb.MongoClient mongoClient;
#Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
#Bean
public com.mongodb.MongoClient mongodbClient() {
mongoClient = new com.mongodb.MongoClient("mongodb://localhost:27017");
return mongoClient;
}
#Override
protected String getDatabaseName() {
return "test";
}
}
The transaction support in Spring is only there to make things easier, it doesn't replace the transaction support for the underlying datastore being used.
In this case, it will simply delegate the starting/committing of a transaction to MongoDB. WHen using a database it will eventually delegate to the database etc.
As this is the case, the pre-requisites for MongoDB still need to be honoured and you will still need a replica.

I want nesting of transaction in spring boot using #Transactional annotation

Currently if exception occurs in method C, code is not rolling back database changes from method B. My expectation is method A should manage transaction in such a way that if exception occurs in method C, code should rollback changes done in method B.
I am using spring boot, maven project.
class SomeClassA{
#Autowired
SomeClassB someClassB;
#Autowired
SomeClassC someClassC;
#Transactional
public A(){
try{
//This method works fine with some database operations.
someClassB.B();
//In this method, exception occurrs.
someClassC.C();
}
catch(Exception e){
}
}
}
class SomeClassB{
#Transactional
public B(){
//some code with database operation
}
}
class SomeClassC{
#Transactional
public C(){
//some code with database operation
//some exception occurs here
}
}
Is it a Checked or Runtime exception ?
Because the default behaviour of #Transactional says:
Any RuntimeException triggers rollback, and any checked Exception does
not.

Is their some kind of error handler for Spring #TransactionalEventListener

Since recently I've been testing the use of the new #TransactionalEventListener from Spring.
But I can't find a nice way to handle error that could be thrown in the event listener method.
By the way for what I've tested the #EventListener annotation doesn't have the same behavior : the RunTimeException is thrown as expected.
For example I would like to avoid write the try catch to be able to know about the error :
#Component
public class PairingEventListener {
...
#TransactionalEventListener
#Transactional
public void onPairingSuccessEvent(PairingSuccessEvent event) {
try {
// some code here that could throws runtime error
} catch (Exception e) {
logger.error(e);
}
}
}
Does anyone know if there a way to achieve something equivalent to JmsErrorHandler but with Spring ApplicationEvent?
#Autowired
DefaultJmsListenerContainerFactory jmsListenerContainerFactory;
...
jmsListenerContainerFactory.setErrorHandler(new JmsErrorHandler());
Thanks !

Spring MVC handling exceptions

I've built a spring mvc application using the controller->service->dao architecture. The DAO objects are using hibernate. The services are annotated #Transactional.
I'm trying to catch dao exceptions in the service, wrap them up and then throw them to my controller:
Service
#Override
public Entity createEntity(Entity ent) throws ServiceException {
try {
return entityDAO.createEntity(ent);
} catch (DataAccessException dae) {
LOG.error("Unable to create entity", dae);
throw new ServiceException("We were unable to create the entity for the moment. Please try again later.", dae);
}
}
Controller
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String createEntity(#ModelAttribute(value = "newEntity") Entity newEntity, RedirectAttributes redirectAttributes) {
try {
entityService.createEntity(newEntity);
} catch (ServiceException se) {
redirectAttributes.addFlashAttribute("error", se.getMessage());
}
}
return "redirect:/entity/manage";
}
However, even though the DataAccessException is caught at the service level, it keeps bubbling up to my controller somehow.
If for example I don't meet a unique field criteria on the database level I get an HTTP Error 500 with the following:
org.hibernate.AssertionFailure: null id in com.garmin.pto.domain.Entity entry (don't flush the Session after an exception occurs)
Code is caching DataAccessException not HibernateException, try caching HibernateException
Is there a way to translate HibernateException to something else, then DataAccessException in sping
If you want to handle the exception in the Controller, don't catch it in the Service.
Service
#Override
public Entity createEntity(Entity ent) throws DataAccessException {
return entityDAO.createEntity(ent);
}
Controller
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String createEntity(#ModelAttribute(value = "newEntity") Entity newEntity, RedirectAttributes redirectAttributes) {
try {
entityService.createEntity(newEntity);
} catch (DataAccessException e) {
redirectAttributes.addFlashAttribute("error", e.getMessage());
}
return "redirect:/entity/manage";
}
Or if you want to leverage Spring the handle the exception, use ExceptionHandler annotation. You can find good tutorial online, for example, Spring MVC #ExceptionHandler Example.
To make exception translation working
You have to annotate your DAO with #Repository
Make sure you have declared this bean <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
Here is a beautiful post on different ways to handle exceptions on a Spring MVC project.
Among those, I find using #ControllerAdvice classes, to handle all the exceptions at one place globally, the most convenient in general. Spring Lemon's source code could serve as a good concrete example.

Spring #Transactional rollbackFor not working

I have a code like below
public abstract class AffltTransactionService implements IAffltTransactionService {
....
#Override
#Transactional
public void processTransactions(List<? extends AffltTransaction> transactions) {
for (AffltTransaction transaction : transactions) {
if (transaction != null) {
processTransaction(transaction);
}
}
}
private void processTransaction(AffltTransaction transaction) {
try {
processTransactionInternal(transaction);
} catch (Exception exception) {
affltTransactionError = new AffltTransactionError(null, null, "error", new Date());
saveAffltTransactionError(affltTransactionError);
log.error(exception.getLocalizedMessage());
}
}
#Transactional(readOnly=false, rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void processTransactionInternal(AffltTransaction transaction) {
processTransactionInternal throws ServiceUnAvailableException which extends RuntimeException
But the transaction is not getting rolled back despite having rollbackFor = Exception.class .
Can you please help.
#Transactional annotation won't have any effect if you are calling the method directly, since Spring creates proxies above annotated classes and the aspect-defined functionality is implemented by proxy. So, when you call the method from within your class it doesn't get through proxy, and hence no transcation is created and/or rolled back.
Take a look at the Spring reference for detailed explanation.
Since you invoke one method from another within the same bean, the Spring AOP doesn't use any advices in this case.
Only processTransactions is wrapped with TransactionInteceptor.
To make it worked you should configure:
<aop:aspectj-autoproxy expose-proxy="true"/>
But it isn't recommened, though.
More info here: http://www.intertech.com/Blog/secrets-of-the-spring-aop-proxy
Use getCurrentSession instead of opensession
I know it was asked a long time ago but I faced with the same issue and for me was missing Spring configurantion annotation:
#EnableTransactionManagement
After write it on ApplicationConfiguration class it was solved. I hope it helps someone on future.
The method you use apply #Transactional should throws exception. Don't use try-catch instead.(I guess you use try-catch in somewhere in your processTransaction function).
Code should be like this:
#Transactional
public void processTransactions(List<? extends AffltTransaction> transactions) threws Exception{
for (AffltTransaction transaction : transactions) {
if (transaction != null) {
processTransaction(transaction);
}
}
}

Resources