You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Andreas Lundblad <an...@gmail.com> on 2013/06/26 22:32:10 UTC

Injecting DB service results in "IllegalStateException: EntityManager is closed"

I've just started using Wicket (I'm half way through Wicket in Action).

I've run into the following problem with a user registration form: In
order to make sure that the provided e-mail is not already registered
I've written a "UniqueEmailValidator" which I attach to the email
field. This validator needs access to the DB-service to query the
database for already registered email-adresses.

The problem is that the DB-service is injected when the registration
page is created and since I'm using open-session-in-view, the
DB-service EntityManager is closed after the page is rendered. This
causes an

  IllegalStateException: EntityManager is closed

once the form is submitted in a subsequent request. (Since the same
old DB-service object is used in the second request.)


(I had the same problem when trying to inject the DB-service in the
session-object. The same DB-service object was used in multiple
requests causing the IllegalStateException.)


I figured this must be a fairly common problem but can't for my life
find any solutions in the archives or through googling. I'm curious
what the best practice is to solve this.


(My project builds upon the code available here:
http://github.com/javadev/wicket-guice-demo )


best regards, Andreas Lundblad

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Martin Grigorov <mg...@apache.org>.
On Fri, Jun 28, 2013 at 11:15 AM, Martin Grigorov <mg...@apache.org>wrote:

> Hi,
>
>
> On Fri, Jun 28, 2013 at 12:43 AM, Bas Gooren <ba...@iswd.nl> wrote:
>
>> Hi Andreas,
>>
>> My guess is that the GuiceComponentInjector proxies are "detached" when
>> the page is serialized.
>> The last page (or the last N pages) are kept in memory by wicket (I
>> think, last I checked was some years ago).
>>
>> Checking ...
>> Yes, see LazyInitProxyFactory and its nested class JdkJHandler (which is
>> used for injected interfaces).
>> The proxy is replaced on serialization.
>>
>> Somehow I always thought the proxies were also detached when the page is
>> detach()-ed. Guess not :-)
>>
>
> This explains it!
> The last used page instance is stored in the HttpSession, and this keeps a
> reference to a proxy with a closed EntityManager.
>
> Wicket detaches all components and their default models, but there is no
> logic to null-ify the transient field in JdkHandler -
> https://github.com/apache/wicket/blob/master/wicket-ioc/src/main/java/org/apache/wicket/proxy/LazyInitProxyFactory.java?source=c#L375
>
> The simplest solution is to not keep the 'target' as a field at all. Just
> as the IProxyTargetLocator for the service at every usage. Will this be
> slow ?!
>

This should read:  Just ask ...


>
>
>>
>> Met vriendelijke groet,
>> Kind regards,
>>
>> Bas Gooren
>>
>> Op 27-6-2013 23:37, schreef Andreas Lundblad:
>>
>>  On Thu, Jun 27, 2013 at 11:25 PM, Bas Gooren <ba...@iswd.nl> wrote:
>>>
>>>> Hi Andreas,
>>>>
>>>> When your validator fails, you remain on the FormPage. A quick debug
>>>> session
>>>> shows me that the injected IService remains the same accross requests.
>>>>
>>> I've seen this too. I thought that this didn't matter since, as you
>>> said: "The GuiceComponentInjector which is set-up in the wicket
>>> application injects proxies".
>>>
>>>  The fix for your problem is simple, and a best practice anyway: inject
>>>> Provider<EntityManager> in your service.
>>>>
>>>> It's a best practice since the service does not know the "scope" of the
>>>> entitymanager, all it knows is that the entitymanager has a different
>>>> lifecycle; Your service may be instantiated and kept around for the time
>>>> your app is running, but could also be instantiated on the fly when
>>>> needed.
>>>> That's one of the reasons why Provider<> was invented: if the scope is
>>>> different, a provider allows you to nicely deal with it.
>>>>
>>>> In case you are not familiar with providers: it has no negative
>>>> side-effects
>>>> in terms of performance.
>>>> The guice-persist module binds the EntityManager per request, so all
>>>> calls
>>>> to Provider<EntityManager> while in the same request will yield the
>>>> exact
>>>> same EntityManager instance.
>>>>
>>>>
>>>>
>>>> PS See https://code.google.com/p/**google-guice/wiki/**
>>>> InjectingProviders<https://code.google.com/p/google-guice/wiki/InjectingProviders>(header "Providers for Mixing Scopes").
>>>>
>>> Thank you Bas for this excellent answer. Much appreciated.
>>>
>>> -- Andreas Lundblad
>>>
>>> ------------------------------**------------------------------**
>>> ---------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>

Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Martin Grigorov <mg...@apache.org>.
Hi,


