I tried to use JRun transaction service from the servlets, without EJB.
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[i];
OrderLine orderLine =
orderLineDAO.addOrderLine(order,product,qtyProduct[i]);
}
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/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists