Well, Here is the Code for the program to Implement 2PC Protocol using Java.
I am able to compile the program, But when i try to execute (using -Xlint) i am unable to get the output, as some Exception Bound Errors are arising. I am confused if i am Executing in a wrong way ? or I am here with the wrong code ?
Client Program
import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.JList; import javax.swing.event.*; import java.util.*; class Client extends Frame implements ActionListener{ Button b1,b2,b3,b4,b5; Panel p1,p2; TextField t1; Label l1; ServerSocket ss; Socket s; DataOutputStream output; DataInputStream input; Connection con; Statement stmt; String prepareStatus=""; static int port; Client(){ b1=new Button("Prepared"); b2=new Button("NotPrepared"); b3=new Button("Commit"); b4=new Button("Execute"); b5=new Button("Exit"); t1=new TextField("",35); l1=new Label("SQL"); p1=new Panel(); p2=new Panel(); p1.setLayout(new FlowLayout()); p1.add(l1); p1.add(t1); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(b4); p2.add(b5); add(p1); add(p2,"South"); setSize(300,300); setVisible(true); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con= DriverManager.getConnection("jdbc:odbc:2pc"); stmt = con.createStatement(); con.setAutoCommit(false); } catch (Exception e1) { } } public void actionPerformed(ActionEvent ae){ try { String str=ae.getActionCommand(); if(str.equals("Execute")){ String query = t1.getText(); System.out.println(stmt.executeUpdate(query)); t1.setText("Executed"); System.out.println(prepareStatus); } if(str.equals("Prepared")){ output.writeUTF("Prepared");//****** prepareStatus=input.readUTF();//***** t1.setText(prepareStatus); System.out.println(prepareStatus); } if(str.equals("NotPrepared")){ output.writeUTF("NotPrepared");//**** System.out.println(prepareStatus); } if(str.equals("Commit")){ if(prepareStatus.equals("Commit")){ con.commit(); t1.setText("commit Complete"); } else t1.setText("commit not Complete"); } if(str.equals("Exit")){ System.exit(0); stmt.close(); con.close(); } } catch(Exception e){ JLabel errorFields = new JLabel("<HTML><FONT COLOR = BLUE>TABLE LOCKED.</FONT></HTML>"); JOptionPane.showMessageDialog(null,errorFields); //e.printStackTrace(); } } public void runServer(int port){ try{ ss=new ServerSocket(port); s=ss.accept(); output=new DataOutputStream(s.getOutputStream()); input=new DataInputStream(s.getInputStream()); System.out.println("ready:"); } catch(Exception e){ e.printStackTrace(); } } public static void main(String args[]) { Client cl=new Client(); port=Integer.parseInt(args[0]); cl.runServer(port); } }
Server Program
import java.io.*; import java.net.*; class Server { public static void main(String args[]) { try { Socket s1=new Socket(InetAddress.getLocalHost(),1902); DataInputStream input1=new DataInputStream(s1.getInputStream()); DataOutputStream output1=new DataOutputStream(s1.getOutputStream()); Socket s2=new Socket(InetAddress.getLocalHost(),1602); DataInputStream input2=new DataInputStream(s2.getInputStream()); DataOutputStream output2=new DataOutputStream(s2.getOutputStream()); while (true){ String rs1=input1.readUTF(); String rs2=input2.readUTF(); //System.out.println(rs1); if((rs1.equals("Prepared")) &&(rs2.equals("Prepared"))) { output1.writeUTF("Commit"); output2.writeUTF("Commit"); } else { output1.writeUTF("wait for others to prepare."); output2.writeUTF("wait for others to prepare."); } } } catch(Exception e1) { e1.printStackTrace(); } } }