问题
I am trying to write propreties into a model and then query it.This part of mycode:
String directory = "EMAILADDRESS" ;
//create the dataset for the tdb store
Dataset ds = TDBFactory.createDataset(directory) ;
//create default rdf model
ds.begin(ReadWrite.WRITE);
Model model = ds.getDefaultModel() ;
//write to the tdb dataset
When I write this and then query the query shows no result ...but when I interchange the order of model and begin i.e.
Model model = ds.getDefaultModel() ;
//write to the tdb dataset
ds.begin(ReadWrite.WRITE);
Then it works fine!! but it sometimes gives this error:
com.hp.hpl.jena.tdb.transaction.TDBTransactionException: Not in a transaction
I know that first way is correct but I don't understand why it doesn't respond to queries..This is code for quering:
public class test4query extends Object {
public static String[] arr=new String[30];
public void mai (String s) {
String directory = "EMAILADDRESS" ;
Dataset ds = TDBFactory.createDataset(directory) ;
ds.begin(ReadWrite.READ) ;
Model model = ds.getDefaultModel() ;
QueryExecution qExec = QueryExecutionFactory.create(s, ds) ;
int i=0;
try{
ResultSet rs = qExec.execSelect() ;
String x=rs.toString();
while (rs.hasNext()) {
QuerySolution qs = rs.next();
String rds;
if(qs.get("x")!=null) {
rds = qs.get("x").toString();
} else {
rds="hi";
}
if(rds==null) {
break;
}
System.out.println(rds);
arr[i] = rds;
i++;
}
} finally
{qExec.close() ;
ds.commit();
ds.end();
}
}
}
回答1:
It is unclear when you get hat exception. The code example is full of parts that are commented out and does not use "m" at all.
You can not call ResultSetFormatter.out(rs) after you have called qExec.close or ds.commit.
来源:https://stackoverflow.com/questions/24349035/errors-in-transaction-in-jena-tdb