我怎样才能在 EJB 上获得隔离事务?

how can i get a isolate transaction on EJB?

我有以下 EJB 会话 bean:

@Stateless
public class FacadeBean implements FacadeBeanRemote {
    @PersistenceContext(unitName = "ejercicio7PU")
    private EntityManager em;

    @Override
    public void insertar(final long id) {
        Record r = new Record();
        r.setId(id);
        em.persist(r);
    }

    @Override
    public void insertarTres(final long id1, final long id2, final long id3) {
        Logger.getLogger(FacadeBean.class.getName()).log(Level.INFO, "Insertando registros");
        insertar(id1);
        insertar(id2);
        insertar(id3);
    }

}

记录实体如下:

@Entity
public class Record implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Record{" + "id=" + id + '}';
    }

}

我在独立 java 应用程序上调用远程方法来插入三个数字:

 ejb.insertarTres(1l, 2l, 3l);

现在,我希望当我使用以下数字再次调用远程方法时:

ejb.insertarTres(4l, 5l, 3l);

即使3号出现异常,4号和5号也被保存

我尝试用

设置插入方法
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

但它不适用于我的

你必须使用sessionContext.getBusinessObject (FacadeBeanLocal.class);
获取要应用的 bean 的另一个实例 REQUIRE_NEW.

@Stateless
public class FacadeBean implements FacadeBeanRemote {
    @PersistenceContext(unitName = "ejercicio7PU")
    private EntityManager em;
    @Resource
    private SessionContext sessionContext;

    private FacadeBeanLocal local;

    @PostConstruct
    void init() {
      local = sessionContext.getBusinessObject(FacadeBeanLocal.class);
     }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void insertar(final long id) {
        Record r = new Record();
        r.setId(id);
        em.persist(r);
    }

    @Override
    public void insertarTres(final long id1, final long id2, final long id3) {
        Logger.getLogger(FacadeBean.class.getName()).log(Level.INFO, "Insertando registros");
        local.insertar(id1);
        local.insertar(id2);
        local.insertar(id3);
    }

}