users@glassfish.java.net

Transaction demarcation in client container

From: <glassfish_at_javadesktop.org>
Date: Tue, 20 Nov 2007 12:58:41 PST

Hello,

I am trying to understand how to use client-controlled transactions in a Java EE client container.

The first question, is that the Java EE 5 specs states that Java EE vendors DO NOT have to support transaction management in clients. But is it correct that Glassfish actually does? So, I should be able to manually start a transaction in my client container, then call EJBs and finally commit, right?

Here is my test setup:
1) I have created the application as an Enterprise Application in NetBeans 6, with an EJB, a Web and a Client module.
2) I have 2 persistent units, using 2 data sources (XA) on 2 different databases (MySQL)
3) I have a couple of entities, and one stateless session bean
4) in the client, I have one Main class, where I inject references to the userTransaction, to the persitenceContexts and to the session bean:

    @EJB
    private static OrderManagementRemote orderManagementBean;
    @Resource
    private static UserTransaction utx;
    @PersistenceUnit(unitName = "TransactionsSample-ejbPU")
    static EntityManagerFactory customersEmf;
    @PersistenceUnit(unitName = "TransactionsSample-ejbPU2")
    static EntityManagerFactory productsEmf;

5) I have a test method with the following code:

    private void test2() {
        try {
            utx.begin();
            orderManagementBean.createProduct("my test product", 1234, 5678);
            utx.commit();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

The problem is that when I run this code, the call to the session bean succeeds (a row is added into the DB). But then, the call to utx.commit() hangs...

I would like to know if the general approach is correct (usually, I would do demarcation on the server side, but I need to check the behavior for client-controlled transactions...)? If no, what's wrong? If yes, any idea what I could check?


In my session bean, I have the following code:

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void createProduct(String description, long price, int quantity) {
        em.joinTransaction(); // at first, I tried without this line... same behavior
        Product p = new Product();
        p.setDescription(description);
        p.setPrice(price);
        p.setAvailableQuantity(quantity);
        em.persist(p);
    }



Many thanks for any hint or feedback,

Cheers,

Olivier
[Message sent by forum member 'oliechti' (oliechti)]

http://forums.java.net/jive/thread.jspa?messageID=246385