On Fri, Jun 28, 2013 at 12:43 AM, Bas Gooren <ba...@iswd.nl> wrote:

> Hi Andreas,
>
> My guess is that the GuiceComponentInjector proxies are "detached" when
> the page is serialized.
> The last page (or the last N pages) are kept in memory by wicket (I think,
> last I checked was some years ago).
>
> Checking ...
> Yes, see LazyInitProxyFactory and its nested class JdkJHandler (which is
> used for injected interfaces).
> The proxy is replaced on serialization.
>
> Somehow I always thought the proxies were also detached when the page is
> detach()-ed. Guess not :-)


This explains it!
The last used page instance is stored in the HttpSession, and this keeps a
reference to a proxy with a closed EntityManager.

Wicket detaches all components and their default models, but there is no
logic to null-ify the transient field in JdkHandler -
https://github.com/apache/wicket/blob/master/wicket-ioc/src/main/java/org/apache/wicket/proxy/LazyInitProxyFactory.java?source=c#L375

The simplest solution is to not keep the 'target' as a field at all. Just
as the IProxyTargetLocator for the service at every usage. Will this be
slow ?!


>
> Met vriendelijke groet,
> Kind regards,
>
> Bas Gooren
>
> Op 27-6-2013 23:37, schreef Andreas Lundblad:
>
>  On Thu, Jun 27, 2013 at 11:25 PM, Bas Gooren <ba...@iswd.nl> wrote:
>>
>>> Hi Andreas,
>>>
>>> When your validator fails, you remain on the FormPage. A quick debug
>>> session
>>> shows me that the injected IService remains the same accross requests.
>>>
>> I've seen this too. I thought that this didn't matter since, as you
>> said: "The GuiceComponentInjector which is set-up in the wicket
>> application injects proxies".
>>
>>  The fix for your problem is simple, and a best practice anyway: inject
>>> Provider<EntityManager> in your service.
>>>
>>> It's a best practice since the service does not know the "scope" of the
>>> entitymanager, all it knows is that the entitymanager has a different
>>> lifecycle; Your service may be instantiated and kept around for the time
>>> your app is running, but could also be instantiated on the fly when
>>> needed.
>>> That's one of the reasons why Provider<> was invented: if the scope is
>>> different, a provider allows you to nicely deal with it.
>>>
>>> In case you are not familiar with providers: it has no negative
>>> side-effects
>>> in terms of performance.
>>> The guice-persist module binds the EntityManager per request, so all
>>> calls
>>> to Provider<EntityManager> while in the same request will yield the exact
>>> same EntityManager instance.
>>>
>>>
>>>
>>> PS See https://code.google.com/p/**google-guice/wiki/**
>>> InjectingProviders<https://code.google.com/p/google-guice/wiki/InjectingProviders>(header "Providers for Mixing Scopes").
>>>
>> Thank you Bas for this excellent answer. Much appreciated.
>>
>> -- Andreas Lundblad
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
Hi Andreas,

My guess is that the GuiceComponentInjector proxies are "detached" when 
the page is serialized.
The last page (or the last N pages) are kept in memory by wicket (I 
think, last I checked was some years ago).

