HI I need some help on how to go about in
java program to design a Strict two phase locking schema. where the input file is given as notepad and input as follows through a notepad

as follows

b1;
r1(Y);
w1(Y);
r1(Z);
b2;
r2(Y);
b3;
r3(Z);
w1(Z);
e1;
w3(Z);
e3;
where b1 is beginning of first transaction, r1 is reading, W1 is writing, e1 is end of transaction for transaction 1.

This is the code which checks whether the operation is read , write or begin transaction or end transaction and act accordingly, here the algorithm which is implemented is one in which no two transaction cannot wait for access on same object if it does so. than the second in line will be aborted.

public static void beginExecution(String str)
    {
        /*
         * This function traverses through each input and accordingly executes the corresponding operation.
         * It reads the first character of each line and determines the operation. It reads the remaining characters
         * till it enounters a "(" to determine the transaction number.
         * The various operations performed are Begin b, Read r, Write w, Commit e. 
         */
        
        String releasedItems[]= null;
        char Op = str.charAt(0);
        
        try
        {
            switch(Op)
            {

                case 'b' : // Begin Transaction
                {
                    String tId = str.substring(1,str.indexOf(";"));
                    mainOpFile.println(str + "Transaction " + tId +" begins");
                    // Insert the new transaction into the Transaction table.
                    String sqlstmt = "Insert into Transactions(Transaction_Id,Data_Item,Transaction_State) Values("+tId+",'','Active');";
                    //opFile.println(sqlstmt);
                    int no_rows= stmt.executeUpdate(sqlstmt); // Execute Query
                    break;
                }

                case 'r': 
                {
                   readOperation(str); // Read Opeation is called
                   break;
                }
                
                case 'w':
                {
                    writeOperation(str); // Write operation is performed
                    break;
                }
                
                case 'e':
                {
                    int cnt=0;
                    String commitState = "";
                    String tId = str.substring(1,str.indexOf(";")); // Determine the transaction id
     
                    String getReleasedItems = "select Data_Item, Transaction_State from Transactions where Transaction_id = "+tId+";";
                    transactionResult = stmt.executeQuery(getReleasedItems);
                    while(transactionResult.next())
                    {
                        releasedItems = transactionResult.getString(1).split(","); // Get list of data items to be unlocked
                        commitState = transactionResult.getString(2); // Get the Transaction state 
                    }
                    if(commitState.equals("Active"))
                    {
                        cnt = getBlockedTransactionCount(); // get the number of blocked transactions in the table
                        // Change state of the current transaction to Committed from Active.
                        int commitResult = stmt.executeUpdate("Update Transactions set Transaction_state = 'Committed' where Transaction_id = " + tId +";");
                        mainOpFile.println(str + " T"+ tId + " committed;");
                        // Delete all the locks the transaction might be holding.
                        int releaseLocks = stmt.executeUpdate("delete from Locks where Transaction_id = " + tId +";");
                        if(cnt!=0)
                            unBlock(releasedItems); // If any item is blocked waiting for the current transaction to finish,
                                                    // they are unblocked and executed.
                    }
                    else if(!commitState.equals("Aborted")) // If the transaction is blocked
                    {
                        String fetchSeq="";
                        String sqlFetchSeq ="Select Sequence_Of_operations from Transactions where Transaction_Id ="+tId+" and Transaction_State='Blocked';";
                        //opFile.println("Select Sequence_Of_operations from Transactions where Transaction_Id ="+tId+" and Transaction_State='Blocked';");
                        ResultSet rs = stmt.executeQuery(sqlFetchSeq);
                        while (rs.next())
                        {
                            fetchSeq = rs.getString(1);
                        }
                        fetchSeq = fetchSeq + "," + str;
                        
                        // Append the operation to the sequence of operations in the Transaction table. 
                        
                        sqlFetchSeq = "Update Transactions set Sequence_Of_operations = '"+fetchSeq+"' where Transaction_Id ="+tId+" ;";
                        //opFile.println("Update Transactions set Sequence_Of_operations = '"+fetchSeq+"' where Transaction_Id ="+tId+" ;");
                        stmt.executeUpdate(sqlFetchSeq);
                        mainOpFile.println(str + " Appended to Sequence of Operations of " + tId );
                    }
                    else // If the transaction is already aborted
                    {
                        // Indicate to the user that the transaction is already aborted and the curent 
                        // operation cannot be executed.
                         mainOpFile.println(str + " Transaction T" + tId +" already Aborted."); 
                    }
                    break;
                }
                
                default: break;

            }
       }

This code is for cautious lock , Hope this will help guys out I wrote this code for my one of the 2PL project

public static void CWait(String , String )
    {
        
    String writeMsg = null;
    try {
            boolean aborted = false;
            boolean blocked = false;
            boolean blockedtrans = false;
            int numLocks = 0;
            String Lock_Trans_ID="";
            String itemAppend = "";
            String releasedItems[]=null;
            numLocks = checkLockTable(dataItem,'w');
            
            if(numLocks == 0) // If no transaction holds a write lock on the data item.
            {
                
                String setLock = "Insert into Locks values("+transID+",'"+dataItem+"','Write');";
                stmt.executeUpdate(setLock);
                
                writeMsg = str +" Item "+ dataItem +" write_locked by T"+transID;
                
             
                mainOpFile.println(writeMsg);
                
               
                String getdataItems = "select Data_Item from Transactions where Transaction_Id = "+transID+";";
                transactionResult = stmt.executeQuery(getdataItems);
                while(transactionResult.next())
                    itemAppend = transactionResult.getString(1);
                if(itemAppend.equals(""))
                    itemAppend = dataItem;
                else
                    itemAppend = itemAppend + ","+dataItem;
                
                String updateTrans = "update Transactions set Data_Item = '"+itemAppend+"' where Transaction_Id = "+transID+";";
                stmt.executeUpdate(updateTrans);
                "+itemAppend);
            }
            else if(numLocks == 1) // If there exists a transaction which already holds a lock on the data item
            {
                String getLocks = "select Transaction_Id from Locks where Item_lock = '"+dataItem+"';";
                lockResult = stmt.executeQuery(getLocks);
                while(lockResult.next())
                    Lock_Trans_ID = lockResult.getString(1); 
                
                if(transID.equals(Lock_Trans_ID)) // Upgrade the Lock to Write 
      
                           
                            blockingOrder++;
                            String newBlockedTrans = "Update Transactions set Transaction_State='Blocked', Order_No = "+blockingOrder+", Blocked_On = '"+dataItem+"', Sequence_Of_operations = '"+str+"' where Transaction_Id = "+transID+";";
                            stmt.executeUpdate(newBlockedTrans);
                           
                            mainOpFile.println(str + " T"+ transID + " Blocked");
                            
                           the current transaction.
                            ResultSet rs = stmt.executeQuery("Select Transaction_id from Locks where Item_Lock='" + dataItem + "' and Transaction_id <> "+transID+";");
                            
                            while (rs.next())
                            {
                              mainOpFile.println("     ;T"+transID+" waiting for "+dataItem+" for T" + rs.getString(1));
                            }
                            blocked = true;
                        
                        }
                    
                    if(aborted) // If the transaction is Aborted
                    {
                        // Get list of data items that are released by the current transaction.
                        
                        String getReleasedItems = "select Data_Item from Transactions where Transaction_Id = "+transID+";";
                        transactionResult = stmt.executeQuery(getReleasedItems);
                       
                        while(transactionResult.next())
                            releasedItems = transactionResult.getString(1).split(",");
                        unBlock(releasedItems);
                        
                    
                }
                }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
    }

Reading from the inputfile and parsing the input and puttting inside a table. The table is a Access database.

I guess this will be use ful for guys who are looking to get the input from the Inputfile and parsing it and storing it in the tables.

public static void readOperation(String str)
    {


        String Trans_State=""; 
        int no_rows =0;
        String releasedItems[]=null;

        try
        {
        String tId = str.substring(1,str.indexOf("("));


        String fetchState = "Select Transaction_State from Transactions where Transaction_id="+tId+";";

        transactionResult = stmt.executeQuery(fetchState);
            while (transactionResult.next())
            {
              Trans_State = transactionResult.getString(1);

            }

            if (Trans_State.equals ("Blocked")) // If the transaction is already blocked
            {
                String fetchSeq="";
                action.

                String sqlFetchSeq ="Select Sequence_Of_operations from Transactions where Transaction_Id ="+tId+" and Transaction_State='Blocked';";

                ResultSet rs = stmt.executeQuery(sqlFetchSeq);
                while (rs.next())
                {
                    fetchSeq = rs.getString(1);
                }
                fetchSeq = fetchSeq + "," + str; // Append the operation to the sequence of operations delimited by a ","


                sqlFetchSeq = "Update Transactions set Sequence_Of_operations = '"+fetchSeq+"' where Transaction_Id ="+tId+" ;";

                no_rows = stmt.executeUpdate(sqlFetchSeq);
                mainOpFile.println(str + " T" + tId +" Blocked ; Appending operation to Sequence of operations");
            }    
            else if (Trans_State.equals("Aborted")) // If the transaction state is "Aborted"
            {
                mainOpFile.println(str + " Aborted: Since Transaction " + tId + " is aborted"); 
                return;
            }
            else if (Trans_State.equals("Committed")) // If the transaction state is already "Committed"
            {

                return;
            }
            else if (Trans_State.equals("Active")) // If the transaction is "Active" 
            {

                String dataItem = str.substring(str.indexOf("(")+1,str.indexOf(")")); // Get the data item
                int count = checkLockTable(dataItem,'r'); // Check if any other transaction holds a write lock on the data item.

                if (count ==0) // If no transaction holds any write lock on the data item.
                {



                    no_rows = stmt.executeUpdate("Insert into Locks values("+tId+",'"+dataItem+"','Read');");
                    mainOpFile.println(str +" Item "+dataItem +" read_locked by T" + tId);
                    String fetchSeq="";


                    String sqlFetchSeq ="Select Data_Item from Transactions where Transaction_id ="+tId+";";

                    ResultSet rs = stmt.executeQuery(sqlFetchSeq);

                    while (rs.next())
                    {
                        fetchSeq = rs.getString(1);
                    }

                    if (fetchSeq.equals(""))
                    {
                        fetchSeq = dataItem;
                    }
                    else
                    {        
                    fetchSeq = fetchSeq +","+ dataItem;
                    }
                    sqlFetchSeq = "Update Transactions set Data_Item = '"+fetchSeq+"' where Transaction_id ="+tId+" ;";

                    no_rows = stmt.executeUpdate(sqlFetchSeq);

                }
                else 
                {

                    String tidlock="";
                    ResultSet rs = stmt.executeQuery("Select Transaction_Id from Locks where Item_Lock = '"+dataItem+"' and Lock_Type = 'Write';");



                    while(rs.next())
                        tidlock = rs.getString(1);


                    lockResult = stmt.executeQuery("select Transaction_id, Transaction_State from Transactions where Transaction_Id = "+tidlock+";");
                    while(lockResult.next())
                    {
                        Trans_State = lockResult.getString(2);
                        String transaction_id_lock=lockResult.getString(1);
                        if (Trans_State.equals("Blocked") ) // If the transaction holding the lock is Blocked
                        {



                            no_rows = stmt.executeUpdate("Update Transactions set Transaction_State='Aborted', Blocked_On='', Sequence_Of_operations='', Order_No=0 where Transaction_id = " + tId + ";");



                            mainOpFile.println(str + " T"+ tId + " Aborted");


                            no_rows = stmt.executeUpdate("Delete from Locks where Transaction_Id =" + tId);    
                            //opFile.println("Delete from Locks where Transaction_Id =" + tId);

                            // Get a list of released data items of the aborted transaction.
                            String getReleasedItems = "select Data_Item from Transactions where Transaction_Id = "+tId+";";
                            //opFile.println("select Data_Item from Transactions where Transaction_Id = "+tId+";");

                            transactionResult = stmt.executeQuery(getReleasedItems);
                            //opFile.println("Releasing Operations pending for T " + tId);
                            while(transactionResult.next())
                                releasedItems = transactionResult.getString(1).split(",");
                            unBlock(releasedItems);    // Unblock the released data items and transactions. 
                        }
                       else
                       {


                            blockingOrder++; // Increment the blocking order
                            stmt.executeUpdate("Update Transactions set Transaction_State='Blocked', Order_No = "+blockingOrder+", Blocked_On = '"+dataItem+"', Sequence_Of_operations = '"+str+"' where Transaction_Id = "+tId +";");
                            mainOpFile.println(str + " T"+ tId + " Blocked");

                            ResultSet rs1 = stmt.executeQuery("Select Transaction_id from Locks where Item_Lock='" + dataItem + "' and Transaction_id <> "+tId+";");

                            while (rs1.next())
                            {
                                mainOpFile.println("     ;T"+tId+" waiting for "+dataItem+" for T" + rs1.getString(1));

                            }


                       }    
                    }
                }   
            }
        }


    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.