You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by on 2015/08/19 14:07:49 UTC

Open previously closed TDB

Hello everyone.

I try open previously closed TDB in one java instance.

And got next error:
Exception in thread "main" com.hp.hpl.jena.shared.ClosedException:
already closed
at com.hp.hpl.jena.graph.impl.GraphBase.checkOpen(GraphBase.java:66)
at com.hp.hpl.jena.graph.impl.GraphBase.add(GraphBase.java:201)
at com.github.hronom.test.jena.TestReopenTdb.main(TestReopenTdb.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Here is full code:
package com.github.hronom.test.jena;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.sparql.core.DatasetGraph;
import com.hp.hpl.jena.tdb.TDBFactory;

import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;

import java.nio.file.Path;
import java.nio.file.Paths;

public class TestReopenTdb {
   public static final Path pathToTdb = Paths.get("test_tdb");

   public static void main(String[] args) {
       Graph graph = openTdb(pathToTdb);

       // 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")
           )
       );

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

       graph.close();

       graph = openTdb(pathToTdb);

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

       graph.close();
   }

   /**
    * Open specified TDB as Graph. If TDB was already opened then return him.
    *
    * @param tdbPath Path to TDB directory.
    * @return Opened Graph.
    */
   private static Graph openTdb(Path tdbPath) {
       // Open TDB.
       DatasetGraph datasetGraph =
TDBFactory.createDatasetGraph(tdbPath.toString());
       return datasetGraph.getDefaultGraph();
   }
}

So how I can reopen TDB in one instance?

Jena version 2.13.0

Re: Open previously closed TDB

Posted by Andy Seaborne <an...@apache.org>.
Simple answer: don't close it.

A TDB graph is a simple projection of part of the database.  It does not 
carry any state itself except info about what it's projecting (graph 
name or default graph).  It holds no triples.  Cost - minimal.  Closing 
the graph does not close the database.

datasetGraph.getDefaultGraph();

You "get" the same one each time.

TDBFactory.createDatasetGraph is a bit of a lie - it will return 
something that has behind it the same storage as all other instances.

(We can make the close operation a no-op)

	Andy


On 19/08/15 13:07, Евгений =) wrote:
> Hello everyone.
>
> I try open previously closed TDB in one java instance.
>
> And got next error:
> Exception in thread "main" com.hp.hpl.jena.shared.ClosedException:
> already closed
> at com.hp.hpl.jena.graph.impl.GraphBase.checkOpen(GraphBase.java:66)
> at com.hp.hpl.jena.graph.impl.GraphBase.add(GraphBase.java:201)
> at com.github.hronom.test.jena.TestReopenTdb.main(TestReopenTdb.java:55)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:606)
> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
>
> Here is full code:
> package com.github.hronom.test.jena;
>
> import com.hp.hpl.jena.graph.Graph;
> import com.hp.hpl.jena.graph.NodeFactory;
> import com.hp.hpl.jena.graph.Triple;
> import com.hp.hpl.jena.sparql.core.DatasetGraph;
> import com.hp.hpl.jena.tdb.TDBFactory;
>
> import org.apache.jena.riot.RDFDataMgr;
> import org.apache.jena.riot.RDFFormat;
>
> import java.nio.file.Path;
> import java.nio.file.Paths;
>
> public class TestReopenTdb {
>     public static final Path pathToTdb = Paths.get("test_tdb");
>
>     public static void main(String[] args) {
>         Graph graph = openTdb(pathToTdb);
>
>         // 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")
>             )
>         );
>
>         RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
>
>         graph.close();
>
>         graph = openTdb(pathToTdb);
>
>         graph.add(
>             new Triple(
>                 NodeFactory.createURI("http://example/unit15"),
>                 NodeFactory.createURI("http://example/creationYear"),
>                 NodeFactory.createURI("http://example/2015")
>             )
>         );
>
>         graph.close();
>     }
>
>     /**
>      * Open specified TDB as Graph. If TDB was already opened then return him.
>      *
>      * @param tdbPath Path to TDB directory.
>      * @return Opened Graph.
>      */
>     private static Graph openTdb(Path tdbPath) {
>         // Open TDB.
>         DatasetGraph datasetGraph =
> TDBFactory.createDatasetGraph(tdbPath.toString());
>         return datasetGraph.getDefaultGraph();
>     }
> }
>
> So how I can reopen TDB in one instance?
>
> Jena version 2.13.0
>