安全地处理Hibernate“SET TRANSACTION必须是事务的第一条语句”错误 [英] Safely handle Hibernate "SET TRANSACTION must be first statement of transaction" error
问题描述
我不确定我是否提出正确的问题,先进的歉意。
问题
我想知道是否有可能有某种类型的处理程序在Hibernate中回滚错误的事务。我遇到了一个问题,即在Hibernate批量更新过程中出现错误时,SET TRANSACTION必须是事务的第一条语句错误出现,之后我将无法执行任何其他查询。
感谢
:)
Hibernate没有自动事务管理系统,所以你必须自己发出开始,提交和回滚命令。
如果你使用spring,那么spring事务可以更容易管理 API
例如,
@Transactional
void updateFoo(Foo foo ){
//做你的东西
}
在上面的例子中,a新事务将在最后一行提交的 updateFoo()方法的开始处开始。如果交易之间有任何问题将会回滚。
I'm not sure if I'm asking the correct question to begin with, apologies in advanced.
Question
I am wondering if it is possible to have some type of handler to rollback an erroneous transaction in Hibernate. I am having a problem which, whenever an error comes up during a batch update from Hibernate the "SET TRANSACTION must be first statement of transaction" error comes up and I would not be able to do any other query after that.
Thanks :)
Hibernate don't have an automatic transaction management system, so you have to issue the begin, commit and roll back commands by yourself.
If you are using spring this can be a lot more easier with spring transaction management API
For example,
@Transactional
void updateFoo(Foo foo){
//do your stuff
}
In the above example a new transaction will be started at the beginning of the updateFoo() method commited at the last line. If there is any problem in between the transaction will be rolled back.
这篇关于安全地处理Hibernate“SET TRANSACTION必须是事务的第一条语句”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!