tags:

views:

98

answers:

1

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&gt; ?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 ?

+1  A: 

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
Do you think it's a bug or a feature ?
Stefano Borini
Ok, I browsed the sources, and it does appear that everything happens under a single transaction. It's the store who keeps the transaction, so recreating the ConjunctiveGraph every time does not perform commit and it will always see the graph at the creation of the store instance.
Stefano Borini