You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cayenne.apache.org by Rich Tuers <rt...@oildex.com> on 2013/02/14 06:19:20 UTC

BaseContext

Hello,

We are using Cayenne 3.0 and recently implemented the WebApplicationContextFilter and we are using BaseContext.getThreadObjectContext().

This is working great for our JSF Web Application.  However we introduced Apache Camel into our tech stack.  Our Front-End Web Application post a message to a queue that our backend picks up, processes the request and then puts the return results back on a queue which the front end is listening too.  When the Front-End receives the information I then want to insert new records into the database.  Therefore I try to get a BaseContext.getThreadObjectContext() however it throws a "java.lang.IllegalStateException: Current thread has no bound ObjectContext".  So it appears the there is no ThreadLocal to attach to and we are likely outside of the WebApplicationContext.  Everything works great, when I am within our JSF application flow.

I've tried to catch the illegal state error and then return a DataContext.createDataContext().   Unfortunately this did not work either, I get an error saying something like "the object is not part of this DataContext".  So I already have the parent object from the original request, I then use camel to send a request to the back-end, which returns me data that I need to create the children.  I try setting the parent on the child and I get this error.

So I'm not sure what to try next.  Any suggestions would be greatly appreciated.

Thank you,
Rich

Re: BaseContext

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 14/02/13 4:19pm, Rich Tuers wrote:
> So I'm not sure what to try next.  Any suggestions would be greatly appreciated.

I am not sure of a solution to your problem, but you'll likely have a better response if you post to the user list. This list is for the development of Cayenne itself.

Cheers
Ari

-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: BaseContext

Posted by Rich Tuers <rt...@oildex.com>.
Thank you. Just tried this and it works great.  Thanks again.




On 2/14/13 8:47 AM, "John Huss" <jo...@gmail.com> wrote:

>You have to bind a context to the thread:
>http://cayenne.apache.org/docs/3.0/api/org/apache/cayenne/BaseContext.html
>#bindThreadObjectContext(org.apache.cayenne.ObjectContext)
>
>Usually this is done by WebApplicationContextFilter.  But if you thread
>isn't associated with a request or isn't covered by the filter then you
>need to create and context and bind it yourself.  Also remember to unbind
>at some point as well.
>
>
>On Thu, Feb 14, 2013 at 9:40 AM, Rich Tuers <rt...@oildex.com> wrote:
>
>> Yes, I thought about doing that and this will probably address this one
>> issue.  I guess my real question is how do I keep from
>> BaseContext.getThreadObjectContext() from throwing that
>> IllegalStateException.  I hit the save button in the UI, create the
>> "Parent" objects which does get the ObjectContext back from
>> BaseContext.getThreadObjectContext().   However before I create the
>>child
>> objects I put a message on a queue that Camel picks up, processes the
>> information and then puts the child object data back on the queue.
>>Which
>> I then pickup and try to create the new children with, using the
>> BaseContext.getThreadObjectContext().  At this point, it throws the
>> Illegal state exception.  I'm assuming once I go to Camel I lose the
>> ThreadLocal and therefore get this exception.
>>
>> Basic Event Flow
>> -------------------
>> 1. User hits Save
>> 2. Controller calls "Front-End" service to create Parent Object.  Parent
>> is successfully saved to the database.
>> 3. Front-end service makes a call to the "back-end" service via Camel
>>(to
>> get child data).
>> 4. back-end service sends data back to the front-end service.
>> 5. Front-end service tries to create the child objects but gets the
>> IllegalStateException.
>>
>>
>> public ObjectContext getDataContext() {
>>         try {
>>                 // Successful when creating the parent
>>                 return BaseContext.getThreadObjectContext();
>>         } catch (Exception ex) {
>>                 // Added this code since I was getting the exception.
>>                 // Trying to figure out how to avoid this exception
>>                 return DataContext.createDataContext();
>>         }
>>     }
>>
>>
>>
>> Thanks,
>> Rich
>>
>>
>>
>>
>>
>> On 2/14/13 8:24 AM, "John Huss" <jo...@gmail.com> wrote:
>>
>> >Try:
>> >
>> >parentObject = myNewContext.localObject(parentObject);
>> >
>> >Then proceed with the rest.  Then both objects will be in the same
>> >ObjectContext.
>> >
>> >
>> >On Thu, Feb 14, 2013 at 9:08 AM, Rich Tuers <rt...@oildex.com> wrote:
>> >
>> >> >Hello,
>> >> >
>> >> >We are using Cayenne 3.0 and recently implemented the
>> >> >WebApplicationContextFilter and we are using
>> >> >BaseContext.getThreadObjectContext().
>> >> >
>> >> >This is working great for our JSF Web Application.  However we
>> >>introduced
>> >> >Apache Camel into our tech stack.  Our Front-End Web Application
>>post a
>> >> >message to a queue that our backend picks up, processes the request
>>and
>> >> >then puts the return results back on a queue which the front end is
>> >> >listening too.  When the Front-End receives the information I then
>>want
>> >> >to insert new records into the database.  Therefore I try to get a
>> >> >BaseContext.getThreadObjectContext() however it throws a
>> >> >"java.lang.IllegalStateException: Current thread has no bound
>> >> >ObjectContext".  So it appears the there is no ThreadLocal to
>>attach to
>> >> >and we are likely outside of the WebApplicationContext.  Everything
>> >>works
>> >> >great, when I am within our JSF application flow.
>> >> >
>> >> >I've tried to catch the illegal state error and then return a
>> >> >DataContext.createDataContext().   Unfortunately this did not work
>> >> >either, I get an error saying something like "the object is not
>>part of
>> >> >this DataContext".  So I already have the parent object from the
>> >>original
>> >> >request, I then use camel to send a request to the back-end, which
>> >> >returns me data that I need to create the children.  I try setting
>>the
>> >> >parent on the child and I get this error.
>> >> >
>> >> >So I'm not sure what to try next.  Any suggestions would be greatly
>> >> >appreciated.
>> >> >
>> >> >Thank you,
>> >> >Rich
>> >>
>> >>
>>
>>


Re: BaseContext

Posted by John Huss <jo...@gmail.com>.
You have to bind a context to the thread:
http://cayenne.apache.org/docs/3.0/api/org/apache/cayenne/BaseContext.html#bindThreadObjectContext(org.apache.cayenne.ObjectContext)

Usually this is done by WebApplicationContextFilter.  But if you thread
isn't associated with a request or isn't covered by the filter then you
need to create and context and bind it yourself.  Also remember to unbind
at some point as well.


On Thu, Feb 14, 2013 at 9:40 AM, Rich Tuers <rt...@oildex.com> wrote:

> Yes, I thought about doing that and this will probably address this one
> issue.  I guess my real question is how do I keep from
> BaseContext.getThreadObjectContext() from throwing that
> IllegalStateException.  I hit the save button in the UI, create the
> "Parent" objects which does get the ObjectContext back from
> BaseContext.getThreadObjectContext().   However before I create the child
> objects I put a message on a queue that Camel picks up, processes the
> information and then puts the child object data back on the queue.  Which
> I then pickup and try to create the new children with, using the
> BaseContext.getThreadObjectContext().  At this point, it throws the
> Illegal state exception.  I'm assuming once I go to Camel I lose the
> ThreadLocal and therefore get this exception.
>
> Basic Event Flow
> -------------------
> 1. User hits Save
> 2. Controller calls "Front-End" service to create Parent Object.  Parent
> is successfully saved to the database.
> 3. Front-end service makes a call to the "back-end" service via Camel (to
> get child data).
> 4. back-end service sends data back to the front-end service.
> 5. Front-end service tries to create the child objects but gets the
> IllegalStateException.
>
>
> public ObjectContext getDataContext() {
>         try {
>                 // Successful when creating the parent
>                 return BaseContext.getThreadObjectContext();
>         } catch (Exception ex) {
>                 // Added this code since I was getting the exception.
>                 // Trying to figure out how to avoid this exception
>                 return DataContext.createDataContext();
>         }
>     }
>
>
>
> Thanks,
> Rich
>
>
>
>
>
> On 2/14/13 8:24 AM, "John Huss" <jo...@gmail.com> wrote:
>
> >Try:
> >
> >parentObject = myNewContext.localObject(parentObject);
> >
> >Then proceed with the rest.  Then both objects will be in the same
> >ObjectContext.
> >
> >
> >On Thu, Feb 14, 2013 at 9:08 AM, Rich Tuers <rt...@oildex.com> wrote:
> >
> >> >Hello,
> >> >
> >> >We are using Cayenne 3.0 and recently implemented the
> >> >WebApplicationContextFilter and we are using
> >> >BaseContext.getThreadObjectContext().
> >> >
> >> >This is working great for our JSF Web Application.  However we
> >>introduced
> >> >Apache Camel into our tech stack.  Our Front-End Web Application post a
> >> >message to a queue that our backend picks up, processes the request and
> >> >then puts the return results back on a queue which the front end is
> >> >listening too.  When the Front-End receives the information I then want
> >> >to insert new records into the database.  Therefore I try to get a
> >> >BaseContext.getThreadObjectContext() however it throws a
> >> >"java.lang.IllegalStateException: Current thread has no bound
> >> >ObjectContext".  So it appears the there is no ThreadLocal to attach to
> >> >and we are likely outside of the WebApplicationContext.  Everything
> >>works
> >> >great, when I am within our JSF application flow.
> >> >
> >> >I've tried to catch the illegal state error and then return a
> >> >DataContext.createDataContext().   Unfortunately this did not work
> >> >either, I get an error saying something like "the object is not part of
> >> >this DataContext".  So I already have the parent object from the
> >>original
> >> >request, I then use camel to send a request to the back-end, which
> >> >returns me data that I need to create the children.  I try setting the
> >> >parent on the child and I get this error.
> >> >
> >> >So I'm not sure what to try next.  Any suggestions would be greatly
> >> >appreciated.
> >> >
> >> >Thank you,
> >> >Rich
> >>
> >>
>
>

Re: BaseContext

Posted by Rich Tuers <rt...@oildex.com>.
Yes, I thought about doing that and this will probably address this one
issue.  I guess my real question is how do I keep from
BaseContext.getThreadObjectContext() from throwing that
IllegalStateException.  I hit the save button in the UI, create the
"Parent" objects which does get the ObjectContext back from
BaseContext.getThreadObjectContext().   However before I create the child
objects I put a message on a queue that Camel picks up, processes the
information and then puts the child object data back on the queue.  Which
I then pickup and try to create the new children with, using the
BaseContext.getThreadObjectContext().  At this point, it throws the
Illegal state exception.  I'm assuming once I go to Camel I lose the
ThreadLocal and therefore get this exception.

Basic Event Flow
-------------------
1. User hits Save
2. Controller calls "Front-End" service to create Parent Object.  Parent
is successfully saved to the database.
3. Front-end service makes a call to the "back-end" service via Camel (to
get child data).
4. back-end service sends data back to the front-end service.
5. Front-end service tries to create the child objects but gets the
IllegalStateException.


public ObjectContext getDataContext() {
	try {
		// Successful when creating the parent
		return BaseContext.getThreadObjectContext();
	} catch (Exception ex) {
		// Added this code since I was getting the exception.
		// Trying to figure out how to avoid this exception
		return DataContext.createDataContext();
	}
    }



Thanks,
Rich


   


On 2/14/13 8:24 AM, "John Huss" <jo...@gmail.com> wrote:

