我正在使用Jena API在Jena TDB中插入和更新三元组。我的设计是这样的,每个插入操作都在事务控制中。例如:
dataset.begin (ReadWrite.WRITE)
try
{
// 1st insert operation
dataset.commit()
} finally {
dataset.end();
}
dataset.begin (ReadWrite.WRITE)
try
{
// 2nd insert operation
dataset.commit()
} finally {
dataset.end();
}
在此之后,当我使用READ事务查询TDB时,我注意到一些条目(主题,谓词或对象)是空的。为什么它会以这种方式运行,即使我没有使用嵌套事务?
public class UpdateTDB2 {
String directory = "C://myTDB//";
Dataset dataset = TDBFactory.createDataset(directory);
public static final String RDFPrefix = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
public static final String XSDPrefix = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>";
public static final String MYPrefix = "PREFIX myprefix: <http://www.myexample.com/mySchema#>";
public static void main(String[] args) {
UpdateTDB2 obj = new UpdateTDB2();
obj.createActionInstance();
obj.createStateInstance();
}
public static void execUpdate(String sparqlUpdateString,
GraphStore graphStore) {
UpdateRequest request = UpdateFactory.create(sparqlUpdateString);
UpdateProcessor proc = UpdateExecutionFactory.create(request,
graphStore);
proc.execute();
}
private void updateTriple(String sparqlUpdateString) {
dataset.begin(ReadWrite.WRITE);
try {
GraphStore graphStore = GraphStoreFactory.create(dataset);
execUpdate(sparqlUpdateString, graphStore);
dataset.commit();
} finally {
dataset.end();
}
}
private void createActionInstance() {
String subject = new StringBuffer("myprefix:").append("LS_1_user")
.toString();
String predicate = "rdf:type";
String object = "myprefix:Action";
String insertString = createInsertString(subject, predicate, object);
String sparqlInsertString = createSparqlString(insertString);
updateTriple(sparqlInsertString);
}
private void createStateInstance() {
String subject = new StringBuffer("myprefix:").append("LS_1_user_LicState")
.toString();
String predicate = "rdf:type";
String object = "myprefix:State";
String insertString = createInsertString(subject, predicate, object);
String sparqlInsertString = createSparqlString(insertString);
updateTriple(sparqlInsertString);
}
private String createInsertString(String subject, String predicate,
String object) {
String insertString = new StringBuffer("INSERT DATA { ")
.append(subject).append(" ").append(predicate).append(" ")
.append(object).append(" }").toString();
return insertString;
}
private String createSparqlString(String str) {
String sparqlString = StrUtils.strjoinNL(UpdateTDB2.XSDPrefix,
UpdateTDB2.RDFPrefix, UpdateTDB2.MYPrefix, str);
System.out.println(sparqlString);
return sparqlString;
}
}
public class QueryTDB3 {
public static void main(String[] args) {
String directory = "C://myTDB//" ;
Dataset dataset = TDBFactory.createDataset(directory) ;
dataset.begin(ReadWrite.READ) ;
try
{
// Do some queries
String sparqlQueryString1 = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ;
String sparqlQueryString2 = "SELECT * { ?s ?p ?o }" ;
execQuery(sparqlQueryString2, dataset) ;
execQuery(sparqlQueryString1, dataset) ;
} finally
{
dataset.end() ;
}
}
public static void execQuery(String sparqlQueryString, Dataset dataset)
{
Query query = QueryFactory.create(sparqlQueryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ;
try {
ResultSet results = qexec.execSelect() ;
ResultSetFormatter.out(results) ;
} finally { qexec.close() ; }
}
}
答案 0 :(得分:0)
您运行的是哪个版本的代码?
如果您显示的代码(第一部分)实际上是在两个单独的运行中运行(因此是两个JVM),那么这可能是由于TDB中已知且已修复的错误。它只存在于最近的相对版本中。
请尝试使用Jena 2.7.4-SNAPSHOT的开发版本。