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;
}
}