You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Arnaud Garcia <ar...@imagemed-87.com> on 2010/04/22 16:00:41 UTC

cayenne wicket unit test => Current thread has no bound ObjectContext

Hello,

I am working on a Wicket Cayenne application and when I launch the tests I
have an error when the DataContext is initialized:

This line, DataContext ctxt = (DataContext)
DataContext.getThreadObjectContext();  throws an

java.lang.IllegalStateException: Current thread has no bound ObjectContext


Well, any ideas to set up correctly the tests ?

thanks


Arnaud

Re: cayenne wicket unit test => Current thread has no bound ObjectContext

Posted by Joe Baldwin <jf...@earthlink.net>.
Arnaud,

Michael is the expert. But my understanding is that the current recommended procedure is to message the BaseContext. Example:

	ObjectContext oc = BaseContext.getThreadObjectContext();
	... create a query
	List list = oc.performQuery(query);

Joe

On Apr 22, 2010, at 11:59 AM, Arnaud Garcia wrote:

> thanks for answers...
> so,
> 
> Yes I am doing a Webapp and I have in my web.xml the cayenne filter....
> 
> I am not sure to understand what is the good way to retrieve the
> DataContext... Each time I need it I thought I just need to do
> (DataContext) DataContext.getThreadObjectContext();
> 
> So now I just have to do change the DataContext by the BaseContext
> BaseContext.getThreadObjectContext();
> 
> since the filter correctly initialized and bind it  to the Thread ....
> 
> is it ok ?
> 
> 
> 
> 
> 2010/4/22 Joe Baldwin <jf...@earthlink.net>
> 
>> Arnaud,
>> 
>> I read right past the "Wicket" reference; it appears that you are creating
>> a webapp.  Michael is correct.  The cayenne filter creates and binds the
>> ObjectContext for you in this case.
>> 
>> Sorry for the confusion,
>> Joe
>> 
>> 
>> On Apr 22, 2010, at 10:13 AM, Michael Gentry wrote:
>> 
>>> Did you configure your web.xml file to use the Cayenne web filter?  In
>>> my application (which is Tapestry, but should apply to Wicket, I
>>> think) I have:
>>> 
>>>   <filter>
>>>       <filter-name>Cayenne Filter</filter-name>
>>> 
>> <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
>>>   </filter>
>>>   <filter-mapping>
>>>       <filter-name>Cayenne Filter</filter-name>
>>>       <url-pattern>/*</url-pattern>
>>>   </filter-mapping>
>>> 
>>> mrg
>>> 
>>> 
>>> On Thu, Apr 22, 2010 at 10:00 AM, Arnaud Garcia <ar...@imagemed-87.com>
>> wrote:
>>>> Hello,
>>>> 
>>>> I am working on a Wicket Cayenne application and when I launch the tests
>> I
>>>> have an error when the DataContext is initialized:
>>>> 
>>>> This line, DataContext ctxt = (DataContext)
>>>> DataContext.getThreadObjectContext();  throws an
>>>> 
>>>> java.lang.IllegalStateException: Current thread has no bound
>> ObjectContext
>>>> 
>>>> 
>>>> Well, any ideas to set up correctly the tests ?
>>>> 
>>>> thanks
>>>> 
>>>> 
>>>> Arnaud
>>>> 
>> 
>> 


Re: cayenne wicket unit test => Current thread has no bound ObjectContext

Posted by Michael Gentry <mg...@masslight.net>.
In my case I had a common superclass with a getDataContext() method
which would retrieve the one stored/bound to the current thread by the
filter.  Here is the relevant code I used:


private DataContext dataContext;

public DataContext getDataContext()
{
    if (dataContext == null)
        dataContext = (DataContext) BaseContext.getThreadObjectContext();

    return dataContext;
}


The only reason I cache the dataContext is to save a slight amount of
processing time.  Probably overkill for my purposes.

Hope that helps,

mrg


On Thu, Apr 22, 2010 at 11:59 AM, Arnaud Garcia <ar...@imagemed-87.com> wrote:
> thanks for answers...
> so,
>
> Yes I am doing a Webapp and I have in my web.xml the cayenne filter....
>
> I am not sure to understand what is the good way to retrieve the
> DataContext... Each time I need it I thought I just need to do
> (DataContext) DataContext.getThreadObjectContext();
>
> So now I just have to do change the DataContext by the BaseContext
> BaseContext.getThreadObjectContext();
>
> since the filter correctly initialized and bind it  to the Thread ....
>
> is it ok ?
>
>
>
>
> 2010/4/22 Joe Baldwin <jf...@earthlink.net>
>
>> Arnaud,
>>
>> I read right past the "Wicket" reference; it appears that you are creating
>> a webapp.  Michael is correct.  The cayenne filter creates and binds the
>> ObjectContext for you in this case.
>>
>> Sorry for the confusion,
>> Joe
>>
>>
>> On Apr 22, 2010, at 10:13 AM, Michael Gentry wrote:
>>
>> > Did you configure your web.xml file to use the Cayenne web filter?  In
>> > my application (which is Tapestry, but should apply to Wicket, I
>> > think) I have:
>> >
>> >    <filter>
>> >        <filter-name>Cayenne Filter</filter-name>
>> >
>>  <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
>> >    </filter>
>> >    <filter-mapping>
>> >        <filter-name>Cayenne Filter</filter-name>
>> >        <url-pattern>/*</url-pattern>
>> >    </filter-mapping>
>> >
>> > mrg
>> >
>> >
>> > On Thu, Apr 22, 2010 at 10:00 AM, Arnaud Garcia <ar...@imagemed-87.com>
>> wrote:
>> >> Hello,
>> >>
>> >> I am working on a Wicket Cayenne application and when I launch the tests
>> I
>> >> have an error when the DataContext is initialized:
>> >>
>> >> This line, DataContext ctxt = (DataContext)
>> >> DataContext.getThreadObjectContext();  throws an
>> >>
>> >> java.lang.IllegalStateException: Current thread has no bound
>> ObjectContext
>> >>
>> >>
>> >> Well, any ideas to set up correctly the tests ?
>> >>
>> >> thanks
>> >>
>> >>
>> >> Arnaud
>> >>
>>
>>
>

Re: cayenne wicket unit test => Current thread has no bound ObjectContext

Posted by Arnaud Garcia <ar...@imagemed-87.com>.
thanks for answers...
so,

Yes I am doing a Webapp and I have in my web.xml the cayenne filter....

I am not sure to understand what is the good way to retrieve the
DataContext... Each time I need it I thought I just need to do
(DataContext) DataContext.getThreadObjectContext();

So now I just have to do change the DataContext by the BaseContext
BaseContext.getThreadObjectContext();

since the filter correctly initialized and bind it  to the Thread ....

is it ok ?




2010/4/22 Joe Baldwin <jf...@earthlink.net>

> Arnaud,
>
> I read right past the "Wicket" reference; it appears that you are creating
> a webapp.  Michael is correct.  The cayenne filter creates and binds the
> ObjectContext for you in this case.
>
> Sorry for the confusion,
> Joe
>
>
> On Apr 22, 2010, at 10:13 AM, Michael Gentry wrote:
>
> > Did you configure your web.xml file to use the Cayenne web filter?  In
> > my application (which is Tapestry, but should apply to Wicket, I
> > think) I have:
> >
> >    <filter>
> >        <filter-name>Cayenne Filter</filter-name>
> >
>  <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
> >    </filter>
> >    <filter-mapping>
> >        <filter-name>Cayenne Filter</filter-name>
> >        <url-pattern>/*</url-pattern>
> >    </filter-mapping>
> >
> > mrg
> >
> >
> > On Thu, Apr 22, 2010 at 10:00 AM, Arnaud Garcia <ar...@imagemed-87.com>
> wrote:
> >> Hello,
> >>
> >> I am working on a Wicket Cayenne application and when I launch the tests
> I
> >> have an error when the DataContext is initialized:
> >>
> >> This line, DataContext ctxt = (DataContext)
> >> DataContext.getThreadObjectContext();  throws an
> >>
> >> java.lang.IllegalStateException: Current thread has no bound
> ObjectContext
> >>
> >>
> >> Well, any ideas to set up correctly the tests ?
> >>
> >> thanks
> >>
> >>
> >> Arnaud
> >>
>
>

Re: cayenne wicket unit test => Current thread has no bound ObjectContext

Posted by Joe Baldwin <jf...@earthlink.net>.
Arnaud,

I read right past the "Wicket" reference; it appears that you are creating a webapp.  Michael is correct.  The cayenne filter creates and binds the ObjectContext for you in this case.

Sorry for the confusion,
Joe


On Apr 22, 2010, at 10:13 AM, Michael Gentry wrote:

> Did you configure your web.xml file to use the Cayenne web filter?  In
> my application (which is Tapestry, but should apply to Wicket, I
> think) I have:
> 
>    <filter>
>        <filter-name>Cayenne Filter</filter-name>
>        <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
>    </filter>
>    <filter-mapping>
>        <filter-name>Cayenne Filter</filter-name>
>        <url-pattern>/*</url-pattern>
>    </filter-mapping>
> 
> mrg
> 
> 
> On Thu, Apr 22, 2010 at 10:00 AM, Arnaud Garcia <ar...@imagemed-87.com> wrote:
>> Hello,
>> 
>> I am working on a Wicket Cayenne application and when I launch the tests I
>> have an error when the DataContext is initialized:
>> 
>> This line, DataContext ctxt = (DataContext)
>> DataContext.getThreadObjectContext();  throws an
>> 
>> java.lang.IllegalStateException: Current thread has no bound ObjectContext
>> 
>> 
>> Well, any ideas to set up correctly the tests ?
>> 
>> thanks
>> 
>> 
>> Arnaud
>> 


Re: cayenne wicket unit test => Current thread has no bound ObjectContext

Posted by Michael Gentry <mg...@masslight.net>.
Did you configure your web.xml file to use the Cayenne web filter?  In
my application (which is Tapestry, but should apply to Wicket, I
think) I have:

    <filter>
        <filter-name>Cayenne Filter</filter-name>
        <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Cayenne Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

mrg


On Thu, Apr 22, 2010 at 10:00 AM, Arnaud Garcia <ar...@imagemed-87.com> wrote:
> Hello,
>
> I am working on a Wicket Cayenne application and when I launch the tests I
> have an error when the DataContext is initialized:
>
> This line, DataContext ctxt = (DataContext)
> DataContext.getThreadObjectContext();  throws an
>
> java.lang.IllegalStateException: Current thread has no bound ObjectContext
>
>
> Well, any ideas to set up correctly the tests ?
>
> thanks
>
>
> Arnaud
>

Re: cayenne wicket unit test => Current thread has no bound ObjectContext

Posted by Joe Baldwin <jf...@earthlink.net>.
Arnaud,

The recommendation has been to transition to using the BaseContext in place of the older DataContext class methods.  (Ironically, it appears that the Cayenne 3.x docs have not all been updated and so in some places they show examples of BaseContext and some show examples of DataContext. This can be confusing.)

The proper method for initializing your ObjectContext depends on whether you are intending to create a stand-alone app, or you are creating a web app (i.e. a ServletContainer).  I am assuming from the error message that you have created a stand-alone app.  If so, then you should do the following:

1. Instantiate a DataContext.
2. Bind it using BaseContext.
3. Access later via the BaseContext class method
4. Execute queries using the returned objectContext (which in this case is really a DataContext instance)

example:
BaseContext.bindThreadObjectContext(DataContext.createDataContext());
...
Create some SelectQuery here
...

ObjectContext oc = BaseContext.getThreadObjectContext();
List list = oc.performQuery(query);

Please refer one of my previous emails to you and you will see an example of using the BaseContext.

Try this out, I think you will get better results.
Joe



On Apr 22, 2010, at 10:00 AM, Arnaud Garcia wrote:

> Hello,
> 
> I am working on a Wicket Cayenne application and when I launch the tests I
> have an error when the DataContext is initialized:
> 
> This line, DataContext ctxt = (DataContext)
> DataContext.getThreadObjectContext();  throws an
> 
> java.lang.IllegalStateException: Current thread has no bound ObjectContext
> 
> 
> Well, any ideas to set up correctly the tests ?
> 
> thanks
> 
> 
> Arnaud