如何在 EJB 容器之外控制 BMT 事务?

How to control a BMT transaction outside of the EJB container?

出于好奇,是否可以直接从 Web 容器控制 EJB 事务?

为了说明,我制作了这个简单的示例,在 Web 容器中启动一个 UserTransaction(使用 Servlet),但事务未绑定到 EJB 容器(在本例中为 BMT SFSB)。

这是为什么?有办法吗?

使用 BMT 的有状态会话 Bean

@Stateful
@TransactionManagement(TransactionManagementType.BEAN)
public class CustomerBean implements CustomerBeanLocal{

    @PersistenceContext(type=PersistenceContextType.EXTENDED)
    private EntityManager em;

    @Override
    public Integer createCustomer(String name) {

        Customer customer = new Customer();
        customer.setId(1);
        customer.setName(name);
        em.persist(customer);
        //em.flush();

        return customer.getId();
    }   
}

UserTransaction 在 Servlet 中启动,但 Session Bean 不持久化

客户未保存到数据库。

public class BMTServlet extends HttpServlet {

    @EJB
    private CustomerBeanLocal customerBean;

    @Resource
    private UserTransaction userTransaction;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {           

            userTransaction.begin();

            customerBean.createCustomer("Tars");       

            userTransaction.commit();

        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
}

如果我们取消对 em.flush(); 的注释,则会出现以下异常:

javax.persistence.TransactionRequiredException: no transaction is in progress
    org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:792)
    org.jboss.ejb3.jpa.integration.JPA1EntityManagerDelegator.flush(JPA1EntityManagerDelegator.java:86)
    bean.CustomerBean.createCustomer(CustomerBean.java:25)

BMT 在您的场景中不起作用,因为 BMT bean 将自行处理事务并且不会参与在 Web 模块中启动的事务(容器事务)。要使用 UserTransaction 从 servlet 控制事务,bean 必须是 CMT。