>Try:
>
>parentObject = myNewContext.localObject(parentObject);
>
>Then proceed with the rest.  Then both objects will be in the same
>ObjectContext.
>
>
>On Thu, Feb 14, 2013 at 9:08 AM, Rich Tuers <rt...@oildex.com> wrote:
>
>> >Hello,
>> >
>> >We are using Cayenne 3.0 and recently implemented the
>> >WebApplicationContextFilter and we are using
>> >BaseContext.getThreadObjectContext().
>> >
>> >This is working great for our JSF Web Application.  However we
>>introduced
>> >Apache Camel into our tech stack.  Our Front-End Web Application post a
>> >message to a queue that our backend picks up, processes the request and
>> >then puts the return results back on a queue which the front end is
>> >listening too.  When the Front-End receives the information I then want
>> >to insert new records into the database.  Therefore I try to get a
>> >BaseContext.getThreadObjectContext() however it throws a
>> >"java.lang.IllegalStateException: Current thread has no bound
>> >ObjectContext".  So it appears the there is no ThreadLocal to attach to
>> >and we are likely outside of the WebApplicationContext.  Everything
>>works
>> >great, when I am within our JSF application flow.
>> >
>> >I've tried to catch the illegal state error and then return a
>> >DataContext.createDataContext().   Unfortunately this did not work
>> >either, I get an error saying something like "the object is not part of
>> >this DataContext".  So I already have the parent object from the
>>original
>> >request, I then use camel to send a request to the back-end, which
>> >returns me data that I need to create the children.  I try setting the
>> >parent on the child and I get this error.
>> >
>> >So I'm not sure what to try next.  Any suggestions would be greatly
>> >appreciated.
>> >
>> >Thank you,
>> >Rich
>>
>>


Re: BaseContext

Posted by John Huss <jo...@gmail.com>.
Try:

parentObject = myNewContext.localObject(parentObject);

Then proceed with the rest.  Then both objects will be in the same
ObjectContext.


On Thu, Feb 14, 2013 at 9:08 AM, Rich Tuers <rt...@oildex.com> wrote:

> >Hello,
> >
> >We are using Cayenne 3.0 and recently implemented the
> >WebApplicationContextFilter and we are using
> >BaseContext.getThreadObjectContext().
> >
> >This is working great for our JSF Web Application.  However we introduced
> >Apache Camel into our tech stack.  Our Front-End Web Application post a
> >message to a queue that our backend picks up, processes the request and
> >then puts the return results back on a queue which the front end is
> >listening too.  When the Front-End receives the information I then want
> >to insert new records into the database.  Therefore I try to get a
> >BaseContext.getThreadObjectContext() however it throws a
> >"java.lang.IllegalStateException: Current thread has no bound
> >ObjectContext".  So it appears the there is no ThreadLocal to attach to
> >and we are likely outside of the WebApplicationContext.  Everything works
> >great, when I am within our JSF application flow.
> >
> >I've tried to catch the illegal state error and then return a
> >DataContext.createDataContext().   Unfortunately this did not work
> >either, I get an error saying something like "the object is not part of
> >this DataContext".  So I already have the parent object from the original
> >request, I then use camel to send a request to the back-end, which
> >returns me data that I need to create the children.  I try setting the
> >parent on the child and I get this error.
> >
> >So I'm not sure what to try next.  Any suggestions would be greatly
> >appreciated.
> >
> >Thank you,
> >Rich
>
>

BaseContext

Posted by Rich Tuers <rt...@oildex.com>.
>Hello,
>
>We are using Cayenne 3.0 and recently implemented the
>WebApplicationContextFilter and we are using
>BaseContext.getThreadObjectContext().
>
>This is working great for our JSF Web Application.  However we introduced
>Apache Camel into our tech stack.  Our Front-End Web Application post a
>message to a queue that our backend picks up, processes the request and
>then puts the return results back on a queue which the front end is
>listening too.  When the Front-End receives the information I then want
>to insert new records into the database.  Therefore I try to get a
>BaseContext.getThreadObjectContext() however it throws a
>"java.lang.IllegalStateException: Current thread has no bound
>ObjectContext".  So it appears the there is no ThreadLocal to attach to
>and we are likely outside of the WebApplicationContext.  Everything works
>great, when I am within our JSF application flow.
>
>I've tried to catch the illegal state error and then return a
>DataContext.createDataContext().   Unfortunately this did not work
>either, I get an error saying something like "the object is not part of
>this DataContext".  So I already have the parent object from the original
>request, I then use camel to send a request to the back-end, which
>returns me data that I need to create the children.  I try setting the
>parent on the child and I get this error.
>
>So I'm not sure what to try next.  Any suggestions would be greatly
>appreciated.
>
>Thank you,
>Rich