Search code examples
transactionssparqlrdfjenafuseki

Transaction using Jena API on Fuseki SPARQL Endpoint does not actually modify the database


I want to use transactions to execute insert/update/delete queries on a SPARQL endpoint. The problem is that the changes are not actually saved to the endpoint.

If I run this small example program:

    public void test1()
    {
        Dataset dataset = DatasetFactory.create("http://localhost:3030/demo");
        System.out.println("Supports transactions = " + dataset.supportsTransactions());
        System.out.println("Triples before: " + dataset.getDefaultModel().size());

        dataset.begin(TxnType.WRITE);

        UpdateRequest request = UpdateFactory.create(
            "INSERT DATA { <http://example.org/b68d359eb001> a 123 }");
        UpdateExecution.dataset(dataset).update(request).execute();

        try
        {
            dataset.commit();
        }
        catch (Throwable throwable)
        {
            throwable.printStackTrace();
        }
        finally
        {
            dataset.end();
        }

        System.out.println("Triples after: " + dataset.getDefaultModel().size());
    }

It outputs:

Supports transactions = true
Triples before: 1178
Triples after: 1179

We can see that the new triple was added to the dataset object. However, if I run the program again, I get the exact some output, so the triple was not actually saved to the database. If I run the same query using Fuseki UI, it saves the triple without a problem. Am I running the transaction incorrectly?

I followed the documentation: https://jena.apache.org/documentation/txn/transactions_api.html (see the largest code snippet there). Note that I need to make updates by using SPARQL queries, not via Model etc.


Solution

  • Looks like I was able to solve it by not using transactions but instead putting all the queries (I have several that need to be executed) into one UpdateRequest. If any of the queries fails, the entire request fails:

    UpdateRequest updateRequest = new UpdateRequest();
    statements.forEach(updateRequest::add);
    UpdateExec.service("http://localhost:3030/demo").update(updateRequest).execute();