2

I am using Jena APIs to insert and update triples in Jena TDB. My design is such that each of the insert operation is within the transaction control. For example:

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

After this, when I query the TDB using the READ transaction , I notice that some of the entries (either subject, predicate, or object) are empty. Why does it behaves this way, even though I am not using nested transactions?

Insert Code

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

}

Query Code

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

}
Joshua Taylor
  • 80,876
  • 9
  • 135
  • 306
  • 1
    Please show the code that tells you the entries are `null`. Often, Jena users report this problem when fact what they mean is that they are calling `getURI()` on a resource, and that is returning `null`. That's not a problem, it just means you have blank nodes (bNodes) in your data. – Ian Dickinson Oct 03 '12 at 21:29
  • @IanDickinson , I have updated the original question with some sample code. There are 2 separate classes, one performing the insert while the other queries the TDB. – sandhya ramachandran Oct 04 '12 at 04:19

1 Answers1

0

Which version of the code are you running?

If the code you show (first part) is really being run in two separate runs (hence two JVMs), then this might be due to a known-and-fixed bug in TDB. It was only present in relative recent versions.

Please try the development version of Jena 2.7.4-SNAPSHOT.

AndyS
  • 14,989
  • 15
  • 20