FileDocCategorySizeDatePackage
OrderHandlerPool.javaAPI DocExample1650Tue Jan 25 10:45:14 GMT 2000None

OrderHandlerPool

public class OrderHandlerPool extends HttpServlet

Fields Summary
private ConnectionPool
pool
Constructors Summary
Methods Summary
private voidchargeCard()

 
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    Connection con = null;

    res.setContentType("text/plain");
    PrintWriter out = res.getWriter(); 

    try {
      con = pool.getConnection();

      // Turn on transactions
      con.setAutoCommit(false);

      Statement stmt = con.createStatement();
      stmt.executeUpdate(
        "UPDATE INVENTORY SET STOCK = (STOCK - 10) WHERE PRODUCTID = 7");
      stmt.executeUpdate(
        "UPDATE SHIPPING SET SHIPPED = (SHIPPED + 10) WHERE PRODUCTID = 7");

      chargeCard();  // method doesn't actually exist...

      con.commit();
      out.println("Order successful!  Thanks for your business!");
    }
    catch (Exception e) {
      // Any error is grounds for rollback
      try { 
        con.rollback();
      }
      catch (Exception ignored) { } 
      out.println("Order failed.  Please contact technical support.");
    }
    finally {
      if (con != null) pool.returnConnection(con);
    }
  
public voidinit(javax.servlet.ServletConfig config)

    super.init(config);
    try {
      pool = new ConnectionPool("jdbc:oracle:oci7:orders", "user", "passwd", 
                                "oracle.jdbc.driver.OracleDriver", 10, 5);
    }
    catch (Exception e) {
      throw new UnavailableException(this, "Couldn't create connection pool");
    }