多个用户访问 RDF 文件进行读写

multiple users accessing RDF file for read and write

我正在使用 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();

看看耶拿网站的Concurrency HowTo。按照 TDB/SDB 交易的相关链接。根据文档:

Locks provide critical section support for managing the interactions of multiple threads in the same JVM. Jena provides multiple-reader/single-writer concurrency support (MRSW).

The pattern general is:

Model model = . . . ;
model.enterCriticalSection(Lock.READ) ;  // or Lock.WRITE
try {
    ... perform actions on the model ...
    ... obey contract - no update operations if a read lock
} finally {
    model.leaveCriticalSection() ;
}

文件存储不提供正确的交易。

选项是:

  1. 使用TDB transactions -- 需要数据集
  2. 使用 Concurrency HowTo -- 适用于模型。
  3. 使用 DatasetGraphWithLock -- 提供基于锁的事务模拟(不完美 - 没有中止)。

如果锁定,请记住写入文件不是原子操作。写入过程中的崩溃会留下半个文件。写入一个文件,然后在同一目录中将其重命名为最终名称。