AIM:
To write a java
program for transactions and concurrency in database operating systems
ALGORITHM:
Step
1:
Start the program
Step
2:
Creation of
bindings to the bank account object
Step
3:
Investigate and implement the object store’s
concurrency options
Step
4:
The executions of transactions are all or none
Step
5:
The interleaving of
multiple transactions is serializable
Step
6:
.Update
is atomic
Step
7:
Start of
the application transaction
Step
8:
Method invocations; as a part of a given
invocation the object will be locked in read or write mode
Step
9:
Commit/abort
of the transaction
Step
10: End
of program.
PROGRAM:
server side:
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class server
{
public static void main(String args[])
{
try
{
ServerSocket s=new ServerSocket(7001);
while(true)
{
Socket incoming=s.accept();
Runnable r=new bankserver(incoming);
Thread t=new Thread(r);
t.start();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class bankserver implements Runnable
{
private Socket incoming;
public bankserver(Socket i)
{
incoming=i;
}
public void run()
{
try
{
try
{
DataOutputStream out=new
DataOutputStream(incoming.getOutputStream());
DataInputStream in=new
DataInputStream(incoming.getInputStream());
System.out.println(in.readInt());
while(true)
{
int choice=in.readInt();
System.out.println("You have selected
choice"+choice);
switch(choice)
{
case 1:
String name=null;
int acno1=0;
if(in.readBoolean())
{
name=in.readUTF();
acno1=in.readInt();
}
else
{
break;
}
int a=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String str1="insert into customer
values('"+name+"','"+acno1+"','"+a+"')";
st.executeUpdate(str1);
}
catch(Exception e)
{
System.out.println("Account Number is already
allocated");
}
break;
case 2:
int acno=in.readInt();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String s1="Select * from customer where
accnumber="+acno;
System.out.println(s1);
int bal=0;
ResultSet rs=st.executeQuery(s1);
boolean check=true;
String name2=null;
while(rs.next())
{
name2=rs.getString(1);
bal=rs.getInt(3);
}
if(name2==null)
{
check=false;
out.writeBoolean(check);
}
else
{
out.writeBoolean(check);
out.writeInt(bal);
}
}
catch(Exception e)
{
e.printStackTrace();
}
break;
case 3:
int acno3=in.readInt();
int amt3=in.readInt();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String s1="Select * from customer where
accnumber="+acno3;
System.out.println(s1);
int bal=0;
ResultSet rs=st.executeQuery(s1);
String name3=null;
boolean check3=true;
while(rs.next())
{
bal=rs.getInt(3);
name3=rs.getString(1);
}
if(name3== null)
{
check3=false;
}
boolean chstatus=false;
if(bal>0 && bal>amt3)
{
Object lock2=new Object();
synchronized(lock2)
{
int tmp=bal-amt3;
String s2="update customer set balance="+tmp+"
where accnumber="+acno3;
System.out.println(s2);
st.executeUpdate(s2);
chstatus=true;
}
}
else
{
System.out.println("Money not Ennough");
}
out.writeBoolean(check3);
if(check3==true)
{
out.writeBoolean(chstatus);
}
}
catch(Exception e)
{
e.printStackTrace();
}
break;
case 4:
int acno4=in.readInt();
int amt4=in.readInt();
System.out.println(acno4);
System.out.println(amt4);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String s1="Select * from customer where
accnumber="+acno4;
System.out.println(s1);
String name1=null;
ResultSet rs=st.executeQuery(s1);
while(rs.next())
{
name1=rs.getString(1);
}
System.out.println(name1);
boolean ch=false;
if(name1!=null)
{
System.out.println("hai");
int actmp=0;
ResultSet rs1=st.executeQuery(s1);
while(rs1.next())
{
actmp=rs1.getInt(3);
}
System.out.println(actmp);
Object lock1=new Object();
synchronized(lock1)
{
int tmpbal=actmp+amt4;
String s2="update customer set
balance="+tmpbal+" where accnumber="+acno4;
System.out.println(s2);
st.executeUpdate(s2);
}
ch=true;
out.writeBoolean(ch);
}
else
{
System.out.println("nO rECORD");
out.writeBoolean(ch);
}
}
catch(Exception e)
{
e.printStackTrace();
}
break;
case 5:
int tac1=in.readInt();
int tac2=in.readInt();
int tamt=in.readInt();
System.out.println(tac1+" "+tac2+" "+tamt);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:customer");
Statement st=con.createStatement();
String s1="Select * from customer where
accnumber="+tac1;
String s2="Select * from customer where
accnumber="+tac2;
System.out.println(s1);
System.out.println(s2);
ResultSet rs1=st.executeQuery(s1);
int bal1=0;
while(rs1.next())
{
bal1=rs1.getInt(3);
}
ResultSet rs2=st.executeQuery(s2);
int bal2=0;
while(rs2.next())
{
bal2=rs2.getInt(3);
}
if(bal1 < tamt)
{
}
else
{ Object
lock=new Object();
synchronized(lock)
{
bal1=bal1-tamt;
bal2=bal2+tamt;
String s3="update customer set balance="+bal1+"
where accnumber="+tac1;
String s4="update customer set
balance="+bal2+" where accnumber="+tac2;
st.executeUpdate(s3);
st.executeUpdate(s4);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
break;
case 6:
System.out.println("Client is exit");
break;
default:
System.out.println("Invalid Input from client");
break;
}
}
}
catch(Exception e)
{
System.out.println("Client is Disconnected");
}
finally
{
incoming.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
client side:
import java.io.*;
import java.net.*;
import java.util.*;
public class client
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",7001);
try
{
DataOutputStream out=new
DataOutputStream(s.getOutputStream());
DataInputStream in=new DataInputStream(s.getInputStream());
out.writeInt(25);
while(true)
{
System.out.println("=====================");
System.out.println("1.Create a new Account");
System.out.println("2.Check Balance");
System.out.println("3.Withdraw");
System.out.println("4.Deposit");
System.out.println("5.Transfer");
System.out.println("6.Exit");
System.out.println("======================");
Scanner sc=new Scanner(System.in);
int choice;
try
{
choice=sc.nextInt();
}
catch(Exception e)
{
System.out.println("Enter the Correct Input");
continue;
}
System.out.println("Enter your choice 1-6");
out.writeInt(choice);
switch(choice)
{
case 1:
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the Account Name:");
String name=br.readLine();
int acno1=0;
boolean b1=false;
try
{
System.out.print("Enter the Account Number:");
acno1=sc.nextInt();
b1=true;
}
catch(Exception e)
{
System.out.println("Enter the Account Number
Correctly");
}
out.writeBoolean(b1);
if(b1== true)
{
out.writeUTF(name);
out.writeInt(acno1);
}
break;
case 2:
System.out.print("Enter the Account Number:");
int acno=0;
boolean pach=false;
try
{
acno=sc.nextInt();
}
catch(Exception e)
{
System.out.println("Enter the Valid Account
Number");
pach=true;
}
int bal=0;
out.writeInt(acno);
if(in.readBoolean())
{
bal=in.readInt();
System.out.println("the Balance is:"+bal);
}
else
{
if(pach==false)
{
System.out.println("Account not available");
}
}
break;
case 3:
boolean check1=false;
int acno3=0;
int amt3=0;
try
{
System.out.print("Enter the Account Number:");
acno3=sc.nextInt();
System.out.print("Enter the Ammount:");
amt3=sc.nextInt();
}
catch(Exception e)
{
System.out.println("Invalid Input");
check1=true;
}
if(check1==false)
{
out.writeInt(acno3);
out.writeInt(amt3);
if(in.readBoolean())
{
if(in.readBoolean())
{
System.out.println("Money is Withdrawn");
}
else
{
System.out.println("Money is not Enough to
Withdraw");
}
}
else
{
System.out.println("Account is not available");
break;
}
}
break;
case 4:
System.out.print("Enter the Account Number:");
int acno4=sc.nextInt();
out.writeInt(acno4);
System.out.print("Enter the Ammount:");
int amt4=sc.nextInt();
out.writeInt(amt4);
if(in.readBoolean())
{
System.out.println("Ammount is Deposited");
}
else
{
System.out.println("Check the Account Number");
}
break;
case 5:
System.out.print("Enter the From Account:");
int tac1=sc.nextInt();
out.writeInt(tac1);
System.out.print("Enter the To Account:");
int tac2=sc.nextInt();
out.writeInt(tac2);
System.out.print("Enter the Amount to transfer:");
int tamt=sc.nextInt();
out.writeInt(tamt);
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid Input");
break;
}
}
}
finally
{
s.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment