You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by "Eugene Tenkaev (Hronom)" <hr...@gmail.com> on 2015/08/06 15:31:18 UTC

How write data in mem `Graph` using UpdateRequest?

Hello everyone! 
I use Apache Jena version 2.13.0.
I try to add in existing Graph new triple by using SPARQL, and triple didn’t added, here is the code:
public class TestTransactions {
    public static void main(String[] args) {
        Dataset dataset = TDBFactory.createDataset();
        DatasetGraph datasetGraph = dataset.asDatasetGraph();
        Graph graph = datasetGraph.getDefaultGraph();

        // Fill graph.
        graph.add(
            new Triple(
                NodeFactory.createURI("http://example/unit13"),
                NodeFactory.createURI("http://example/name"),
                NodeFactory.createLiteral("Unit 13", "en")
            )
        );

        graph.add(
            new Triple(
                NodeFactory.createURI("http://example/unit13"),
                NodeFactory.createURI("http://example/type"),
                NodeFactory.createURI("http://example/robot")
            )
        );

        graph.add(
            new Triple(
                NodeFactory.createURI("http://example/unit13"),
                NodeFactory.createURI("http://example/creationYear"),
                NodeFactory.createURI("http://example/2015")
            )
        );

        // Test.
        dataset.begin(ReadWrite.WRITE);

        UpdateRequest request =
            UpdateFactory.create(
            "INSERT { ?s <http://example/value> '1' } WHERE { ?s <http://example/creationYear> <http://example/2015> . }"
        );
        UpdateAction.execute(request, dataset);

        dataset.commit();
        dataset.end();

        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
    }
}

If I remove “dataset.begin(ReadWrite.WRITE);”, “dataset.commit();” and “dataset.end();” triple will be inserted…

Test project on GitHub https://github.com/Hronom/test-jena

Re: How write data in mem `Graph` using UpdateRequest?

Posted by Hronom <hr...@gmail.com>.
Big thanks for the detailed response!

В Четверг, 6 Авг. 2015 в 6:27 , Rob Vesse 
<rv...@dotnetrdf.org> написал:
> Because if you access a graph prior to starting a transaction you get 
> a
> non-transactional graph and the database is accessed in 
> non-transactional
> mode
> 
> When you then start a transaction the database switches to 
> transactional
> mode access and uncommitted non-transactional changes are lost as a 
> result
> 
> Either always use transactions or never use transactions, we strongly
> recommend always using transactions as otherwise your data is subject 
> to
> corruption and loss should your program exit unexpectedly or not 
> clean up
> the resources properly
> 
> If you really want to mix and match (which you really really 
> shouldn't do)
> then you need to make sure that you call TDB.sync(graph) prior to 
> starting
> your transaction to ensure the non-transactional changes are persisted
> 
> Rob
> 
> On 06/08/2015 15:32, "Евгений =)" <hr...@gmail.com> wrote:
> 
>> Thanks! So I change code:
>> public class TestTdbTransactions {
>>     public static void main(String[] args) {
>>         Dataset dataset = TDBFactory.createDataset();
>>         dataset.begin(ReadWrite.WRITE);
>>         DatasetGraph datasetGraph = dataset.asDatasetGraph();
>>         Graph graph = datasetGraph.getDefaultGraph();
>> 
>>         // Fill graph.
>>         graph.add(
>>             new Triple(
>>                 NodeFactory.createURI("http://example/unit13"),
>>                 NodeFactory.createURI("http://example/name"),
>>                 NodeFactory.createLiteral("Unit 13", "en")
>>             )
>>         );
>> 
>>         graph.add(
>>             new Triple(
>>                 NodeFactory.createURI("http://example/unit13"),
>>                 NodeFactory.createURI("http://example/type"),
>>                 NodeFactory.createURI("http://example/robot")
>>             )
>>         );
>> 
>>         graph.add(
>>             new Triple(
>>                 NodeFactory.createURI("http://example/unit13"),
>>                 NodeFactory.createURI("http://example/creationYear"),
>>                 NodeFactory.createURI("http://example/2015")
>>             )
>>         );
>> 
>>         // Test.
>>         UpdateRequest request = UpdateFactory.create(
>>             "INSERT { ?s <http://example/value> '1' } WHERE { ?s <
>> http://example/creationYear> <http://example/2015> . }"
>>         );
>>         UpdateAction.execute(request, dataset);
>> 
>>         dataset.commit();
>>         dataset.end();
>> 
>>         RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
>>     }
>> }
>> 
>> And it works!
>> 
>> But if I move "dataset.begin(ReadWrite.WRITE);" after "Graph graph =
>> datasetGraph.getDefaultGraph();" this code doesn't work... why this
>> happens?
>> 
>> 2015-08-06 17:12 GMT+03:00 Rob Vesse <rv...@dotnetrdf.org>:
>> 
>>>  You are mixing non-transactional use with transactional use and 
>>> this
>>>  breaks things, your initial insert is in non-transactional mode 
>>> but then
>>>  you start a transaction so some of the changes get lost
>>> 
>>>  You should always stick to one usage model, we strongly recommend
>>>  transactional use
>>> 
>>>  Simply moving your dataset.begin(ReadWrite.WRITE); call to before 
>>> you
>>>  start making changes to the graph should resolve your issue
>>> 
>>>  Rob
>>> 
>>>  On 06/08/2015 14:31, "Eugene Tenkaev (Hronom)" <hr...@gmail.com> 
>>> wrote:
>>> 
>>>  >Hello everyone!
>>>  >I use Apache Jena version 2.13.0.
>>>  >I try to add in existing Graph new triple by using SPARQL, and 
>>> triple
>>>  >didn’t added, here is the code:
>>>  >public class TestTransactions {
>>>  >    public static void main(String[] args) {
>>>  >        Dataset dataset = TDBFactory.createDataset();
>>>  >        DatasetGraph datasetGraph = dataset.asDatasetGraph();
>>>  >        Graph graph = datasetGraph.getDefaultGraph();
>>>  >
>>>  >        // Fill graph.
>>>  >        graph.add(
>>>  >            new Triple(
>>>  >                NodeFactory.createURI("http://example/unit13"),
>>>  >                NodeFactory.createURI("http://example/name"),
>>>  >                NodeFactory.createLiteral("Unit 13", "en")
>>>  >            )
>>>  >        );
>>>  >
>>>  >        graph.add(
>>>  >            new Triple(
>>>  >                NodeFactory.createURI("http://example/unit13"),
>>>  >                NodeFactory.createURI("http://example/type"),
>>>  >                NodeFactory.createURI("http://example/robot")
>>>  >            )
>>>  >        );
>>>  >
>>>  >        graph.add(
>>>  >            new Triple(
>>>  >                NodeFactory.createURI("http://example/unit13"),
>>>  >                
>>> NodeFactory.createURI("http://example/creationYear"),
>>>  >                NodeFactory.createURI("http://example/2015")
>>>  >            )
>>>  >        );
>>>  >
>>>  >        // Test.
>>>  >        dataset.begin(ReadWrite.WRITE);
>>>  >
>>>  >        UpdateRequest request =
>>>  >            UpdateFactory.create(
>>>  >            "INSERT { ?s <http://example/value> '1' } WHERE { ?s
>>>  ><http://example/creationYear> <http://example/2015> . }"
>>>  >        );
>>>  >        UpdateAction.execute(request, dataset);
>>>  >
>>>  >        dataset.commit();
>>>  >        dataset.end();
>>>  >
>>>  >        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
>>>  >    }
>>>  >}
>>>  >
>>>  >If I remove “dataset.begin(ReadWrite.WRITE);”, 
>>> “dataset.commit();” and
>>>  >“dataset.end();” triple will be inserted…
>>>  >
>>>  >Test project on GitHub https://github.com/Hronom/test-jena
>>> 
>>> 
>>> 
>>> 
>>> 
> 
> 
> 
> 

Re: How write data in mem `Graph` using UpdateRequest?

Posted by Rob Vesse <rv...@dotnetrdf.org>.
Because if you access a graph prior to starting a transaction you get a
non-transactional graph and the database is accessed in non-transactional
mode

When you then start a transaction the database switches to transactional
mode access and uncommitted non-transactional changes are lost as a result

Either always use transactions or never use transactions, we strongly
recommend always using transactions as otherwise your data is subject to
corruption and loss should your program exit unexpectedly or not clean up
the resources properly

If you really want to mix and match (which you really really shouldn't do)
then you need to make sure that you call TDB.sync(graph) prior to starting
your transaction to ensure the non-transactional changes are persisted

Rob

On 06/08/2015 15:32, "Евгений =)" <hr...@gmail.com> wrote:

>Thanks! So I change code:
>public class TestTdbTransactions {
>    public static void main(String[] args) {
>        Dataset dataset = TDBFactory.createDataset();
>        dataset.begin(ReadWrite.WRITE);
>        DatasetGraph datasetGraph = dataset.asDatasetGraph();
>        Graph graph = datasetGraph.getDefaultGraph();
>
>        // Fill graph.
>        graph.add(
>            new Triple(
>                NodeFactory.createURI("http://example/unit13"),
>                NodeFactory.createURI("http://example/name"),
>                NodeFactory.createLiteral("Unit 13", "en")
>            )
>        );
>
>        graph.add(
>            new Triple(
>                NodeFactory.createURI("http://example/unit13"),
>                NodeFactory.createURI("http://example/type"),
>                NodeFactory.createURI("http://example/robot")
>            )
>        );
>
>        graph.add(
>            new Triple(
>                NodeFactory.createURI("http://example/unit13"),
>                NodeFactory.createURI("http://example/creationYear"),
>                NodeFactory.createURI("http://example/2015")
>            )
>        );
>
>        // Test.
>        UpdateRequest request = UpdateFactory.create(
>            "INSERT { ?s <http://example/value> '1' } WHERE { ?s <
>http://example/creationYear> <http://example/2015> . }"
>        );
>        UpdateAction.execute(request, dataset);
>
>        dataset.commit();
>        dataset.end();
>
>        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
>    }
>}
>
>And it works!
>
>But if I move "dataset.begin(ReadWrite.WRITE);" after "Graph graph =
>datasetGraph.getDefaultGraph();" this code doesn't work... why this
>happens?
>
>2015-08-06 17:12 GMT+03:00 Rob Vesse <rv...@dotnetrdf.org>:
>
>> You are mixing non-transactional use with transactional use and this
>> breaks things, your initial insert is in non-transactional mode but then
>> you start a transaction so some of the changes get lost
>>
>> You should always stick to one usage model, we strongly recommend
>> transactional use
>>
>> Simply moving your dataset.begin(ReadWrite.WRITE); call to before you
>> start making changes to the graph should resolve your issue
>>
>> Rob
>>
>> On 06/08/2015 14:31, "Eugene Tenkaev (Hronom)" <hr...@gmail.com> wrote:
>>
>> >Hello everyone!
>> >I use Apache Jena version 2.13.0.
>> >I try to add in existing Graph new triple by using SPARQL, and triple
>> >didn’t added, here is the code:
>> >public class TestTransactions {
>> >    public static void main(String[] args) {
>> >        Dataset dataset = TDBFactory.createDataset();
>> >        DatasetGraph datasetGraph = dataset.asDatasetGraph();
>> >        Graph graph = datasetGraph.getDefaultGraph();
>> >
>> >        // Fill graph.
>> >        graph.add(
>> >            new Triple(
>> >                NodeFactory.createURI("http://example/unit13"),
>> >                NodeFactory.createURI("http://example/name"),
>> >                NodeFactory.createLiteral("Unit 13", "en")
>> >            )
>> >        );
>> >
>> >        graph.add(
>> >            new Triple(
>> >                NodeFactory.createURI("http://example/unit13"),
>> >                NodeFactory.createURI("http://example/type"),
>> >                NodeFactory.createURI("http://example/robot")
>> >            )
>> >        );
>> >
>> >        graph.add(
>> >            new Triple(
>> >                NodeFactory.createURI("http://example/unit13"),
>> >                NodeFactory.createURI("http://example/creationYear"),
>> >                NodeFactory.createURI("http://example/2015")
>> >            )
>> >        );
>> >
>> >        // Test.
>> >        dataset.begin(ReadWrite.WRITE);
>> >
>> >        UpdateRequest request =
>> >            UpdateFactory.create(
>> >            "INSERT { ?s <http://example/value> '1' } WHERE { ?s
>> ><http://example/creationYear> <http://example/2015> . }"
>> >        );
>> >        UpdateAction.execute(request, dataset);
>> >
>> >        dataset.commit();
>> >        dataset.end();
>> >
>> >        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
>> >    }
>> >}
>> >
>> >If I remove “dataset.begin(ReadWrite.WRITE);”, “dataset.commit();” and
>> >“dataset.end();” triple will be inserted…
>> >
>> >Test project on GitHub https://github.com/Hronom/test-jena
>>
>>
>>
>>
>>





Re: How write data in mem `Graph` using UpdateRequest?

Posted by .
Thanks! So I change code:
public class TestTdbTransactions {
    public static void main(String[] args) {
        Dataset dataset = TDBFactory.createDataset();
        dataset.begin(ReadWrite.WRITE);
        DatasetGraph datasetGraph = dataset.asDatasetGraph();
        Graph graph = datasetGraph.getDefaultGraph();

        // Fill graph.
        graph.add(
            new Triple(
                NodeFactory.createURI("http://example/unit13"),
                NodeFactory.createURI("http://example/name"),
                NodeFactory.createLiteral("Unit 13", "en")
            )
        );

        graph.add(
            new Triple(
                NodeFactory.createURI("http://example/unit13"),
                NodeFactory.createURI("http://example/type"),
                NodeFactory.createURI("http://example/robot")
            )
        );

        graph.add(
            new Triple(
                NodeFactory.createURI("http://example/unit13"),
                NodeFactory.createURI("http://example/creationYear"),
                NodeFactory.createURI("http://example/2015")
            )
        );

        // Test.
        UpdateRequest request = UpdateFactory.create(
            "INSERT { ?s <http://example/value> '1' } WHERE { ?s <
http://example/creationYear> <http://example/2015> . }"
        );
        UpdateAction.execute(request, dataset);

        dataset.commit();
        dataset.end();

        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
    }
}

And it works!

But if I move "dataset.begin(ReadWrite.WRITE);" after "Graph graph =
datasetGraph.getDefaultGraph();" this code doesn't work... why this happens?

2015-08-06 17:12 GMT+03:00 Rob Vesse <rv...@dotnetrdf.org>:

> You are mixing non-transactional use with transactional use and this
> breaks things, your initial insert is in non-transactional mode but then
> you start a transaction so some of the changes get lost
>
> You should always stick to one usage model, we strongly recommend
> transactional use
>
> Simply moving your dataset.begin(ReadWrite.WRITE); call to before you
> start making changes to the graph should resolve your issue
>
> Rob
>
> On 06/08/2015 14:31, "Eugene Tenkaev (Hronom)" <hr...@gmail.com> wrote:
>
> >Hello everyone!
> >I use Apache Jena version 2.13.0.
> >I try to add in existing Graph new triple by using SPARQL, and triple
> >didn’t added, here is the code:
> >public class TestTransactions {
> >    public static void main(String[] args) {
> >        Dataset dataset = TDBFactory.createDataset();
> >        DatasetGraph datasetGraph = dataset.asDatasetGraph();
> >        Graph graph = datasetGraph.getDefaultGraph();
> >
> >        // Fill graph.
> >        graph.add(
> >            new Triple(
> >                NodeFactory.createURI("http://example/unit13"),
> >                NodeFactory.createURI("http://example/name"),
> >                NodeFactory.createLiteral("Unit 13", "en")
> >            )
> >        );
> >
> >        graph.add(
> >            new Triple(
> >                NodeFactory.createURI("http://example/unit13"),
> >                NodeFactory.createURI("http://example/type"),
> >                NodeFactory.createURI("http://example/robot")
> >            )
> >        );
> >
> >        graph.add(
> >            new Triple(
> >                NodeFactory.createURI("http://example/unit13"),
> >                NodeFactory.createURI("http://example/creationYear"),
> >                NodeFactory.createURI("http://example/2015")
> >            )
> >        );
> >
> >        // Test.
> >        dataset.begin(ReadWrite.WRITE);
> >
> >        UpdateRequest request =
> >            UpdateFactory.create(
> >            "INSERT { ?s <http://example/value> '1' } WHERE { ?s
> ><http://example/creationYear> <http://example/2015> . }"
> >        );
> >        UpdateAction.execute(request, dataset);
> >
> >        dataset.commit();
> >        dataset.end();
> >
> >        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
> >    }
> >}
> >
> >If I remove “dataset.begin(ReadWrite.WRITE);”, “dataset.commit();” and
> >“dataset.end();” triple will be inserted…
> >
> >Test project on GitHub https://github.com/Hronom/test-jena
>
>
>
>
>

Re: How write data in mem `Graph` using UpdateRequest?

Posted by Rob Vesse <rv...@dotnetrdf.org>.
You are mixing non-transactional use with transactional use and this
breaks things, your initial insert is in non-transactional mode but then
you start a transaction so some of the changes get lost

You should always stick to one usage model, we strongly recommend
transactional use

Simply moving your dataset.begin(ReadWrite.WRITE); call to before you
start making changes to the graph should resolve your issue

Rob

On 06/08/2015 14:31, "Eugene Tenkaev (Hronom)" <hr...@gmail.com> wrote:

>Hello everyone! 
>I use Apache Jena version 2.13.0.
>I try to add in existing Graph new triple by using SPARQL, and triple
>didn’t added, here is the code:
>public class TestTransactions {
>    public static void main(String[] args) {
>        Dataset dataset = TDBFactory.createDataset();
>        DatasetGraph datasetGraph = dataset.asDatasetGraph();
>        Graph graph = datasetGraph.getDefaultGraph();
>
>        // Fill graph.
>        graph.add(
>            new Triple(
>                NodeFactory.createURI("http://example/unit13"),
>                NodeFactory.createURI("http://example/name"),
>                NodeFactory.createLiteral("Unit 13", "en")
>            )
>        );
>
>        graph.add(
>            new Triple(
>                NodeFactory.createURI("http://example/unit13"),
>                NodeFactory.createURI("http://example/type"),
>                NodeFactory.createURI("http://example/robot")
>            )
>        );
>
>        graph.add(
>            new Triple(
>                NodeFactory.createURI("http://example/unit13"),
>                NodeFactory.createURI("http://example/creationYear"),
>                NodeFactory.createURI("http://example/2015")
>            )
>        );
>
>        // Test.
>        dataset.begin(ReadWrite.WRITE);
>
>        UpdateRequest request =
>            UpdateFactory.create(
>            "INSERT { ?s <http://example/value> '1' } WHERE { ?s
><http://example/creationYear> <http://example/2015> . }"
>        );
>        UpdateAction.execute(request, dataset);
>
>        dataset.commit();
>        dataset.end();
>
>        RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
>    }
>}
>
>If I remove “dataset.begin(ReadWrite.WRITE);”, “dataset.commit();” and
>“dataset.end();” triple will be inserted…
>
>Test project on GitHub https://github.com/Hronom/test-jena