Multiple instances of the same resource

9 views
Skip to first unread message

Nikola Milikic

unread,
Apr 17, 2011, 9:56:55 AM4/17/11
to jenabean-dev
Hi all,

We've been using Jenabean in our research project and it was really
easy to start with. Thanks for all the efffort!

But, as our application become bigger, we are facing certain issues.
The problem is that when loading twice the same resource (with the
same URI), Jenabean creates two objects in JVM instead of one as I
would first expect. I think this is a potential memory leak because if
the same resource is loaded several times from within different parts
of the application, the number of objects (actually representing the
same instance) multiplies. It also causes objects not to be mutually
synchronized (as they are not referencing the same instance) and
change to one doesn't reflect the others potentially causing 'dirty
read'.

Has anyone faced this issue before and is there some workaround like
enabling some sort of caching or similar?

Thanks a lot for your time!

Nikola

Taylor Cowan

unread,
Apr 17, 2011, 1:13:32 PM4/17/11
to Nikola Milikic, jenabean-dev
Nikola,

Raw janabean helps you persist beans, and create beans. Its not a
container, you could create your own cache of course.

Did you know there os also a jpa4jena? It does cache the instances
within a persistence context. I can help u get started

Taylor

Sent from my Windows Phone From: Nikola Milikic
Sent: Sunday, April 17, 2011 8:56 AM
To: jenabean-dev
Subject: Multiple instances of the same resource
Hi all,

Nikola

--
You received this message because you are subscribed to the Google
Groups "jenabean-dev" group.
To post to this group, send email to jenabe...@googlegroups.com.
To unsubscribe from this group, send email to
jenabean-dev...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/jenabean-dev?hl=en.

Nikola Milikic

unread,
Apr 17, 2011, 3:10:45 PM4/17/11
to jenabean-dev
Hi Taylor,

Thanks a lot for your response! I've looked now at the jpa4jena and it
looks pretty straightforward as a JPA implementation (I assume partial
implementation because some of it's aspects are not applicable on
RDF).

I suppose this is true, but just to check: can I still reuse my
Jenabean annotations which mapped my domain model classes to the
ontology classes?

I would also ask for your assistance on how to configure jenamodels.n3
to use my in memory Jena model (for now, when using plain Jenabean,
I'm passing this model instance to the RDF2Bean and Bean2RDF
instances) which is connected to the database through SDB. Is there an
example of similar case?

Is there anything else I should watch for? Or is it just enough to
change in my application to instead of saving, loading and deleting
instances by calling appropriate methods of RDF2Bean and Bean2RDF
instances to do appropriate calls on EntityManager instance?

Thanks a lot for your help!

Nikola

uoccou

unread,
Apr 17, 2011, 3:55:59 PM4/17/11
to jenabe...@googlegroups.com, Nikola Milikic
Hi,

Have you double-checked your equals(Object) and hashcode() methods ?
--

Regards

Ultan O'Carroll


Archive Link
email@
Lewis TDI 1842
SkyTwenty
Blog

Taylor Cowan

unread,
Apr 17, 2011, 4:11:46 PM4/17/11
to jenabe...@googlegroups.com
Yes, you are correct, there's a lot of RDBMS specific stuff that
doesn't relate...so it's unimplemented. you can do the native
queries, and even named queries, there are some examples in the test
suite.

I think you can use your beans as is, all I did was map JPA
annotations back to the jenbean annotations. I need to review the
project a bit to give a definitive answer, I've been learning .net and
azure this year, and have just now found some time to look back on the
java side.

Nikola Milikic

unread,
Apr 25, 2011, 4:53:11 AM4/25/11
to jenabe...@googlegroups.com
Hi all,

Thanks for the responses! 

My domain model classes (mapped to the ontologies classes) are designed in a manner that there is a class called Resource (representing rdf:Resource class) which every other classes extend. There I have uri field annotated with Jenabean's @Id annotation serving as an unique identifier. And my equals and hashCode methods are using just this field as follows:

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uri == null) ? 0 : uri.hashCode());
return result;
}

public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Resource other = (Resource) obj;
if (uri == null) {
if (other.uri != null)
return false;
} else if (!uri.equals(other.uri))
return false;
return true;
}

I tried to switch to using jpa4jena as Taylor suggested. Since I'm using in-memory Jena model (with SDB sitting in front of the database) I have configured jenamodels.n3 (located in META-INF folder) as follows

tws:intelleomodel a ja:MemoryModel ;
tws:package "org.intelleo.services.datacentral";
.

But when I want to obtain an instance of the EntityManagerFactory (actually JBFactory instance) I'm getting null, thus I can't obtain EntityManager either:

JBProvider p = new JBProvider(DataModelManager.getInstance().getDataModel());
JBFactory f =  p.createEntityManagerFactory("tws:intelleomodel", null);
EntityManager em =  f.createEntityManager();

DataModelManager.getInstance().getDataModel() is just returning this Jena model my application is using and DataModelManager class is located in the 'org.intelleo.services.datacentral' package (referenced from jenamodels.n3).

I feel I'm so close to making this to work, but just missing possibly an extra step to perform.:) 

I would highly appreciate assistance with obtaining EntityManager instance. Thanks!

Best,
Nikola

Email: nikola....@gmail.com
URL:   nikola.milikic.info

Nikola Milikic

unread,
Apr 28, 2011, 3:24:17 PM4/28/11
to jenabe...@googlegroups.com
Hi Taylor, all,

I realized what I was doing wrong when instantiating JBProvider. In its constructor the argument passed shouldn't be (Jena's) Model instance with data (as I first supposed), but the model of the assembler file.

Model m = ModelFactory.createDefaultModel();
URL uri = m.getClass().getResource("/jenamodels.n3");
m.read(uri.toString(), "N3");

JBProvider p = new JBProvider(m);

What's not clear to me now is how can I pass an existing Model instance with data in it (in my case sitting in a database behind SDB). I've dove into the source code and the only way I found an EntityManager being created is with an empty Model with just Jenabean javaclass mappings read from the package declared as tws:package value (created in JBProvider 97L and passed to the JBFactory instance, passing it to every new EntityManager instance created by the JBFactory 56L).

I think a new JBFactory constructor or a specific method for passing an existing Jena's model should be introduced. Or am I missing something?

Thanks a lot! 

Best,
Nikola

Email: nikola....@gmail.com
URL:   nikola.milikic.info


Nikola Milikic

unread,
May 9, 2011, 10:31:58 AM5/9/11
to jenabe...@googlegroups.com
Hi Taylor,

I'm inspecting the jpa4jena code and it's not clear to me how is enabled to always get the same JVM object instance when loading the same Jenabean resource. I see there is a field cache of a type HashSet in the class JBEntityManager, but is is not used, for instance, when retrieving the instance from the Jena model in the method find. Actually, if I make a simple test where I persist an instance and then load it twice, I'm getting two different JVM objects.

I was thinking of trying to implement this caching on my own. And in order to deal with this issue, I was thinking of using Google's Guava MapMaker and store the objects as weak references in it, so garbage collector would automatically remove it. What do you think about this approach?

Thanks a lot for the answer!

Best,
Nikola

Taylor Cowan

unread,
May 9, 2011, 8:55:12 PM5/9/11
to Nikola Milikic, jenabe...@googlegroups.com
Good idea, let me know how it goes and well get it committed to the code base. 

Sent from my Windows Phone

From: Nikola Milikic
Sent: Monday, May 09, 2011 9:32 AM
To: jenabe...@googlegroups.com
Subject: Re: Multiple instances of the same resource

Reply all
Reply to author
Forward
0 new messages