/* * COM1317 Project * Transaction.java * Copyright (C) Jing Shan 2003 */ import java.io.*; import java.util.*; class Transaction extends Thread{ private int id; private LockManager lockmnger; private LinkedList lockList; Transaction (int i, LockManager l){ id = i; lockmnger = l; lockList = new LinkedList(); } public void addLock(LCB l){ lockList.addLast(l); } public void printLockList(){ System.out.println("Transaction "+ id+" has "+lockList.size()+" locks in lock list"); } public LinkedList getLockList(){ return lockList; } public void run(){ System.out.println("Transaction "+id+" is started"); System.out.println(); try{ File myFile = new File(id+".txt"); FileReader in =new FileReader(myFile); BufferedReader tmpBufReader = new BufferedReader(in); String oneLine = tmpBufReader.readLine(); while (oneLine != null){ StringTokenizer s = new StringTokenizer(oneLine); if(s.countTokens() != 2){ System.out.println("illegal command in "+id+".txt:: "+ oneLine); return; } try{ lockmnger.lock(this,s.nextToken(), s.nextToken()); }catch (DeadLockException e){ System.out.println(e); System.out.println("transaction "+ id+" aborts"); lockmnger.unlockAll(this); return; } oneLine = tmpBufReader.readLine(); } }catch (IOException e){ System.out.println("illegal file operations: "+e); } } public int id(){ return id; } }