我想重写我当前的代码来使用事务。但是,根据耶拿文档 (http://incubator.apache.org/jena/documentation/tdb/tdb_transactions.html) 不支持嵌套事务。
假设,我想从数据库中查询一些数据,并为找到的每个资源添加一个 rdfs:label。我是否必须像下面的代码中那样严格分离读取和编写代码,或者是否有更有效的方法来实现此示例?
Dataset dataset = ...;
dataset.begin(ReadWrite.READ);
ArrayList<Resource> res = new ArrayList<Resource>();
try{
QueryExecution qe = QueryExecutionFactory.create("SELECT ?x WHERE { ?x a <Whatever> . }", dataset);
ResultSet rs = qe.execSelect();
try
{
while(rs.hasNext())
{
QuerySolution s = rs.nextSolution();
RDFNode node = s.get("x");
if(node.isResource) res.add(node.asResource());
}
}finally{ qe.close(); }
}finally{ dataset.end(); }
dataset.begin(ReadWrite.WRITE);
try{
Property label = model.getProperty("http://www.w3.org/2000/01/rdf-schema#label");
for(Resource r : res)
{
r.addProperty(label, "text");
}
dataset.commit();
}finally{ dataset.end(); }
我已经在 semanticweb.com 上发布了这个问题,但没有得到任何答案,所以我希望这里有人可以帮助我。
确实,TDB 不支持嵌套事务,但是您可以在 WRITE 事务中进行任意数量的读取。因此,启动一个 ReadWrite.WRITE 事务并在那里完成所有处理。无需嵌套事务即可执行您想要执行的操作。
有关TDB交易支持的更多信息,请查看此处的官方文档:
- http://incubator.apache.org/jena/documentation/tdb/tdb_transactions.html