2

I am trying to understand this behavior. It's definitely not what I expect. I have two programs, one reader, and one writer. The reader opens a RDFlib graph store, then performs a query every 2 seconds

import rdflib
import random
from rdflib import store
import time

default_graph_uri = "urn:uuid:a19f9b78-cc43-4866-b9a1-4b009fe91f52"

s = rdflib.plugin.get('MySQL', store.Store)('rdfstore')

config_string = "host=localhost,password=foo,user=foo,db=foo"
rt = s.open(config_string,create=False)
if rt != store.VALID_STORE:
    s.open(config_string,create=True)


while True:
    graph = rdflib.ConjunctiveGraph(s, identifier = rdflib.URIRef(default_graph_uri))
    rows = graph.query("SELECT ?id ?value { ?id <http://localhost#ha> ?value . }")
    for r in rows:
        print r[0], r[1]
    time.sleep(2)
    print " - - - - - - - - "

The second program is a writer that adds stuff to the triplestore

import rdflib
import random
from rdflib import store

default_graph_uri = "urn:uuid:a19f9b78-cc43-4866-b9a1-4b009fe91f52"

s = rdflib.plugin.get('MySQL', store.Store)('rdfstore')

config_string = "host=localhost,password=foo,user=foo,db=foo"
rt = s.open(config_string,create=False)
if rt != store.VALID_STORE:
    s.open(config_string,create=True)

graph = rdflib.ConjunctiveGraph(s, identifier = rdflib.URIRef(default_graph_uri))

graph.add( ( 
            rdflib.URIRef("http://localhost/"+str(random.randint(0,100))), 
            rdflib.URIRef("http://localhost#ha"),
            rdflib.Literal(str(random.randint(0,100)))
            ) 
            )
graph.commit()

I would expect to see the number of results increment on the reader as I submit stuff using the writer, but this does not happen. The reader continues to return the same result as when it started. If however I stop the reader and restart it, the new results appear.

Does anybody know what am I doing wrong ?

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431

1 Answers1

3

One easy fix is to put "graph.commit()" just after the line "graph = rdflib.ConjunctiveGraph(...)" in reader. I'm not sure what's the cause and why commiting before read fixes this. I'm guessing that:

  • When opening MySQLdb connection, a transaction is started automatically
  • This transaction doesn't see updates from other, later transactions.
  • "graph.commit()" bubbles down to some "connection.commit()" somewhere that discards this transaction and starts a new one.
Pēteris Caune
  • 43,578
  • 6
  • 59
  • 81