Checking ...
Yes, see LazyInitProxyFactory and its nested class JdkJHandler (which is 
used for injected interfaces).
The proxy is replaced on serialization.

Somehow I always thought the proxies were also detached when the page is 
detach()-ed. Guess not :-)

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 27-6-2013 23:37, schreef Andreas Lundblad:
> On Thu, Jun 27, 2013 at 11:25 PM, Bas Gooren <ba...@iswd.nl> wrote:
>> Hi Andreas,
>>
>> When your validator fails, you remain on the FormPage. A quick debug session
>> shows me that the injected IService remains the same accross requests.
> I've seen this too. I thought that this didn't matter since, as you
> said: "The GuiceComponentInjector which is set-up in the wicket
> application injects proxies".
>
>> The fix for your problem is simple, and a best practice anyway: inject
>> Provider<EntityManager> in your service.
>>
>> It's a best practice since the service does not know the "scope" of the
>> entitymanager, all it knows is that the entitymanager has a different
>> lifecycle; Your service may be instantiated and kept around for the time
>> your app is running, but could also be instantiated on the fly when needed.
>> That's one of the reasons why Provider<> was invented: if the scope is
>> different, a provider allows you to nicely deal with it.
>>
>> In case you are not familiar with providers: it has no negative side-effects
>> in terms of performance.
>> The guice-persist module binds the EntityManager per request, so all calls
>> to Provider<EntityManager> while in the same request will yield the exact
>> same EntityManager instance.
>>
>>
>>
>> PS See https://code.google.com/p/google-guice/wiki/InjectingProviders (header "Providers for Mixing Scopes").
> Thank you Bas for this excellent answer. Much appreciated.
>
> -- Andreas Lundblad
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Andreas Lundblad <an...@gmail.com>.
On Thu, Jun 27, 2013 at 11:25 PM, Bas Gooren <ba...@iswd.nl> wrote:
> Hi Andreas,
>
> When your validator fails, you remain on the FormPage. A quick debug session
> shows me that the injected IService remains the same accross requests.

I've seen this too. I thought that this didn't matter since, as you
said: "The GuiceComponentInjector which is set-up in the wicket
application injects proxies".

> The fix for your problem is simple, and a best practice anyway: inject
> Provider<EntityManager> in your service.
>
> It's a best practice since the service does not know the "scope" of the
> entitymanager, all it knows is that the entitymanager has a different
> lifecycle; Your service may be instantiated and kept around for the time
> your app is running, but could also be instantiated on the fly when needed.
> That's one of the reasons why Provider<> was invented: if the scope is
> different, a provider allows you to nicely deal with it.
>
> In case you are not familiar with providers: it has no negative side-effects
> in terms of performance.
> The guice-persist module binds the EntityManager per request, so all calls
> to Provider<EntityManager> while in the same request will yield the exact
> same EntityManager instance.
>
>
>
> PS See https://code.google.com/p/google-guice/wiki/InjectingProviders (header "Providers for Mixing Scopes").

Thank you Bas for this excellent answer. Much appreciated.

-- Andreas Lundblad

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
Hi Andreas,

When your validator fails, you remain on the FormPage. A quick debug 
session shows me that the injected IService remains the same accross 
requests.
The fix for your problem is simple, and a best practice anyway: inject 
Provider<EntityManager> in your service.

It's a best practice since the service does not know the "scope" of the 
entitymanager, all it knows is that the entitymanager has a different 
lifecycle; Your service may be instantiated and kept around for the time 
your app is running, but could also be instantiated on the fly when needed.
That's one of the reasons why Provider<> was invented: if the scope is 
different, a provider allows you to nicely deal with it.

