HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java Servlets: Part 2 Design Practices"
Volume: 5 Issue: 3, p.76

	

Listing 1: Connection Object as an Instance Variable
    
public MyServlet extends HttpServlet {
  private Connection connection;
  public init(ServletConfig config) {
    // Get database details from config
    ...
    // Create a connection object
    connection = ...
  }

  public void service(HttpServletRequest req, HttpServletResponse res) {
    // Use the connection object for database access
    ...
  }
}



Listing 2: Connection Object- One Transaction or Two Transactions?

import java.sql.*;
class Transaction {
  private Connection conn;
  Transaction() {
    String dburl = "jdbc:odbc:Test";
    String user = "scott";
    String password = "tiger";
    try {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       conn = DriverManager.getConnection(dburl, user, password);
       System.out.println("Connection created.");
    } catch(Exception e) { e.printStackTrace(); }
  }
  void transact(int action) {
    Statement stmt = conn.createStatement();
    try {
      conn.setAutoCommit(false);
      switch (action) {
      case 1 :
        stmt.executeUpdate("insert into dept val-
        ues(41,'100','100')");
        System.out.println("1");
        try { Thread.sleep(5); catch(Exception e) {}
        stmt.executeUpdate("insert into dept val-
        ues(51,'101','101')");
        System.out.println("11");
        stmt.executeUpdate("insert into dept val-
        ues(62,'102','102')");
        System.out.println("111");
        try { Thread.sleep(2); } catch(Exception e) {}
        conn.commit();
        System.out.println("1 commited");
        break;
      case 2 :
        stmt = conn.createStatement();
        stmt.executeUpdate("update dept set dname='SomeAc-
        counting' where deptno=10");
        System.out.println("2");
        stmt.executeUpdate("update dept set dname='SomeAc-
        counting' where deptno=20");
        System.out.println("22");
        try { Thread.sleep(5); } catch(Exception e) {}
        conn.rollback();
        System.out.println("2 rolled back");
        stmt.executeUpdate("update dept set loc='Bangalore'  
        where deptno=10");
        System.out.println("222");
        break;
      default :
        break;
      }
    } catch(Exception e) { e.printStackTrace(); }
  }
}

public class TestThread extends Thread {
  Transaction t;
  int action;
  public TestThread(Transaction tThread, int action) {
    t = tThread;
    this.action = action;
  }
  public void run() {
    t.transact(action);
  }
  public static void main(String s[]) {
    Transaction t = new Transaction();
    TestThread t1 = new TestThread(t, 1);
    TestThread t2 = new TestThread(t, 2);
    t1.start(); 
    t2.start();
  }
}





 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.