You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Heiko Kopp <it...@daimler.com> on 2010/07/26 13:04:16 UTC

NullPointerException in pcGet-Method

Hello everybody,

I've had a weird situation with the OpenJPA Version 1.3 where I got a
NullPointerException inside a pcGet-Method. The Stacktrace is as follows:


java.lang.NullPointerException
	at XXX.model.EconomicRelationship.pcGetgroup(EconomicRelationship.java)
	at XXX.model.EconomicRelationship.getGroup(EconomicRelationship.java:316)
	at
XXX.GetEconomicUnitHandler.createOrGetCurrentUnit(GetEconomicUnitHandler.java:411)
	at
XXX.GetEconomicUnitHandler.fillEconomicUnits(GetEconomicUnitHandler.java:479)
	at
XXX.GetEconomicUnitHandler.handleGetEconomicUnit(GetEconomicUnitHandler.java:623)
	at
XXX.PartnerAccessServiceSoapBindingImpl.getEconomicUnit(PartnerAccessServiceSoapBindingImpl.java:89)
	at sun.reflect.GeneratedMethodAccessor80.invoke(Unknown Source)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:618)
	at
org.apache.axis2.jaxws.server.dispatcher.JavaDispatcher.invokeTargetOperation(JavaDispatcher.java:70)
	at
org.apache.axis2.jaxws.server.dispatcher.JavaBeanDispatcher.invoke(JavaBeanDispatcher.java:97)
	at
org.apache.axis2.jaxws.server.EndpointController.invoke(EndpointController.java:100)
	at
org.apache.axis2.jaxws.server.JAXWSMessageReceiver.receive(JAXWSMessageReceiver.java:142)
	at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:375)
	at
org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:305)
	at
com.ibm.ws.websvcs.transport.http.WASAxis2Servlet.doPost(WASAxis2Servlet.java:921)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
	at
com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1146)
	at
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:593)
	at
com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:534)
	at
com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:90)
	at
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:751)
	at
com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1478)
	at
com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:126)
	at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
	at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
	at
com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)
	at
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
	at
com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
	at
com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
	at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
	at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:196)
	at
com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:751)
	at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:881)
	at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1497)


I've decompiled the generated pcGetgroup() method:


 private static final EconomicRelationshipGroup
pcGetgroup(EconomicRelationship paramEconomicRelationship)
  {
    if (paramEconomicRelationship.pcStateManager == null)
      return paramEconomicRelationship.group;
    int i = pcInheritedFieldCount + 1;
    paramEconomicRelationship.pcStateManager.accessingField(i);
    return paramEconomicRelationship.group;
  }


It will be called by getGroup with its parameter set to self, leading to the
assumption that paramEconomicRelationship is not 'null'. Thus the only
possible NPE can occur when pcStateManager is null.

Can someone shed some light on me why the pcStateManager can be 'null'? 

Some more information about the system environment:

OpenJPA Version 1.3.0 (SNAPSHOT), the only version information i found was
Bnd-LastModified: 1270704087063
Websphere 6.1.0.29 with EJB 3.0 and Webservice Feature Pack.

The system does NOT have exclusive access to the data. External modification
of the entities occurs and is taken care of by using refresh() on every
delivered entity (even twice sometimes as EntityNotFoundException and/or
ArgumentException may occur).

The Entity itself that countains the 'group' member is defined as follows:


@Entity
@Table(name = "WBZ")
@NamedQueries({
    ....
})
public class EconomicRelationship
{
    @EmbeddedId
    private EconomicRelationshipKey key;

    ....

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "GRP_NR", referencedColumnName = "GRP_NR", unique =
true)
    private EconomicRelationshipGroup group;
    
    ....
}


I'm very thankful for any help or idea you may have. So far this is been a
single failure and we where not able to reproduce it. The whole system is
highly multithreaded, the multithreaded property for openJPA is set in
persistence.xml. It is taken care that entity managers are thread safe by
the use of ThreadLocal.

Sincerely,

Heiko
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5337474.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Re: NullPointerException in pcGet-Method

Posted by Michael Dick <mi...@gmail.com>.
Hi Heiko, comments below.

On Fri, Jul 30, 2010 at 2:03 AM, <it...@daimler.com> wrote:

> Helo Michael,
>
> I'm almost entirely sure that there is no sharing between threads. In our
> system the webservice handling method is created via ThreadLocal. There
> may be about 100 parallel handlers, each having its own thread for
> handling requests. When a handler starts, it obtains an EntityManager that
> is again created via ThreadLocal, thus there will be the same entity
> manager for the same thread. As much as I understand from openJPA, the
> entity manager handles the the entities and keeps track of them.
>
> We never close the entity manager, but rather let it open for reuse, but
> as noted only in the same thread protected by ThreadLocal. No new threads
> are spawned or created while handling a single webservice request. No
> entities are being stored anywhere in the request handling code, neither
> statically or as instances.
>
> This is the dilemma here, I've really put efford in finding out if the
> entites have been shared, but I simply can't find any situation where this
> can be true.
>

