Budi, 

You need to double check with Alliare support on this, but if my memory
serves me correctly, you cannot use the JRUN Transaction object unless you
have EJB's enabled on your version of JRUN Server (in my case, we purchased
a lower end version that does NOT have EJB's enabled).  It was my
understanding that the JRUN Transaction tag and the underlying object were
both EJB's in of themselves, and NOT Java servlets.  If you want to use
them, the JRUN documentation goes through the process of setting up the
Transaction tag and recompiling the EJB on your local machine.  It is my
understanding that you can not use the Transaction tag and it's underlying
EJB as is out of the box, you have to set it up first.

For our web site, I use the transaction object that comes packaged in my
JDBC driver directly in a servlet class, instead of using JRUN's transaction
object/tag.  But that's only when absolutely necessary.  For most of my
updates/inserts to our SQLServer 2000 database, I used stored procedures and
the JRUN stored procedure tag, to pass in my data for updating/insertion.
Within the stored procedure itself, I double check my return parameter to
verify that an insert of a new record was successful, and roll back the
transaction as part of the stored procedure code if the insert was not
successful.  If I do a rollback in the stored procedure, I return a (-111)
to my servlet, and handle the error from there.

I don't know if this information helps, but I would double check with the
Allaire technical staff and verify that JRUN Transaction object itself is an
EJB, and if so, point you to the documentation for setting up the
transaction tag/EJB for your system.  Again, it is my understanding that the
Transaction object packaged with JRUN Server will only function if your JRUN
Server version has EJB's enabled.

Celeste 

-----Original Message-----
From: Budi Prawira [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 09, 2002 10:36 PM
To: JRun-Talk
Subject: Using Transaction in Servlet


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

 
 


______________________________________________________________________
Your ad could be here. Monies from ads go to support these lists and provide more 
resources for the community. http://www.fusionauthority.com/ads.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to