gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:14:15 24 4
gpt4 key购买 nike

我使用 RDF 文件来存储由不同用户添加的有关各种主题的在线资源的链接 (URL)。

我正在 Apache 服务器上使用 Jena API 读取和写入 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() ;
}

关于java - 多个用户访问 RDF 文件进行读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com