如何在EJB中提交事务?
我有以下场景,
public void someEjbMethod1()
{
for (int i=0; i=10; i++)
{
em.merge(arr[i]);
em.flush();
}
}
我需要分别合并 (arr[i]
) 的每个对象。因为上面的代码将在函数末尾提交所有 arr[i] 实例。
我正在考虑做以下事情:
public void someEjbMethod1()
{
for (int i=0; i=10; i++)
{
saveObj(arr[i]);
}
}
// should I use a transaction attribute here??
public void saveObj(SomeObject obj)
{
em.merge(arr[i]);
em.flush();
}
I have the following scenario,
public void someEjbMethod1()
{
for (int i=0; i=10; i++)
{
em.merge(arr[i]);
em.flush();
}
}
I need to merge each object of (arr[i]
) separately. as the above code will commit all the arr[i]
instances at the end of the function.
I am thinking to do the following:
public void someEjbMethod1()
{
for (int i=0; i=10; i++)
{
saveObj(arr[i]);
}
}
// should I use a transaction attribute here??
public void saveObj(SomeObject obj)
{
em.merge(arr[i]);
em.flush();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要容器管理事务,则可以使用带有值 TransactionAttributeType.REQUIRES_NEW 将
saveObj
方法注释为:这将确保每次调用都会启动一个新事务
saveObj
方法。与someEjbMethod
关联的现有事务将在每次调用saveObj
方法之前挂起。为saveObj
方法启动的每个事务都将在返回时提交,因此每个实体都将在其自己的事务中在数据库中更新。If you want container managed transactions, you may use the @TransactionAttribute with the value TransactionAttributeType.REQUIRES_NEW to annotate the
saveObj
method as:This will ensure that a new transaction will be started for every invocation of the
saveObj
method. The existing transaction associated with thesomeEjbMethod
will be suspended before every invocation of thesaveObj
method. Every transaction started for thesaveObj
method will be committed on return, and hence every entity will be updated in the database in it's own transaction.您可以请求 UserTransaction,看看 这里 获取一些灵感。
You can request a UserTransaction, have a look here for some inspiration.