Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
//Java Program to implement 2 phase locking protocol. TwoPhaseLocking.java package twopahselocking; public class TwoPahseLocking { public static void main(String[] args) throws InterruptedException { System.out.println("+++++2 Phase Locking Protocol+++++"); Thread t1 = new Thread(new Runner()); Thread t2 = new Thread(new Runner()); Thread t3 = new Thread(new Runner()); t1.setName("Transaction-1"); t2.setName("Transaction-2"); t3.setName("Transaction-3"); System.out.println("Starting the Transactions~!~!~!"); t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); Thread.sleep(20000); System.out.println("All transactions are executed successfully!!!"); } } Runner.java package twopahselocking; import static java.lang.Thread.sleep; import java.util.logging.Level; import java.util.logging.Logger; public class Runner implements Runnable { public static int dbloc=0; public static String str,str1; public void run() { test(); } public synchronized void test() { str = Thread.currentThread().getName(); if(str.equals(Thread.currentThread().getName()) && dbloc==0) { str1=Thread.currentThread().getName(); System.out.println("\n"+str1+" is started!!!"); dbloc=1; System.out.println(str1+ " is acquired lock"); TransactionPerform obj = new TransactionPerform(); System.out.println(str1+" is going to perform its transaction"); obj.perfTrans(); try { sleep(5000); } catch (InterruptedException ex) { Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex); } dbloc=0; System.out.println(str1+" is completed its execution!!!"); System.out.println("Notifying other transaction to start again"); } else { System.out.println(str+" is waiting for its turn."); try { sleep(2000); test(); } catch (InterruptedException ex) { Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex); } } } } TransactionPerform.java package twopahselocking; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class TransactionPerform { public static int querryNum=1; String str; public static String querry1 = "Insert into Staff values('S128','Kusum Gupta','Mumbai','F','1992-02-13','8108146033','Nursary course','Nurse')"; public static String querry2 = "Update Staff Set Name='Mungesh Babulal' where Staff_ID='S114'"; public static String querry3 = "Update Staff Set Contact_no='8108146033' where Staff_ID='S117'"; int numRowsChanged; public synchronized void perfTrans() { try { //Create statement //Get Connection Connection myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/HMS", "root", ""); //Create statement System.out.println("\nDatabase connection is created"); Statement myStm = myCon.createStatement(); //Execute querry //ResultSet myRs = myStm.executeUpdate(q); if(querryNum==1) numRowsChanged = myStm.executeUpdate(querry1); else if(querryNum==2) numRowsChanged = myStm.executeUpdate(querry2); else if(querryNum==3) numRowsChanged = myStm.executeUpdate(querry3); querryNum++; //Process result if (numRowsChanged >= 1) { System.out.println("Table Staff Updated Successfully!!!"); } else { System.out.println("Table Staff does not Updated Successfully!!!"); } } catch (Exception e) { } } } Output:- run: +++++2 Phase Locking Protocol+++++ Starting the Transactions~!~!~! Transaction-1 is started!!! Transaction-1 is acquired lock Transaction-2 is waiting for its turn. Transaction-1 is going to perform its transaction Transaction-3 is waiting for its turn. Database connection is created Table Staff Updated Successfully!!! Transaction-2 is waiting for its turn. Transaction-3 is waiting for its turn. Transaction-2 is waiting for its turn. Transaction-3 is waiting for its turn. Transaction-1 is completed its execution!!! Notifying other transaction to start again Transaction-2 is started!!! Transaction-2 is acquired lock Transaction-2 is going to perform its transaction Transaction-3 is waiting for its turn. Database connection is created Table Staff Updated Successfully!!! Transaction-3 is waiting for its turn. Transaction-3 is waiting for its turn. Transaction-2 is completed its execution!!! Notifying other transaction to start again Transaction-3 is started!!! Transaction-3 is acquired lock Transaction-3 is going to perform its transaction Database connection is created Table Staff Updated Successfully!!! Transaction-3 is completed its execution!!! Notifying other transaction to start again All transactions are executed successfully!!! BUILD SUCCESSFUL (total time: 37 seconds)