I can't dispute your research. It sounds like you've done a lot of looking.


> And just my two cents. Its fine that the state manager is nulled but in
> that case, this shall not lead to an NPE IMHO, don't you think?


Well that's the crux of the matter, I took another look at the code :

private static final EconomicRelationshipGroup
pcGetgroup(EconomicRelationship paramEconomicRelationship)
 {
   if (paramEconomicRelationship.pcStateManager == null)  // Point A:  we
check for null
     return paramEconomicRelationship.group;
   int i = pcInheritedFieldCount + 1;
   paramEconomicRelationship.pcStateManager.accessingField(i); // Point B:
NPE occurs.
   return paramEconomicRelationship.group;
 }

How can paramEconomicRelationship.pcStateManager be non-null at point A but
null at point B unless another thread has modified it? Either the entity (or
EntityManager) is accessed on multiple threads with one of them setting it
to null, or we're looking in the wrong spot.

What would help here is a testcase that reproduces the problem - maybe a
jUnit that spawns threads and saves the EMs in threadLocal?


> And even
> weirder is that in my error I do not even query a complex entity, like an
> embedded key or something. Its a simple Long-Value. That shall even WORK
> for detached objects, don't you think (again)? *smirk*
>

I'm not sure what you're saying here. Once an entity is detached you
certainly should not be able to access any unloaded fields regardless of
whether they're embedded. I think I'm missing the point of your assertion
though.

-mike


