在EJB中获取多个事务的干净,标准方法是什么?

编程入门 行业动态 更新时间:2024-10-09 00:52:58
本文介绍了在EJB中获取多个事务的干净,标准方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 batchEdit(List< E>实体),它调用编辑(E实体)函数一个循环,而每个edit()都拥有它自己的事务,因此失败的编辑不会回滚良好的编辑。我目前的实施方式如下:

I have a batchEdit(List<E> entity) that calls an edit(E entity) function in a loop, while each edit() has it's own transaction so that failed edits don't rollback the good edits. I currently have it implemented like so:

选项1

@Stateless @TransactionManagement( value = TransactionManagementType.CONTAINER ) public class Service<E> { @Resource private SessionContext context; @Override @TransactionAttribute( value = TransactionAttributeType.REQUIRES_NEW ) public E edit( E entity ) { //edit code } @Override public List<E> bulkEdit( List<E> entities ) { for(E entity : entities){ //case 1: Regular edit, Does not create a new transaction! //edit(entity); //case 2: Hacky edit, creates a new transaction context.getBusinessObject( Service.class ).editPersistNulls( entity ); } } }

根据此stackoverflow讨论, @TransactionAttribute 在我的情况1中被忽略,因为它不跨越任何EJB边界,所以 batchEdit()调用 edit()好像它没有注释。在案例2中使用 context.getBusinessObject()函数来获取bean的引用会导致TransactionManagement批注工作,但是完成所有这些操作似乎很奇怪。

According to this stackoverflow discussion, The @TransactionAttribute is ignored in my case 1 because it doesn't cross any EJB boundaries, so batchEdit() calls edit() as if it wasn't annotated. Using the context.getBusinessObject() function in case 2 to grab a reference of the bean causes the TransactionManagement annotation to work, but it seems really weird to go through all of that.

选项2

我的另一个选择是更改为bean托管交易:

The other option I have is to change to bean managed transactions:

@TransactionManagement( value = TransactionManagementType.BEAN )

然后我会失去JPA Magic并且必须管理各地的交易。我不认为我团队中的其他人会想要通过它,所以如果有更好或标准的方法来做到这一点,那么任何见解都会受到赞赏。

But then I would lose the "JPA Magic" and have to manage the transactions everywhere. I don't think other people on my team would want to go through that, so if there's a better or standard way to do this, any insight is appreciated.

我们正在使用OpenJPA和EJB,但我们正试图接近JPA标准。

We are using OpenJPA and EJBs, but we are trying to stay close to the JPA standard.

推荐答案

我认为hacky在旁观者的眼中。 context.getBusinessObject 精确存在,这样你就可以做到这一点。

I guess that "hacky" is in the eye of the beholder. context.getBusinessObject exists precisely so that you can do this kind of thing.

另一种选择是使用秒class:

The alternative is to use a second class:

@Stateless public class BulkService<E> { @EJB private Service<E> service; public List<E> bulkEdit( List<E> entities ) { for(E entity : entities) { service.editPersistNulls( entity ); } } }

谨防但是,大型实体列表,因为您的包含事务可以超时。

Beware of large entity lists though, as your encompassing transaction can time out.

如果您使用的是符合Java EE 7的服务器实现,那么请考虑使用 JSR-352批处理应用程序支持。

If you're using a Java EE 7 compliant server implementation then consider using the JSR-352 Batch Applications support.

更多推荐

在EJB中获取多个事务的干净,标准方法是什么?

本文发布于:2023-10-19 17:39:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   干净   事务   标准   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!