@Transactional 回滚除了对数据库的一个保存操作之外的所有内容。

huangapple 未分类评论46阅读模式
英文:

@Transactional rollback everything except one save on the database

问题

在我的之前的问题得到了很好的回答 => https://stackoverflow.com/questions/60995945/transactional-not-working-when-i-throw-exception-on-next-line

我现在有这个问题:

我有一个方法:

@Transactional
public void processFile(final Path path) {
    
    try {

        //操作数据库并添加条目的代码

    } catch (final Exception e) {
         this.handleException(e));
    }
 }

该方法会调用下面的方法,将“失败的文件名”作为数据库表(monitoring)上的条目添加:

void handleException(final Throwable e) {

    //保存未能处理的文件的文件名
    this.filesMonitoringJpaManager.save(newEntryFileName);

    throw new Exception(...)
}

所以,方法processFile正在进行各种数据库更改,我希望在发生任何错误时回滚。尽管方法handleException正在将失败处理的文件名保存到表monitoring中。

那么:

在出现异常的情况下如何回滚整个事务,但仍然在数据库上执行this.filesMonitoringJpaManager.save(newEntryFileName);的“保存/更新”操作,因为我需要它?

英文:

Following my previous question which was nicely answered => https://stackoverflow.com/questions/60995945/transactional-not-working-when-i-throw-exception-on-next-line

I have this question now :

I have a method:

@Transactional
public void processFile(final Path path) {
	
	try {

        //code that manipulates database adding entries

	} catch (final Exception e) {
		 this.handleException(e));
	}
 }

which calls the below appending the fileName that failed as an entry on the database table (monitoring):

void handleException(final Throwable e) {

    //Save the name of the file that failed to be processed
	this.filesMonitoringJpaManager.save(newEntryFileName);

	throw new Exception(...)
}

So the method processFile is doing various database changes that i want to rollback when any error occurs . Though method handleException is saving to table monitoring the fileName is failed to be processed.


So :

How can i rollback the whole transaction in case of Exception but still do that save/update this.filesMonitoringJpaManager.save(newEntryFileName); on the database because i need it ?

huangapple
  • 本文由 发表于 2020年4月6日 19:55:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61059253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定