jeudi 28 mai 2015

BMT in client-side classes?

I have a 3-layer application with a client-container, EJB-container (business-layer and data-access-layer). I want to make transactions in my client by using BMT. My EJBs on the EJB-container are all working with CMT.

But I am not sure if that actually works, since the client is not working in an EJB-container. The client injects the EJBs of the EJB-container with lookups on a Context object, which seem to work fine. I declared my client-side class as:

@Singleton
@TransactionManagement(TransactionManagementType.BEAN)

Is this correct? I guess the client side class which is used by my GUI has to be a singleton.

I have these objects:

private Context jndiCntx;
private UserTransaction tx;

@PostConstruct
public void initialize() throws Exception {
    jndiCntx = new InitialContext();
    tx = (UserTransaction) jndiCntx.lookup("java:comp/UserTransaction");
    tx.begin();
}

And this PostConstruct initializes the transaction. Here is where I get a NullPointerException (in commitOrder()):

@Override
public void commitOrder() throws ApplicationException {
    try {
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        throw new ApplicationException("", "commitOrder() failed!");
    }
}

@Override
public void cancelOrder() throws ApplicationException {
    try {
        tx.rollback();
    } catch (Exception e) {
        throw new ApplicationException("", "cancelorder() failed!");
    }
}

So it seems like that I don't get a UserTransaction object. I am slightly confused by the different ways of using contexts and transactions. What am I doing wrong? And by the way: What is the difference between using a Transaction with a TransactionManager and using a UserTransaction?

Aucun commentaire:

Enregistrer un commentaire