@ManagedBean 和 @Transactional - Spring 中的错误?解决方法?
我的 Web 应用程序中有以下 JSF 支持 bean
@ManagedBean
class MyBackingBean implements Serializable {
private MyHibernateRepository repository;
...
@Transactional
public void save() {
....
repository.save(myObject);
}
}
当它到达 repository.save
方法调用时 - 我收到以下错误
no transaction is in progress
我有两个问题
- 这是因为错误 像这样?
- 我相信有两种解决方法 - 还有其他方法吗?
2.1 第一个解决方法 - 使用
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
repository.save(myObject);
}
});
2.2 第二个解决方法
创建一个帮助器类并对其进行注释。
2.3(第三种可能的解决方法是在内部类的方法上注释 @Transactional 这与 2.2 非常相似)。
I had the following JSF backing bean in my Webapp
@ManagedBean
class MyBackingBean implements Serializable {
private MyHibernateRepository repository;
...
@Transactional
public void save() {
....
repository.save(myObject);
}
}
When it gets to the repository.save
method call - I get the following error
no transaction is in progress
I've got two questions
- Is this because of a bug like this?
- I believe there are two workarounds - are there any others?
2.1 First workaround - using
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
repository.save(myObject);
}
});
2.2 Second workaround
Create a helper class and annotate that instead.
2.3 (A possible third workaround would be to annotate @Transactional on a method of an inner class This is quite similar to 2.2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Spring 注释时(我知道 @Transactional 是 Sun 标准 - 但您需要一个实现) - Spring 使用 AOP 注释类以添加事务处理代码。这仅适用于 Spring bean。如果您的类是 JSF 的支持 bean - Mojarra 框架不会将自己的事务处理代码插入到此注释中。
简短的回答 -
@Transactional
适用于 Spring 加载的 bean。否则,您需要找到一个支持它的框架,否则就认为它不起作用。When using Spring annotations (I know that
@Transactional
is a Sun standard - but you need an implementation) - Spring uses AOP to annotate to the class to add the transaction handling code. This only works for Spring beans. If your class is a backing bean for JSF - the Mojarra framework won't insert its own transaction handling code to this annotation.Short answer -
@Transactional
works for beans loaded by Spring. Otherwise you need to find a framework that supports it or assume it won't work.