• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Locking without writeLock()

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic