简体   繁体   中英

Jena TDB: Nested Transactions

I'd like to rewrite my current code for making use of transactions. However, according to the Jena documentation ( http://incubator.apache.org/jena/documentation/tdb/tdb_transactions.html ) nested transactions are not supported.

Let's say, I'd like to query some data from the database and add a rdfs:label to each resource found. Would I have to strictly separte reading and writing code like in the following code, or is there a more efficient way to implement this example?

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

I've posted this question on semanticweb.com , but haven't received any answers, so I hope someone here can help me.

It's true that nested transactions are not supported in TDB, however you can do as many reads as you want in a WRITE transaction. So, start a ReadWrite.WRITE transaction and do all you processing there. No need of nested transactions for what you want to do.

For more on TDB's transaction support, please, look at the official documentation here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM