package examples.jdbc.oracle;

import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import weblogic.common.*;

/**
 * This example demonstrates how the WebLogic extension to <tt>Connection
 * waitOnResources()</tt> can be used to return control to the program
 * when the current operation would otherwise wait (in this case
 * indefinitely) for locked DBMS resources.
 * <p>
 * This example is run on the command line and is output to
 * System.out.
 *
 *The <font face="Courier New" size=-1>waitOnResources()</font> method
 *is not supported when using the Oracle 8 API. Therefore, <b>this
 *example will not run when using the Oracle 8 API</b>. For more
 *information on using Oracle API verisions, see <a
 *href="../../../install/install_jdbc.html#client">Setting
 *your path and client libraries</a> in the WebLogic jDriver for Oracle
 *installation guide.
 *
 *
 *<h3>To set up this example:</h3>
 *<ol>
 *<li>Set up your development shell as described in <a
 *href=../../../techstart/environment.html>Setting up your environment</a>.
 *<p>
 *<li> Change connection parameters to correspond to your Oracle configuration.
 *
 *<p>
 *
 *<li>Compile this example by executing the following command in your development shell:
 * If you need more help, check the section on connecting
 *to a database in the Developers Guide, <a
 *href=../../../classdocs/API_joci.html>Using WebLogic jDriver for Oracle</a>.
 *<p>
 *
 *<font face="Courier New"><b>$ javac -d %CLIENT_CLASSES% waiton.java</b></font>
 *<p>	
 *
 *<li>Run this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ java examples.jdbc.oracle.waiton</b></font>
 *
 *</ol> *
 * @author Copyright (c) 1997-2000 by BEA Systems, Inc.  All Rights Reserved.
 */
public class waiton {
  
  /**
   * Runs this example from the command line.
   */
  public static void main(String argv[]) throws Exception  {
    weblogic.jdbc.oci.Connection con1 = null;
    weblogic.jdbc.oci.Connection con2 = null;
    Statement stmt1 = null;
    Statement stmt2 = null;
    
    Driver myDriver = null;
    try {
      java.util.Properties props = new java.util.Properties();
      props.put("user",     "scott");
      props.put("password", "tiger");
      props.put("server",   "DEMO");
      
      myDriver = (Driver) Class.forName("weblogic.jdbc.oci.Driver").newInstance();
      
      // The explicit casts are needed to make the WebLogic extensions available
      con1 = (weblogic.jdbc.oci.Connection)myDriver.connect("jdbc:weblogic:oracle", props);
      con1.setAutoCommit(false);
      con1.waitOnResources(false);
      
      con2 = (weblogic.jdbc.oci.Connection)myDriver.connect("jdbc:weblogic:oracle", props);
      con2.setAutoCommit(false);
      con2.waitOnResources(false);
      
      stmt1 = con1.createStatement();
      stmt2 = con2.createStatement();
      
      try {
        stmt1.execute("create table waiton1 ( argument int )");
        stmt1.execute("create table waiton2 ( argument int )");
      }
      catch (SQLException e) {}
      
      stmt1.execute("insert into waiton1 values( 1 )");
      stmt1.execute("insert into waiton2 values( 1 )");
      con1.commit();
      
      stmt1.execute("update waiton1 set argument = 1");  // lock up waiton1
      stmt2.execute("update waiton2 set argument = 2");  // lock up waiton2
      
      try {
        stmt1.execute("update waiton2 set argument = 1");  // provoke hang without waitOnResources()
      }
      catch (SQLException e) {
        System.out.println("\nIf the following exception mentions "
                           + "ORA-00054: resource busy...\n"
                           + "then the feature has been demonstrated.\n");
        e.printStackTrace();
        stmt1.execute("drop table waiton1");
        stmt2.execute("drop table waiton2");
      }
    }
    catch (Exception e) { e.printStackTrace(); }
    finally {
      try {con1.close(); con2.close();}    catch (Exception e) {;}
      System.out.println("We're done.");
    }
  }
}

