You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Erik Brakkee <er...@gmail.com> on 2010/07/29 21:54:11 UTC

Re: Wicket and JEE6

I have done a lot of experimentation at the moment and I am using CDI
dependency injection now for a simple wicket project. It is based on the
code at utils.wamblee.org (wicket/inject).

The issue I am running into is the following. I am creating a detachable
entity object which is simply a detachable model that safes the primary key
in onDetach() + sets the object to null.
It loads the object from persistent storage using entityManager.find(class,
key) in the get() method (if not yet initialized).

The detachable model is constructed using an entity manager obtained using
CDI. However, in the following scenario, things go wrong: (1) edit an entity
in a form, (2) click ok, (3) browser back.

What happens is that I get a NullPointerException in EntityManagerWrapper on
line 314 on glassfish v3. What is actually happening here is that the
detachable model is being serialized but the EntityManagerWrapper (the
contextual that delegates to the actual entitymanager to use) has transient
fields which become null after deserialization. The EntityManagerWrapper is
declared Serializable but in fact it isn't.

Now, I have to do two things. First I have to report an issue on glassfish
v3. I think the entity manager wrapper should be truly serializable and not
just declared serializable.  Second, I need to work around this problem. The
solution I am thinking of is to use a simple utility that provides a truly
serializable entity manager that delegates to an entity manager obtained
using CDI. That would solve the problem with the detachable.

However, there is one more issue which is the injection setup in the
application. I still need the injection at component instantiation to allow
use of injected objects in the constructor. But I still have a question
whether it would be required to also use a IComponentOnBeforeRenderListener
to do an additional injection in the onBeforeRender() method. The only
reason I would need to do this is when pages in wicket are serialized as
well. Do I need to take page serialization into account? Is there also a
callback in wicket to listen for component serialization and
deserialization?

Re: Wicket and JEE6

Posted by Erik Brakkee <er...@gmail.com>.
It appears that I ran into an existing Glassfish V3 issue. The same issue
occurs with passivation of regular EJBs that have an entitymanager injected.

See https://glassfish.dev.java.net/issues/show_bug.cgi?id=11356

Re: Wicket and JEE6

Posted by Erik Brakkee <er...@gmail.com>.
I have found an elegant solution to the serialization problem.
Basically, in the ComponentInstantiationListener that perform injection, I
am also attaching a new InjectionBehavior. The injection behavior has a
private transient boolean field indicating whether injections are up to
date. Upon serialization/deserialization of a component, this field is reset
to false because it is transient. As a result, I can detect in the
beforeRender() method of the behavior whether I need to do injection.

The code of the component instantiationlistener is now as follows:

    @Override
    public void onInstantiation(Component aComponent) {
        injector.inject(aComponent);
        aComponent.add(new InjectionBehavior());
    }

With the InjectionBahavior defined as follows:

public class InjectionBehavior extends AbstractBehavior {

    private transient boolean injectionUptodate;

    public InjectionBehavior() {
        injectionUptodate = true;
    }

    @Override
    public void beforeRender(Component aComponent) {
        if (!injectionUptodate) {
            InjectorBuilder.getInjector().inject(aComponent);
            injectionUptodate = true;
        }
    }
}

This solution prevents unnecessary injection. This is required because
injection is expensive. For a simple class with only one injection I
measured only 1400 injections per second (I understand the weld project is
working on improving performance at the moment).


Another part of the solution is to be able to deal with detachable models
that need to keep a reference to an entity manager. The problem is that
these detachable models are also serialized/deserialized. For this I am
using a similar trick now to create a dynamic proxy of an entity manager
that delegates to a dynamically looked up version of the entity manager. The
lookup is done by creating a dummy object with a @persistenceContet in it
and using CDI to inject/obtain the entity manager. Here similarly, the
lookup caches the entity manager as a transient reference so it knows when
to recompute the entity manager.

The object that does this lookup is in fact a managed bean with a producer
method annoted with @Produces @ApplicationDatabase and @PersistenceContext:

public class EntityManagerLookup implements Lookup {

    private static class EmHolder {
        @PersistenceContext
        private EntityManager em;

        public EntityManager getEm() {
            return em;
        }
    }

    private transient EntityManager em;

    @Override
    public Object lookup() throws Exception {
        if (em == null) {
            EmHolder holder = new EmHolder();
            InjectorBuilder.getInjector().inject(holder);
            em = holder.getEm();
        }
        return em;
    }

    @Produces
    @ApplicationDatabase
    @PersistenceContext
    public EntityManager getDefaultEntityManager() {
        LookupProxyFactory<EntityManager> factory = new
LookupProxyFactory<EntityManager>(
            EntityManager.class, new EntityManagerLookup());
        return factory.getProxy();
    }

}

Using this method, I can inject this serialization-safe entitymanager into
any object using

@Inject @ApplicationDatabase EntityManager entityManager;

So, in fact, I have a two-part solution. First of all it is robust in the
sense that @PersistenceContext still works. Second, I can also use a custom
injection to obtain serialization safe entity managers. When the entity
manager is obtained using the custom method however, problems can still
exist with for instance application scoped objects which also appear to be
serialized (this is in fact wrong as the objects will no longer be
application scoped because several copies are created).

A consequence of this solution is that all injected dependencies should be
declared as transient because this is more efficient.

The code for this can be found at http://utils.wamblee.org (wicket/inject,
support/inject, and support/cdi).

Re: Wicket and JEE6

Posted by Erik Brakkee <er...@gmail.com>.
On Thu, Jul 29, 2010 at 10:18 PM, James Carman
<ja...@carmanconsulting.com>wrote:

> The problem with using the AspectJ-injected references occurs when you
> pass your reference to another class (such as a model, for instance).
> That class may not be instrumented via AspectJ to handle the
> serialization/deserialization properly for that reference.  So, it
> will fail.
>
> I also think it is a dodgy approach with aspect J and that it's best to
avoid it. Also, because it involves additional configuration and more
complexity.

The simplest solution appears to be to inject using the onBeforeRender
listener. The disadvantage is however that I will be doing this injection
too many times (every construction and onBeforeRender).

Re: Wicket and JEE6

Posted by James Carman <ja...@carmanconsulting.com>.
The problem with using the AspectJ-injected references occurs when you
pass your reference to another class (such as a model, for instance).
That class may not be instrumented via AspectJ to handle the
serialization/deserialization properly for that reference.  So, it
will fail.


On Thu, Jul 29, 2010 at 4:14 PM, Erik Brakkee <er...@gmail.com> wrote:
> On Thu, Jul 29, 2010 at 9:54 PM, Erik Brakkee <er...@gmail.com>wrote:
>
>>  Is there also a callback in wicket to listen for component serialization
>> and deserialization?
>>
>
> Googling for this it also seems possible to use an aspectj pointcut to do
> injection at deserialization.
>

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


Re: Wicket and JEE6

Posted by Erik Brakkee <er...@gmail.com>.
On Thu, Jul 29, 2010 at 9:54 PM, Erik Brakkee <er...@gmail.com>wrote:

>  Is there also a callback in wicket to listen for component serialization
> and deserialization?
>

Googling for this it also seems possible to use an aspectj pointcut to do
injection at deserialization.