2
String directory = "directoryPath"; 
Dataset dataset = TDBFactory.createDataset(directory);
Dataset datasetnew = TDBFactory.createDataset(directory);

Is the dataset a reference to the TDB directory or it's like a folder inside the TDB directory? Will datasetnew again creates a reference to the Directory or separate datset folder will get created? Basically, I have a use case of creating one dataset per user where the user can save all its models and does not interfere with models of other users.

If this is not how it's done, can someone suggest me a way!

Stanislav Kralin
  • 10,115
  • 4
  • 30
  • 52
Shreshtha Garg
  • 125
  • 1
  • 9

1 Answers1

3

When you specify the same directory, a new directory will not be created. In actual fact both dataset and datsetnew point to the same physical RDF store. I.e. if you try to create an transaction on both of them you will get a Currently in an active transaction org.apache.jena.dboe.transaction.txn.TransactionException. So NO, you cannot create multiple datasets in a single TDB directory.

Another observation is that you seem to be using TDB1 rather than TDB2. TDB2 is the later version and it would therefore be the best to use. Then your code will look as follows:

  Path path = Paths.get(".").toAbsolutePath().normalize();      
  String dbDir = path.toFile().getAbsolutePath() + "/db/"; 

  Location location = Location.create(dbDir);

  Dataset dataset = TDB2Factory.connectDataset(location);
  String strQuery = "INSERT DATA {<http://dbpedia.org/resource/Grace_Hopper> <http://xmlns.com/foaf/0.1/surname> \"Hopper\" .}";

  dataset.begin(ReadWrite.WRITE);
  UpdateRequest updateRequest = UpdateFactory.create(strQuery);
  UpdateProcessor updateProcessor = UpdateExecutionFactory.create(updateRequest, dataset);

  updateProcessor.execute();
  dataset.commit();
  dataset.close();
Henriette Harmse
  • 3,070
  • 1
  • 11
  • 18