Hi all,

This is an old issue. I posted it around July last year, but I still got NO
answer. It's really really frustrating.

I tried to use JRun Transaction service from the servlets, _without_ EJB.
Please note the 'without' word, because everytime I post this issue,
somebody just point me to Knowledge Base Article number 21556 about using
Transaction service with session EJB. I know that article and it is not
related to my problem.

I use JRun 3.1 and Oracle database. My application uses DAO object for the
communication to the database.

================================================================
Assume that DAOException is a class that extends Exception. Assume also that
I have value-object Order, OrderLine, and Product.

public class CreateOrderServlet extends HttpServlet {
..

public void doPost(HttpServletRequest req, HttpServletResponse res) {
  Product productToOrder[] = null;
  int qtyProduct[] = null;
  // get request parameters...
  // process the info about the products being ordered,
  // and the quantity from the request parameters...

  OrderDAO orderDAO = ...; // instantiate OrderDAO object
  OrderLineDAO orderLineDAO = ...; // instantiate OrderLineDAO object
  UserTransaction tx = ...; // instantiate UserTransaction object
  // bound in the name space

  try {
    try {
      tx.begin();
      Order order = orderDAO.createNewOrder(...);
      for (int i = 0; i < productToOrder.length; i++) {
        Product product = productToOrder;
        OrderLine orderLine =
orderLineDAO.addOrderLine(order,product,qtyProduct);
      }
      tx.commit();
    } catch (DAOException daox) {
      tx.rollback();
    }
  } catch (Exception x) {
    // system-exception handling...
  }

  // write response...
}

..
}

public abstract DAOBase {

protected Connection getConnection() {
// get the connection from data source object bound in
// name space...
}

}

public class OrderDAO extends DAOBase {
..

public Order createNewOrder(String orderId) throw DAOException {
  Connection connection = null;
  Statement statement = null;

  String qry = "..."; // adding a new record

  try {
    connection = this.getConnection();
    statement = connection.createStatement();
    statement.executeUpdate(qry);

    Order order = new Order(orderId);
    return order;
  } catch (SQLException sqlx) {
    throw new DAOException(sqlx);
  } finally {
    // close statement and connection
  }
}

..
}

public class OrderLineDAO extends DAOBase {
..

public OrderLine addOrderLine(Order order, Product product, int qty) {
  Connection connection = null;
  Statement statement = null;

  String qry = "..."; // adding a new record

  try {
    connection = this.getConnection();
    statement = connection.createStatement();
    statement.executeUpdate(qry);

    OrderLine orderLine = new OrderLine(order,product,qty);
    return orderLine;
  } catch (SQLException sqlx) {
    throw new DAOException(sqlx);
  } finally {
    // close statement and connection
  }
}

..
}
=============================================================

I put a trap on the second order line addition.
This is what happens when I try to create a new order containing 3 order
lines:
1. a new record is created in order table.
2. a new record is created in order line table (first order line)
3. the second order line addition is failed to be added, a DAOException is
thrown
4. Transaction is supposed to be rolled back.

when I check the database, the new order record and the first order line are
not rolled back (the data exists).

Is there anything wrong with the codes? or,
am I missing something in the configuration?

Any help will be greatly appreciated!
Budi

 
 

______________________________________________________________________
This list and all House of Fusion resources hosted by CFHosting.com. The place for 
dependable ColdFusion Hosting.
Archives: http://www.mail-archive.com/jrun-talk@houseoffusion.com/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to