简体   繁体   English

使用多个EJB将数据保存在CDI托管bean的一个事务中

[英]Use multiple EJB's to save data in one transaction from CDI managed beans

I have following scenario: 我有以下情况:

I have a JSF Dialog(Primefaces) with some tabulators in there. 我有一个带有一些制表符的JSF Dialog(Primefaces)。 The dialog has "OK" Button. 该对话框具有“确定”按钮。 If click "OK" all the data in tabulators should be saved in one transaction. 如果单击“确定”,则制表符中的所有数据应保存在一个事务中。 The problem is each tabulator has associated (CDI) session scoped backing bean, and each backing bean has "save"-Method to save the data of particular tabulator with the help of stateless EJB. 问题在于每个制表符都具有关联的(CDI)会话作用域后备bean,并且每个后备bean具有“保存”方法-在无状态EJB的帮助下保存特定制表符的数据。

How can I do so that all the EJB related code will be saved in one transaction. 我该怎么做,以便将所有与EJB相关的代码保存在一个事务中。

What I already know: 我已经知道的:

  1. Stateful EJB. 有状态EJB。 (This method need to rewrite stateles EJB calls and delegate them to stateful EJB). (此方法需要重写stateles EJB调用并将它们委托给有状态EJB)。
  2. write global stateless EJB that saves the data(this method need the data from each session bean) 编写保存数据的全局无状态EJB(此方法需要每个会话bean中的数据)

It is possible that the tabulator (CDI) session backing bean will be reused in another place. 制表符(CDI)会话支持bean可能会在其他地方重用。 So it should be independent. 因此,它应该是独立的。 What do you think is a good method ? 您认为什么是好方法?

EDIT: Pseudocode: 编辑:伪代码:

@SessionScoped
class BackingBeanTab1 {
   //CDI session scoped bean for Info in Tab1
   String firstname;
   String lastname;

   @Inject StatelessTab1DAO dao1;

   public void save () {
      dao1.save(..);
   }
}

@Stateless
class StatelessTab1DAO {
   public void save (...);
}

Similar for Tab2 ...

@SessionScoped
class BackingBeanTab2 {
   //CDI session scoped bean for Info in Tab1
   int debit;

   @Inject StatelessTab2DAO dao1;
   public void save();
}

@Stateless
class StatelessTab2DAO {
   public void save (...);
}

With this design I can reuse each tabulator. 通过这种设计,我可以重用每个制表符。 But what good possiblites are ther to save in one transaction ? 但是,在一次交易中还能节省哪些好的后代呢?

It depends on your application, but in general data from the tabs should be kept in temporary storage (session or statefull bean) and then saved on dialog OK button. 这取决于您的应用程序,但是通常,选项卡中的数据应保存在临时存储区(会话或全状态bean)中,然后保存在对话框的“确定”按钮上。 There could be at least a few options: 至少可以有一些选择:

  • use stateful bean as facade 使用有状态的bean作为外观

Your facade implements all methods of your stateless beans and stores data in its variables, once the OK button is clicked it invokes save method on facade, facade calls each stateless session bean to save the data - all in one transaction either saved or rolled back. 您的Facade实现了无状态Bean的所有方法并将数据存储在其变量中,一旦单击“确定”按钮,它将在Facade上调用save方法,Facade调用每个无状态会话Bean来保存数据-全部保存或回滚到一个事务中。

  • use http session for temporary storage 使用http会话进行临时存储

This is your current design as I believe from description. 根据您的描述,这是您当前的设计。 But your backing beans should only keep data in session, not save it to the database (as my understanding is that all tabs must be filled for data to be valid). 但是您的后备bean应该只将数据保留在会话中,而不是将其保存到数据库中(因为我的理解是必须填充所有选项卡才能使数据有效)。 Then when the OK button is clicked you could use: 然后,当单击确定按钮时,您可以使用:

either the UserTransaction object to begin the transaction, call all session beans with required data, and commit transaction UserTransaction对象开始事务,使用所需数据调用所有会话bean,然后提交事务

or create a statless facade and call this facade with all required data (which would call all other session beans like in a stateful example), and let the container to handle transaction management. 或创建无状态外观,并使用所有必需的数据调用该外观(这将调用所有其他会话Bean,如有状态示例中所示),然后让容器处理事务管理。

if you like to code in some other path than you can try this, 如果您想尝试其他方式进行编码,可以尝试一下,

for the each cdi validation you can use ajax call so while filling the data in table the inserted data will be validate accordingly .And for saving the data in one transaction you may need cmt or bmt transaction in ejb bean and if your using bmt than the start transaction and commit transaction should be done by developer onle 对于每个cdi验证,您都可以使用ajax调用,以便在将数据填充到表中时对插入的数据进行相应的验证。对于将数据保存在一个事务中,您可能需要在ejb bean中使用cmt或bmt事务,并且如果使用bmt而不是开始事务和提交事务应由开发人员完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM