【发布时间】:2019-07-26 16:52:03
【问题描述】:
我正在使用 RDF 文件来存储由不同用户添加的关于各种主题的在线资源的链接 (URL)。
我正在使用 Jena API 在 Apache 服务器上读取和写入 RDF 文件。
我担心多个用户会同时登录系统并可能同时与文件交互。
我想知道这是否会导致更新文件时出现任何问题,例如,它会以某种方式损坏文件。对于实时应用程序,我可以继续执行此操作吗,或者由于多个用户同时访问 RDF 文件以进行读取和写入,它会导致我的应用程序崩溃。
非常感谢您的帮助。
谢谢
赛义德
//updated code to understand answer.
// Example of Locks for reading
File f = new File(fileName);
InputStream in = new FileInputStream(f);
Model model = ModelFactory.createDefaultModel();
model.read(in,null);
String queryString = "...";
model.enterCriticalSection(Lock.READ); // use of lock
try {
qe = QueryExecutionFactory.create(qry, model);
rs = qe.execSelect();
for ( ; rs.hasNext() ; )
{
//read literals
//read literals
out.println(....);
}
qe.close();
} finally
{
model.leaveCriticalSection() ;
}
//******************************
// Example of Locks for WRITING
File fout = new File(fileName);
Model model = ModelFactory.createDefaultModel();
model.read(in,null);
OutputStream os = new FileOutputStream(fout);
// model updation
// new triplets. new data being added
model.enterCriticalSection(Lock.WRITE); // use of lock
try {
model.write(os);
} finally
{
model.leaveCriticalSection() ;
}
os.close();
【问题讨论】: