aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking without writeLock() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Locking without writeLock()" Watch "Locking without writeLock()" New topic
Author

Locking without writeLock()

Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
In Andrew's book there's usage of writeLock().lock() for persist method. Here in my case I think its not required. dataSectionOffset is always same(It is set in the beginning only once when the server starts). dbInfo instance stores this kind of data, like dataSectionOffset and recordLength. synchronization of dbFile (RandomAccessFile) I guess is enough for this function. Let me know if I am wrong..!!???

<code>


long dataSectionOffset = dbInfo.getDataOffset();
StringBuffer padding = new StringBuffer();
StringBuffer content = new StringBuffer();

for(int fieldIndex = 0 ; fieldIndex < dbInfo.getNumberOfFields(); fieldIndex++){
int paddingLength = dbInfo.getFieldLength(fieldIndex) - data[fieldIndex].length();
padding.setLength(paddingLength);
content.append(data[fieldIndex]+padding);
}

dbFile.seek(dataSectionOffset);
long recordLength = dbInfo.getRecordLength();

synchronized(dbFile){
if(create==true)
dbFile.seek(dbFile.length());
else
dbFile.seek((recNo-1)*recordLength);
dbFile.write(content.toString().getBytes());
}
}
</code>


Animesh Saxena<br /> <br />Open Source Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locking without writeLock()