What's wrong with this code? which model could be created?

public boolean execUpdate(String _sparsql){

boolean ret = false;

_sparsql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>"+

     " INSERT DATA"+

     "{<http://example/book8> dc:title    \"A new book8\" ;"+

     "                        dc:creator  \"A.N.Other8\" .}";

InputStream in  =  new FileInputStream("c:\\books.ttl");

Model model = ModelFactory.createDefaultModel();

UpdateRequest request = UpdateFactory.create();

try {

  request.add(sparsql);

  model.read(in,null,"TURTLE");

  model.enterCriticalSection(Lock.WRITE);

  UpdateAction.execute(request, model);

  model.commit();

  model.leaveCriticalSection();

  System.out.println("==== finished ====");
  ret = true;
}catch (Exception e){         
} finally {   
}
return ret;
}

Error:result

[2555-12-15 21:16:10.468]  ERROR java.lang.Class - java.lang.UnsupportedOperationException: this model does not support transactions
    at com.hp.hpl.jena.graph.impl.SimpleTransactionHandler.notSupported(SimpleTransactionHandler.java:45)
    at com.hp.hpl.jena.graph.impl.SimpleTransactionHandler.begin(SimpleTransactionHandler.java:34)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.begin(ModelCom.java:1268)
    at OntFile.OntFile.execUpdate(OntFile.java:163)
    at OntFile.OntFile.main(OntFile.java:240)

asked 15 Dec '12, 09:20

Ton%20Rawat's gravatar image

Ton Rawat
263
accept rate: 50%

edited 16 Dec '12, 01:19

Rob%20Vesse's gravatar image

Rob Vesse ♦
13.8k1715


Memory models don't support transactions. Remove the model.commit.

This style is as safer although if an exception occurs in your code, the model is possible logically inconsistent from the applications POV anyway.

model.enterCriticalSection(Lock.WRITE);
try{ 
   ...
} finally { model.leaveCriticalSection(); }
link

answered 15 Dec '12, 12:45

AndyS's gravatar image

AndyS ♦
13.2k37
accept rate: 32%

Thank you for suggestion AndyS. Anyway the solution for write back below. :) ...

m.enterCriticalSection(Lock.WRITE);

UpdateAction.execute(request, m);

FileWriter fstream = new FileWriter("c:\books.ttl");

BufferedWriter out = new BufferedWriter(fstream);

String lang = FileUtils.guessLang("c:\books.ttl");

m.write(out,lang);

m.leaveCriticalSection();
...

link

answered 15 Dec '12, 13:18

Ton%20Rawat's gravatar image

Ton Rawat
263
accept rate: 50%

1

If you want atomic, safe update of a file on disk, then playing the "write a new file (in the same directory), delete the old file, move the new file to the old file" dance (with checks for a machine crash between rm and mv if you care), is the way to do it.

(15 Dec '12, 13:24) AndyS ♦ AndyS's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,265
×590
×46

Asked: 15 Dec '12, 09:20

Seen: 1,146 times

Last updated: 16 Dec '12, 01:19