4
我想重写当前使用事务的代码。但是,根据Jena文档(http://incubator.apache.org/jena/documentation/tdb/tdb_transactions.html),不支持嵌套事务。Jena TDB:嵌套事务
比方说,我想查询数据库中的一些数据,并为每个找到的资源添加一个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这个问题,但没有收到任何答案,所以我希望这里有人能帮助我。