package com.dynamic121.util.jdbc ;
/*
 *  This program demonstrates a readlock inside a transaction
 *  with 2 connections to a single BDB table
 *  Tested with the JDBC driver : mm.mysql-2.0.4-bin.jar
 *  Julien REVEL
 */

import java.sql.*;
import java.lang.Thread ;

public class MySQL_BDB_ReadLock
{

    private Connection Conn = null;
    private Connection Conn2 = null;

    public static void main(String[] Args)
    {
      String DriverName = "";
      String DBUrl = "";
      boolean initialize_dataset = false;

      for (int i = 0; i < Args.length; i++) {
        if (Args[i].equals("-driver")) {
            if (i + 1 < Args.length) {
                i++;
                DriverName = Args[i];
            }
        }
        else if (Args[i].equals("-url")) {
            if (i + 1 < Args.length) {
                i++;
                DBUrl = Args[i];
            }
        }
      }

      if (DriverName.length() == 0 || DBUrl.length() == 0) {
        System.out.println("usage: java JDBCBench -driver [driver_class_name] -url [url_to_db] ");
        System.exit(-1);
      }

      System.out.println("*********************************************************");
      System.out.println("* JDBCBench v1.0                                        *");
      System.out.println("*********************************************************");
      System.out.println();
      System.out.println("Driver: " + DriverName);
      System.out.println("URL:" + DBUrl);
      System.out.println();
      System.out.println();

      try {
        Class.forName(DriverName);
        Connection C = DriverManager.getConnection(DBUrl);
        Connection C2 = DriverManager.getConnection(DBUrl);

        MySQL_BDB_ReadLock Me = new MySQL_BDB_ReadLock(C, C2);
      }
      catch (Exception E) {
        System.out.println(E.getMessage());
        E.printStackTrace();
      }
    }

    public MySQL_BDB_ReadLock(Connection C, Connection C2)
    {
        try
        {
          Conn = C;
          Conn2= C2 ;
          createDatabase();
        }
        catch (Exception E) {
            System.out.println(E.getMessage());
            E.printStackTrace();
        }
    }

    /*
     *  createDatabase() - Creates and Initializes a scaled database.
     */

    void createDatabase() throws Exception
    {
      int iIsolation1, iIsolation2, iIsolation3 ;
      ResultSet rs ;

        try {
            Statement Stmt = Conn.createStatement();
            Statement Stmt2 = Conn2.createStatement();

            String    sQuery ;

            try
            {
              sQuery = "DROP TABLE tellers" ;
              Stmt.executeUpdate(sQuery);
            }
            catch (Exception e )
            {
            }

            // Create table
            sQuery = "CREATE TABLE tellers ( ";
            sQuery+= "Tid         INT NOT NULL, PRIMARY KEY(Tid),";
            sQuery+= "Bid         INT,";
            sQuery+= "Tbalance    INT)";
            sQuery+="type=BDB" ;

            // This code if for Oracle.
/*
            sQuery = "CREATE TABLE tellers ( ";
            sQuery+= "Tid         INT,";
            sQuery+= "Bid         INT,";
            sQuery+= "Tbalance    INT)";
*/

            Stmt.executeUpdate(sQuery);
            Stmt.clearWarnings();

            // Check isolation level on connexions, with me, always = 8
            iIsolation1 = Conn.getTransactionIsolation() ;
            iIsolation2 = Conn2.getTransactionIsolation() ;

            // Try to change isolation level. Does not work
            sQuery = "Set transaction isolation level READ UNCOMMITTED" ;
            Stmt.executeQuery(sQuery);
            // Always returns 8 !
            iIsolation1 = Conn.getTransactionIsolation() ;

            // Try to change isolation level. Does not work
            sQuery = "Set transaction isolation level READ UNCOMMITTED" ;
            Stmt2.executeQuery(sQuery);
            // Always returns 8 !
            iIsolation2 = Conn2.getTransactionIsolation() ;

            Conn.setAutoCommit(false);

            // Insert on first connexion
            sQuery = "INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (10,20,30)";
            Stmt.executeUpdate(sQuery);

            // Read on second connexion
            sQuery = "select * from tellers" ;
            // The program hangs on the next statement
            rs = Stmt2.executeQuery(sQuery);

            // I never reach the following line.
            Conn.commit();
        }
        catch (Exception E)
        {
            System.out.println(E.getMessage());
            E.printStackTrace();
        }

    }
}


