0

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();
        }

    }
}
Rob Hall
  • 2,693
  • 16
  • 22
cooljohny
  • 656
  • 5
  • 13
  • 31

1 Answers1

0

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.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
AndyS
  • 16,345
  • 17
  • 21
  • 1
    The code example is not complete. It does not show you writing to the `Dataset`, committing those writes, and then attempting to read it later. Please construct a minimal working example, with data, that demonstrates the error, which we can run ourselves. If we cannot duplicate the error, it is much less likely that you will get a solution. – Rob Hall Jun 23 '14 at 15:02