> Thanks for all your help in tracking this down.
>
> Best regards,
>
> Heiko
>
>
>
>
> michael.d.dick@gmail.com
> 29.07.2010 16:17
> Bitte antworten an
> users@openjpa.apache.org
>
>
> An
> users@openjpa.apache.org
> Kopie
>
> Thema
> Re: NullPointerException in pcGet-Method
>
>
>
>
>
>
> Disclaimer: there's been a lot of discussion on OPENJPA-453 - I haven't
> digested all of it.
>
> The testcase for OPENJPA-453 has a single entity (not EntityManager) being
> accessed by two threads simultaneously. Heiko, are you sure the entity
> instances are not shared between threads?
>
> The StateManager may be nulled out when the entity is detached or evicted
> (based on OPENJPA-453). So the only way you _should_ be able to get into
> this case is if the entity is accessed on multiple threads - one thread
> detaches / evicts and the other uses it.
>
> Christiaan, I don't think the statemanager will be nulled on refresh() -
> if
> you've seen differently that's good data.
>
> -mike
>
>
> On Wed, Jul 28, 2010 at 6:09 AM, Christiaan
> <ch...@hotmail.com>wrote:
>
> >
> > Hi Heiko,
> > your situation seems related to this one (also see related jira issue
> > http://issues.apache.org/jira/browse/OPENJPA-453):
> >
> >
>
> http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcReplaceField-td2979657.html#a2979657
>
> >
> > Although in this scenario the nullpointer relates to the statemanager of
> > the
> > embedded object. You do have an embedded primary key object, however the
> > nullpointer seems to happen in the owning object? I can imagine that
> > nullifying the statemanager happens with the refresh() call. My own
> > experience is that it happens with the evict() or commit() with
> > retainValues
> > set to false.
> >
> > Personally I think this really is a problem related to multithreading in
> > the
> > internal structures of OpenJPA, since the nullpointer happens in code
> > generated by OpenJPA. Accessing the statemanager and at the same time
> > nullifying it is something OpenJPA code is aware of, not the application
> > programmer.
> >
> > regards,
> > Christiaan
> > --
> > View this message in context:
> >
>
> http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345501.html
>
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>
>
>
> If you are not the intended addressee, please inform us immediately that
> you have received this e-mail in error, and delete it. We thank you for your
> cooperation.

Re: Re: NullPointerException in pcGet-Method

Posted by it...@daimler.com.
Helo Michael,

I'm almost entirely sure that there is no sharing between threads. In our 
system the webservice handling method is created via ThreadLocal. There 
may be about 100 parallel handlers, each having its own thread for 
handling requests. When a handler starts, it obtains an EntityManager that 
is again created via ThreadLocal, thus there will be the same entity 
manager for the same thread. As much as I understand from openJPA, the 
entity manager handles the the entities and keeps track of them. 

We never close the entity manager, but rather let it open for reuse, but 
as noted only in the same thread protected by ThreadLocal. No new threads 
are spawned or created while handling a single webservice request. No 
entities are being stored anywhere in the request handling code, neither 
statically or as instances.

This is the dilemma here, I've really put efford in finding out if the 
entites have been shared, but I simply can't find any situation where this 
can be true.

And just my two cents. Its fine that the state manager is nulled but in 
that case, this shall not lead to an NPE IMHO, don't you think? And even 
weirder is that in my error I do not even query a complex entity, like an 
embedded key or something. Its a simple Long-Value. That shall even WORK 
for detached objects, don't you think (again)? *smirk*

Thanks for all your help in tracking this down.

Best regards,

Heiko




michael.d.dick@gmail.com 
29.07.2010 16:17
Bitte antworten an
users@openjpa.apache.org


An
users@openjpa.apache.org
Kopie

Thema
Re: NullPointerException in pcGet-Method






Disclaimer: there's been a lot of discussion on OPENJPA-453 - I haven't
digested all of it.

The testcase for OPENJPA-453 has a single entity (not EntityManager) being
accessed by two threads simultaneously. Heiko, are you sure the entity
instances are not shared between threads?

The StateManager may be nulled out when the entity is detached or evicted
(based on OPENJPA-453). So the only way you _should_ be able to get into
this case is if the entity is accessed on multiple threads - one thread
detaches / evicts and the other uses it.

Christiaan, I don't think the statemanager will be nulled on refresh() - 
if
you've seen differently that's good data.

-mike


On Wed, Jul 28, 2010 at 6:09 AM, Christiaan 
<ch...@hotmail.com>wrote:

>
> Hi Heiko,
> your situation seems related to this one (also see related jira issue
> http://issues.apache.org/jira/browse/OPENJPA-453):
>
> 
http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcReplaceField-td2979657.html#a2979657

>
> Although in this scenario the nullpointer relates to the statemanager of
> the
> embedded object. You do have an embedded primary key object, however the
> nullpointer seems to happen in the owning object? I can imagine that
> nullifying the statemanager happens with the refresh() call. My own
> experience is that it happens with the evict() or commit() with
> retainValues
> set to false.
>
> Personally I think this really is a problem related to multithreading in
> the
> internal structures of OpenJPA, since the nullpointer happens in code
> generated by OpenJPA. Accessing the statemanager and at the same time
> nullifying it is something OpenJPA code is aware of, not the application
> programmer.
>
> regards,
> Christiaan
> --
> View this message in context:
> 
http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345501.html

> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

Re: NullPointerException in pcGet-Method

Posted by Michael Dick <mi...@gmail.com>.
Disclaimer: there's been a lot of discussion on OPENJPA-453 - I haven't
digested all of it.

The testcase for OPENJPA-453 has a single entity (not EntityManager) being
accessed by two threads simultaneously. Heiko, are you sure the entity
instances are not shared between threads?

The StateManager may be nulled out when the entity is detached or evicted
(based on OPENJPA-453). So the only way you _should_ be able to get into
this case is if the entity is accessed on multiple threads - one thread
detaches / evicts and the other uses it.

Christiaan, I don't think the statemanager will be nulled on refresh() - if
you've seen differently that's good data.

-mike


On Wed, Jul 28, 2010 at 6:09 AM, Christiaan <ch...@hotmail.com>wrote:

>
> Hi Heiko,
> your situation seems related to this one (also see related jira issue
> http://issues.apache.org/jira/browse/OPENJPA-453):
>
> http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcReplaceField-td2979657.html#a2979657
>
> Although in this scenario the nullpointer relates to the statemanager of
> the
> embedded object. You do have an embedded primary key object, however the
> nullpointer seems to happen in the owning object? I can imagine that
> nullifying the statemanager happens with the refresh() call. My own
> experience is that it happens with the evict() or commit() with
> retainValues
> set to false.
>
> Personally I think this really is a problem related to multithreading in
> the
> internal structures of OpenJPA, since the nullpointer happens in code
> generated by OpenJPA. Accessing the statemanager and at the same time
> nullifying it is something OpenJPA code is aware of, not the application
> programmer.
>
> regards,
> Christiaan
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345501.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: NullPointerException in pcGet-Method

Posted by Christiaan <ch...@hotmail.com>.
Hi Heiko,
your situation seems related to this one (also see related jira issue
http://issues.apache.org/jira/browse/OPENJPA-453):
http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcReplaceField-td2979657.html#a2979657

Although in this scenario the nullpointer relates to the statemanager of the
embedded object. You do have an embedded primary key object, however the
nullpointer seems to happen in the owning object? I can imagine that
nullifying the statemanager happens with the refresh() call. My own
experience is that it happens with the evict() or commit() with retainValues
set to false. 

Personally I think this really is a problem related to multithreading in the
internal structures of OpenJPA, since the nullpointer happens in code
generated by OpenJPA. Accessing the statemanager and at the same time
nullifying it is something OpenJPA code is aware of, not the application
programmer. 

regards,
Christiaan
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345501.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: NullPointerException in pcGet-Method

Posted by Heiko Kopp <it...@daimler.com>.
Yes I'm aware of this situation, but as I stated clear the whole system is
multithreaded but the application itself takes a lot of care by the use of
ThreadLocal and synchronized blocks.

The EntityManagers itself are created in a ThreadLocal as well as the
handlers for each webservice call, thus in each thread an own entity manager
exists. 

Therefore, there should be no two entities shared among two threads here, or
let me rephrase that: From the application point of view this cannot happen.
The only thing that might be wrong is IBM's J9 implementation of ThreadLocal
but I currently doubt this.

How can it happen, that "someone" in that case does "something" to set
pcStateManager 'null' and why. And even more 'how' ... what makes
pcStateManager become 'null'?

Your post does not clarify and just states things I've stated in my post.
I'm aware that the property for openJPA regarding multithreading is solely
for its internal structures. 

My problem here is actually very clear. Why is pcStateManager becoming
'null' even if the EntityManager that may cache the entities and the
entities can only be accessed by the very same thread.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345282.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: NullPointerException in pcGet-Method

Posted by Heiko Kopp <it...@daimler.com>.
Yes I'm aware of this situation, but as I stated clear the whole system is
multithreaded but the application itself takes a lot of care by the use of
ThreadLocal and synchronized blocks.

The EntityManagers itself are created in a ThreadLocal as well as the
handlers for each webservice call, thus in each thread an own entity manager
exists. 

Therefore, there should be no two entities shared among two threads here, or
let me rephrase that: From the application point of view this cannot happen.
The only thing that might be wrong is IBM's J9 implementation of ThreadLocal
but I currently doubt this.

How can it happen, that "someone" in that case does "something" to set
pcStateManager 'null' and why. And even more 'how' ... what makes
pcStateManager become 'null'?

Your post does not clarify and just states things I've stated in my post.
I'm aware that the property for openJPA regarding multithreading is solely
for its internal structures. 

My problem here is actually very clear. Why is pcStateManager becoming
'null' even if the EntityManager that may cache the entities and the
entities can only be accessed by the very same thread.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345237.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: NullPointerException in pcGet-Method

Posted by Pinaki Poddar <pp...@apache.org>.
openjpa.Multithreaded=true protects OpenJPA itself. It does not protect the
application. By that I mean, OpenJPA acquires thread guards to access its
own internal data structures when openjpa.Multithreaded is set to true. But
an application has to guard its own data structures for multi-threaded
access or mutation.  

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5342253.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: NullPointerException in pcGet-Method

Posted by Heiko Kopp <it...@daimler.com>.
Yes I'm aware of this situation, but as I stated clear the whole system is
multithreaded but the application itself takes a lot of care by the use of
ThreadLocal and synchronized blocks.

The EntityManagers itself are created in a ThreadLocal as well as the
handlers for each webservice call, thus in each thread an own entity manager
exists. 

Therefore, there should be no two entities shared among two threads here, or
let me rephrase that: From the application point of view this cannot happen.
The only thing that might be wrong is IBM's J9 implementation of ThreadLocal
but I currently doubt this.

How can it happen, that "someone" in that case does "something" to set
pcStateManager 'null' and why. And even more 'how' ... what makes
pcStateManager become 'null'?

Your post does not clarify and just states things I've stated in my post.
I'm aware that the property for openJPA regarding multithreading is solely
for its internal structures. 

My problem here is actually very clear. Why is pcStateManager becoming
'null' even if the EntityManager that may cache the entities and the
entities can only be accessed by the very same thread.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5345233.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: NullPointerException in pcGet-Method

Posted by Pinaki Poddar <pp...@apache.org>.

 private static final EconomicRelationshipGroup
pcGetgroup(EconomicRelationship paramEconomicRelationship)
  {
1:    if (paramEconomicRelationship.pcStateManager == null)
2:      return paramEconomicRelationship.group;
3:    int i = pcInheritedFieldCount + 1;
4:    paramEconomicRelationship.pcStateManager.accessingField(i);
5:    return paramEconomicRelationship.group;
  }

> The whole system is highly multithreaded,

One possibility:
Thread t1 entered this method when pcStateManager was not null. Hence failed
the check at line 1 went to Line 3. But by then another thread t2 did
"something" that nullified pcStateManager. Line 4 threw NPE. 

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/NullPointerException-in-pcGet-Method-tp5337474p5342244.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.