In case you are not familiar with providers: it has no negative 
side-effects in terms of performance.
The guice-persist module binds the EntityManager per request, so all 
calls to Provider<EntityManager> while in the same request will yield 
the exact same EntityManager instance.

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 27-6-2013 22:03, schreef Andreas Lundblad:
> On Thu, Jun 27, 2013 at 3:59 PM, Bas Gooren <ba...@iswd.nl> wrote:
>> I'm not the one who posed the original question.
>>
>> Andreas, like I said: please show us some code, and we can help you pinpoint
>> your issue.
>
> Thanks for all the rapid help!
>
> I've created a minimal page that shows the error. Basically no
> difference from the original code (
> http://github.com/javadev/wicket-guice-demo )
>
> To reproduce:
>
> 1. Download and unzip http://aoeu.se/www/wicketguice-problem.zip
> 2. mvn jetty:run
> 3. Go to localhost:9090, then go to "Form page"
> 4. Try to submit "gavin"
> 5. Try to submit "gavin" again.
>
> This results in the 'EntityManager is closed' exception. No idea how
> to solve this. If it's unsolvable, I'll look into the Provider
> approach, but it would be really nice to manage without it.
>
> best regards,
> Andreas Lundblad
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
Hi Andreas,

When I debugged your code yesterday, I changed the injection in 
JpaService to Provider<EntityManager>, and the error was gone. See 
http://pastebin.com/6NwjcVt4

Please ensure that your Jetty instance is actually using the updated code;

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 28-6-2013 10:04, schreef Andreas Lundblad:
> On Thu, Jun 27, 2013 at 11:27 PM, Bas Gooren <ba...@iswd.nl> wrote:
>> PS See https://code.google.com/p/google-guice/wiki/InjectingProviders
>> (header "Providers for Mixing Scopes").
>>
>>
>> Met vriendelijke groet,
>> Kind regards,
>>
>> Bas Gooren
>>
> A (possibly stupid) follow-up question.
>
> To try out the idea that you proposed with a Provider, I changed
>
>      @Inject
>      IService service;
>      ...
>      service.xyz()
>
> to
>
>      @Inject
>      Provider<IService> service;
>      ...
>      service.get().xyz()
>
>
> But I still get the same "EntityManager is closed" error. (I also
> tried changing from EntityManager to Provider<EntityManager> in the
> JpaService. Same error.)
>
> Is this the correct way of using Guice Providers in Wicket? If no,
> where can I find examples/documentation on how to use Providers in
> Wicket? If yes, the "EntityManager is closed" error seems to be due to
> something else.
>
> best regards,
> Andreas Lundblad
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Andreas Lundblad <an...@gmail.com>.
On Thu, Jun 27, 2013 at 11:27 PM, Bas Gooren <ba...@iswd.nl> wrote:
> PS See https://code.google.com/p/google-guice/wiki/InjectingProviders
> (header "Providers for Mixing Scopes").
>
>
> Met vriendelijke groet,
> Kind regards,
>
> Bas Gooren
>

A (possibly stupid) follow-up question.

To try out the idea that you proposed with a Provider, I changed

    @Inject
    IService service;
    ...
    service.xyz()

to

    @Inject
    Provider<IService> service;
    ...
    service.get().xyz()


But I still get the same "EntityManager is closed" error. (I also
tried changing from EntityManager to Provider<EntityManager> in the
JpaService. Same error.)

Is this the correct way of using Guice Providers in Wicket? If no,
where can I find examples/documentation on how to use Providers in
Wicket? If yes, the "EntityManager is closed" error seems to be due to
something else.

best regards,
Andreas Lundblad

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
PS See https://code.google.com/p/google-guice/wiki/InjectingProviders 
(header "Providers for Mixing Scopes").

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 27-6-2013 22:03, schreef Andreas Lundblad:
> On Thu, Jun 27, 2013 at 3:59 PM, Bas Gooren <ba...@iswd.nl> wrote:
>> I'm not the one who posed the original question.
>>
>> Andreas, like I said: please show us some code, and we can help you pinpoint
>> your issue.
>
> Thanks for all the rapid help!
>
> I've created a minimal page that shows the error. Basically no
> difference from the original code (
> http://github.com/javadev/wicket-guice-demo )
>
> To reproduce:
>
> 1. Download and unzip http://aoeu.se/www/wicketguice-problem.zip
> 2. mvn jetty:run
> 3. Go to localhost:9090, then go to "Form page"
> 4. Try to submit "gavin"
> 5. Try to submit "gavin" again.
>
> This results in the 'EntityManager is closed' exception. No idea how
> to solve this. If it's unsolvable, I'll look into the Provider
> approach, but it would be really nice to manage without it.
>
> best regards,
> Andreas Lundblad
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Andreas Lundblad <an...@gmail.com>.
On Thu, Jun 27, 2013 at 3:59 PM, Bas Gooren <ba...@iswd.nl> wrote:
> I'm not the one who posed the original question.
>
> Andreas, like I said: please show us some code, and we can help you pinpoint
> your issue.


Thanks for all the rapid help!

I've created a minimal page that shows the error. Basically no
difference from the original code (
http://github.com/javadev/wicket-guice-demo )

To reproduce:

1. Download and unzip http://aoeu.se/www/wicketguice-problem.zip
2. mvn jetty:run
3. Go to localhost:9090, then go to "Form page"
4. Try to submit "gavin"
5. Try to submit "gavin" again.

This results in the 'EntityManager is closed' exception. No idea how
to solve this. If it's unsolvable, I'll look into the Provider
approach, but it would be really nice to manage without it.

best regards,
Andreas Lundblad

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
I'm not the one who posed the original question.

Andreas, like I said: please show us some code, and we can help you 
pinpoint your issue.
In case your service is being serialized, a quick fix is to inject a 
Provider<EntityManager> instead of an EntityManager.

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 27-6-2013 14:25, schreef Martin Grigorov:
> Here are some discussions:
> http://stackoverflow.com/questions/10431640/guice-persist-attempting-to-execute-an-operation-on-a-closed-entitymanager
> http://stackoverflow.com/questions/14585505/jpa-guice-persist-permanently-opened-connection-issue
>
>
> On Thu, Jun 27, 2013 at 3:20 PM, Bas Gooren <ba...@iswd.nl> wrote:
>
>> I agree; Since the EntityManager is bound to the request (scope), it's
>> usually better to inject Provider<EntityManager>
>>
>> However, in the demo project the service is not a singleton, so it should
>> be fine. A new instance of the service is created for every injection.
>>
>>
>> Met vriendelijke groet,
>> Kind regards,
>>
>> Bas Gooren
>>
>> Op 27-6-2013 9:10, schreef Martin Grigorov:
>>
>>> Hi,
>>>
>>> I'm not sure whether this is the best solution but you can inject a
>>> Provider:
>>>
>>> @Inject
>>> private Provider<EntityManager> em;
>>>
>>> ...
>>>
>>> em.get().find(...);
>>>
>>>
>>> On Thu, Jun 27, 2013 at 9:36 AM, Bas Gooren <ba...@iswd.nl> wrote:
>>>
>>>   Hi,
>>>> Looking at the github project you refer to, it should work.
>>>> The GuiceComponentInjector which is set-up in the wicket application
>>>> injects proxies. That means that when your form is submitted (and a
>>>> second
>>>> request is made), a new service instance is created.
>>>>
>>>> So if it's not working for you, there is probably another problem with
>>>> your code.
>>>> Can you show us some relevant parts of your code, e.g. the validator and
>>>> the page which contains the validator?
>>>>
>>>> Met vriendelijke groet,
>>>> Kind regards,
>>>>
>>>> Bas Gooren
>>>>
>>>> Op 26-6-2013 22:32, schreef Andreas Lundblad:
>>>>
>>>>    I've just started using Wicket (I'm half way through Wicket in Action).
>>>>
>>>>> I've run into the following problem with a user registration form: In
>>>>> order to make sure that the provided e-mail is not already registered
>>>>> I've written a "UniqueEmailValidator" which I attach to the email
>>>>> field. This validator needs access to the DB-service to query the
>>>>> database for already registered email-adresses.
>>>>>
>>>>> The problem is that the DB-service is injected when the registration
>>>>> page is created and since I'm using open-session-in-view, the
>>>>> DB-service EntityManager is closed after the page is rendered. This
>>>>> causes an
>>>>>
>>>>>      IllegalStateException: EntityManager is closed
>>>>>
>>>>> once the form is submitted in a subsequent request. (Since the same
>>>>> old DB-service object is used in the second request.)
>>>>>
>>>>>
>>>>> (I had the same problem when trying to inject the DB-service in the
>>>>> session-object. The same DB-service object was used in multiple
>>>>> requests causing the IllegalStateException.)
>>>>>
>>>>>
>>>>> I figured this must be a fairly common problem but can't for my life
>>>>> find any solutions in the archives or through googling. I'm curious
>>>>> what the best practice is to solve this.
>>>>>
>>>>>
>>>>> (My project builds upon the code available here:
>>>>> http://github.com/javadev/****wicket-guice-demo<http://github.com/javadev/**wicket-guice-demo>
>>>>> <http://**github.com/javadev/wicket-**guice-demo<http://github.com/javadev/wicket-guice-demo>
>>>>>> )
>>>>>
>>>>> best regards, Andreas Lundblad
>>>>>
>>>>> ------------------------------****----------------------------**
>>>>> --**---------
>>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apa**che.org<http://apache.org>
>>>>> <us...@wicket.apache.org>
>>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>>
>>>>>
>>>>>


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Martin Grigorov <mg...@apache.org>.
Here are some discussions:
http://stackoverflow.com/questions/10431640/guice-persist-attempting-to-execute-an-operation-on-a-closed-entitymanager
http://stackoverflow.com/questions/14585505/jpa-guice-persist-permanently-opened-connection-issue


On Thu, Jun 27, 2013 at 3:20 PM, Bas Gooren <ba...@iswd.nl> wrote:

> I agree; Since the EntityManager is bound to the request (scope), it's
> usually better to inject Provider<EntityManager>
>
> However, in the demo project the service is not a singleton, so it should
> be fine. A new instance of the service is created for every injection.
>
>
> Met vriendelijke groet,
> Kind regards,
>
> Bas Gooren
>
> Op 27-6-2013 9:10, schreef Martin Grigorov:
>
>> Hi,
>>
>> I'm not sure whether this is the best solution but you can inject a
>> Provider:
>>
>> @Inject
>> private Provider<EntityManager> em;
>>
>> ...
>>
>> em.get().find(...);
>>
>>
>> On Thu, Jun 27, 2013 at 9:36 AM, Bas Gooren <ba...@iswd.nl> wrote:
>>
>>  Hi,
>>>
>>> Looking at the github project you refer to, it should work.
>>> The GuiceComponentInjector which is set-up in the wicket application
>>> injects proxies. That means that when your form is submitted (and a
>>> second
>>> request is made), a new service instance is created.
>>>
>>> So if it's not working for you, there is probably another problem with
>>> your code.
>>> Can you show us some relevant parts of your code, e.g. the validator and
>>> the page which contains the validator?
>>>
>>> Met vriendelijke groet,
>>> Kind regards,
>>>
>>> Bas Gooren
>>>
>>> Op 26-6-2013 22:32, schreef Andreas Lundblad:
>>>
>>>   I've just started using Wicket (I'm half way through Wicket in Action).
>>>
>>>> I've run into the following problem with a user registration form: In
>>>> order to make sure that the provided e-mail is not already registered
>>>> I've written a "UniqueEmailValidator" which I attach to the email
>>>> field. This validator needs access to the DB-service to query the
>>>> database for already registered email-adresses.
>>>>
>>>> The problem is that the DB-service is injected when the registration
>>>> page is created and since I'm using open-session-in-view, the
>>>> DB-service EntityManager is closed after the page is rendered. This
>>>> causes an
>>>>
>>>>     IllegalStateException: EntityManager is closed
>>>>
>>>> once the form is submitted in a subsequent request. (Since the same
>>>> old DB-service object is used in the second request.)
>>>>
>>>>
>>>> (I had the same problem when trying to inject the DB-service in the
>>>> session-object. The same DB-service object was used in multiple
>>>> requests causing the IllegalStateException.)
>>>>
>>>>
>>>> I figured this must be a fairly common problem but can't for my life
>>>> find any solutions in the archives or through googling. I'm curious
>>>> what the best practice is to solve this.
>>>>
>>>>
>>>> (My project builds upon the code available here:
>>>> http://github.com/javadev/****wicket-guice-demo<http://github.com/javadev/**wicket-guice-demo>
>>>> <http://**github.com/javadev/wicket-**guice-demo<http://github.com/javadev/wicket-guice-demo>
>>>> >)
>>>>
>>>>
>>>> best regards, Andreas Lundblad
>>>>
>>>> ------------------------------****----------------------------**
>>>> --**---------
>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apa**che.org<http://apache.org>
>>>> <us...@wicket.apache.org>
>>>> >
>>>>
>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>
>>>>
>>>>
>

Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
I agree; Since the EntityManager is bound to the request (scope), it's 
usually better to inject Provider<EntityManager>

However, in the demo project the service is not a singleton, so it 
should be fine. A new instance of the service is created for every 
injection.

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 27-6-2013 9:10, schreef Martin Grigorov:
> Hi,
>
> I'm not sure whether this is the best solution but you can inject a
> Provider:
>
> @Inject
> private Provider<EntityManager> em;
>
> ...
>
> em.get().find(...);
>
>
> On Thu, Jun 27, 2013 at 9:36 AM, Bas Gooren <ba...@iswd.nl> wrote:
>
>> Hi,
>>
>> Looking at the github project you refer to, it should work.
>> The GuiceComponentInjector which is set-up in the wicket application
>> injects proxies. That means that when your form is submitted (and a second
>> request is made), a new service instance is created.
>>
>> So if it's not working for you, there is probably another problem with
>> your code.
>> Can you show us some relevant parts of your code, e.g. the validator and
>> the page which contains the validator?
>>
>> Met vriendelijke groet,
>> Kind regards,
>>
>> Bas Gooren
>>
>> Op 26-6-2013 22:32, schreef Andreas Lundblad:
>>
>>   I've just started using Wicket (I'm half way through Wicket in Action).
>>> I've run into the following problem with a user registration form: In
>>> order to make sure that the provided e-mail is not already registered
>>> I've written a "UniqueEmailValidator" which I attach to the email
>>> field. This validator needs access to the DB-service to query the
>>> database for already registered email-adresses.
>>>
>>> The problem is that the DB-service is injected when the registration
>>> page is created and since I'm using open-session-in-view, the
>>> DB-service EntityManager is closed after the page is rendered. This
>>> causes an
>>>
>>>     IllegalStateException: EntityManager is closed
>>>
>>> once the form is submitted in a subsequent request. (Since the same
>>> old DB-service object is used in the second request.)
>>>
>>>
>>> (I had the same problem when trying to inject the DB-service in the
>>> session-object. The same DB-service object was used in multiple
>>> requests causing the IllegalStateException.)
>>>
>>>
>>> I figured this must be a fairly common problem but can't for my life
>>> find any solutions in the archives or through googling. I'm curious
>>> what the best practice is to solve this.
>>>
>>>
>>> (My project builds upon the code available here:
>>> http://github.com/javadev/**wicket-guice-demo<http://github.com/javadev/wicket-guice-demo>)
>>>
>>>
>>> best regards, Andreas Lundblad
>>>
>>> ------------------------------**------------------------------**---------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>


Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

I'm not sure whether this is the best solution but you can inject a
Provider:

@Inject
private Provider<EntityManager> em;

...

em.get().find(...);


On Thu, Jun 27, 2013 at 9:36 AM, Bas Gooren <ba...@iswd.nl> wrote:

> Hi,
>
> Looking at the github project you refer to, it should work.
> The GuiceComponentInjector which is set-up in the wicket application
> injects proxies. That means that when your form is submitted (and a second
> request is made), a new service instance is created.
>
> So if it's not working for you, there is probably another problem with
> your code.
> Can you show us some relevant parts of your code, e.g. the validator and
> the page which contains the validator?
>
> Met vriendelijke groet,
> Kind regards,
>
> Bas Gooren
>
> Op 26-6-2013 22:32, schreef Andreas Lundblad:
>
>  I've just started using Wicket (I'm half way through Wicket in Action).
>>
>> I've run into the following problem with a user registration form: In
>> order to make sure that the provided e-mail is not already registered
>> I've written a "UniqueEmailValidator" which I attach to the email
>> field. This validator needs access to the DB-service to query the
>> database for already registered email-adresses.
>>
>> The problem is that the DB-service is injected when the registration
>> page is created and since I'm using open-session-in-view, the
>> DB-service EntityManager is closed after the page is rendered. This
>> causes an
>>
>>    IllegalStateException: EntityManager is closed
>>
>> once the form is submitted in a subsequent request. (Since the same
>> old DB-service object is used in the second request.)
>>
>>
>> (I had the same problem when trying to inject the DB-service in the
>> session-object. The same DB-service object was used in multiple
>> requests causing the IllegalStateException.)
>>
>>
>> I figured this must be a fairly common problem but can't for my life
>> find any solutions in the archives or through googling. I'm curious
>> what the best practice is to solve this.
>>
>>
>> (My project builds upon the code available here:
>> http://github.com/javadev/**wicket-guice-demo<http://github.com/javadev/wicket-guice-demo>)
>>
>>
>> best regards, Andreas Lundblad
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

Re: Injecting DB service results in "IllegalStateException: EntityManager is closed"

Posted by Bas Gooren <ba...@iswd.nl>.
Hi,

Looking at the github project you refer to, it should work.
The GuiceComponentInjector which is set-up in the wicket application 
injects proxies. That means that when your form is submitted (and a 
second request is made), a new service instance is created.

So if it's not working for you, there is probably another problem with 
your code.
Can you show us some relevant parts of your code, e.g. the validator and 
the page which contains the validator?

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 26-6-2013 22:32, schreef Andreas Lundblad:
> I've just started using Wicket (I'm half way through Wicket in Action).
>
> I've run into the following problem with a user registration form: In
> order to make sure that the provided e-mail is not already registered
> I've written a "UniqueEmailValidator" which I attach to the email
> field. This validator needs access to the DB-service to query the
> database for already registered email-adresses.
>
> The problem is that the DB-service is injected when the registration
> page is created and since I'm using open-session-in-view, the
> DB-service EntityManager is closed after the page is rendered. This
> causes an
>
>    IllegalStateException: EntityManager is closed
>
> once the form is submitted in a subsequent request. (Since the same
> old DB-service object is used in the second request.)
>
>
> (I had the same problem when trying to inject the DB-service in the
> session-object. The same DB-service object was used in multiple
> requests causing the IllegalStateException.)
>
>
> I figured this must be a fairly common problem but can't for my life
> find any solutions in the archives or through googling. I'm curious
> what the best practice is to solve this.
>
>
> (My project builds upon the code available here:
> http://github.com/javadev/wicket-guice-demo )
>
>
> best regards, Andreas Lundblad
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>