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這個問題,但沒有收到任何答案,所以我希望這裏有人能幫助我。