You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by "John Coleman (JIRA)" <ji...@apache.org> on 2012/11/14 02:44:12 UTC

[jira] [Created] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

John Coleman created TAP5-2027:
----------------------------------

             Summary: EntityManagerObjectProvider always provides the initial EntityManger proxy created
                 Key: TAP5-2027
                 URL: https://issues.apache.org/jira/browse/TAP5-2027
             Project: Tapestry 5
          Issue Type: Bug
          Components: tapestry-jpa
    Affects Versions: 5.3.6, 5.3.5
            Reporter: John Coleman


When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following patch fixes the issue and is provided as-is, free and without copyright or warranty:


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Coleman updated TAP5-2027:
-------------------------------

    Description: 
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




  was:
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following patch fixes the issue and is provided as-is, free and without copyright or warranty:


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




    
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "Lenny Primak (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496804#comment-13496804 ] 

Lenny Primak commented on TAP5-2027:
------------------------------------

I just took a quick look (but not tested) at your code, and I don't see how not caching a proxy has any difference.
Since the PlasticProxyFactory calls createObject() upon every method call.

I just tested a simple app with two PUs on the same page and it works fine.
I still say this is not an issue.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "Lenny Primak (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13498317#comment-13498317 ] 

Lenny Primak commented on TAP5-2027:
------------------------------------

I can reproduce the problem with your code.
DAO injection does not work properly, 
while direct injection into pages/components of @PersisgentContext does work.
I still don't see how this fix can possibly work.


                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "Lenny Primak (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497167#comment-13497167 ] 

Lenny Primak commented on TAP5-2027:
------------------------------------

I just tested that second scenario, without DAOs, but a different PU on each page directly injected into the page,
and that works fine.

The code you provided still should not make any difference.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497974#comment-13497974 ] 

John Coleman commented on TAP5-2027:
------------------------------------

I built a simple test application and reproduced the problem just as in my live app.

I can't see a way of attaching it here so I'll email it if you let me know where to send.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496957#comment-13496957 ] 

John Coleman commented on TAP5-2027:
------------------------------------

The case involves injection of EntityManager in separate DAO classes that are used by separate pages, not direct injection of different EntityManager into a single page. It seems the injection handling is different.

i.e.

Page1 -> DAOX -> @PersistenceContext(unitName=A) EntityManager
Page2 -> DAOY -> @PersistenceContext(unitName=B) EntityManager

For DAOY the reference to the EntityManager of unitName=A is provided when the DAO class implementation is built.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Coleman updated TAP5-2027:
-------------------------------

    Description: 
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.





  was:
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




    
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497896#comment-13497896 ] 

John Coleman commented on TAP5-2027:
------------------------------------

Maybe there is some code convention I have missed. Can you share your test code? I will see if I can produce a simple test case to reproduce this issue for sharing.

                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "Lenny Primak (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13498317#comment-13498317 ] 

Lenny Primak commented on TAP5-2027:
------------------------------------

I can reproduce the problem with your code.
DAO injection does not work properly, 
while direct injection into pages/components of @PersisgentContext does work.
I still don't see how this fix can possibly work.


                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Coleman updated TAP5-2027:
-------------------------------

    Attachment: TapestryJPATest.zip

navigate to the Alpha page, then the Beta page - the BetaDAO gets a reference to the Alpha DB unit
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Comment Edited] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497974#comment-13497974 ] 

John Coleman edited comment on TAP5-2027 at 11/15/12 12:21 PM:
---------------------------------------------------------------

I built a simple test application and reproduced the problem just as in my live app.

I've attached a zip file of the complete app.
                
      was (Author: johncoleman):
    I built a simple test application and reproduced the problem just as in my live app.

I can't see a way of attaching it here so I'll email it if you let me know where to send.
                  
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Coleman updated TAP5-2027:
-------------------------------

    Attachment: TapestryJPATest.zip

navigate to the Alpha page, then the Beta page - the BetaDAO gets a reference to the Alpha DB unit
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Coleman updated TAP5-2027:
-------------------------------

    Description: 
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




  was:
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following patch fixes the issue and is provided as-is, free and without copyright or warranty:


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




    
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "Lenny Primak (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496804#comment-13496804 ] 

Lenny Primak commented on TAP5-2027:
------------------------------------

I just took a quick look (but not tested) at your code, and I don't see how not caching a proxy has any difference.
Since the PlasticProxyFactory calls createObject() upon every method call.

I just tested a simple app with two PUs on the same page and it works fine.
I still say this is not an issue.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13498483#comment-13498483 ] 

John Coleman commented on TAP5-2027:
------------------------------------

Well, the fix defo works.

The documentation specifically uses this example that was my model:

public class UserDAOImpl implements UserDAO {
   @Inject
   @PersistenceContext(unitName = "DemoUnit")
   private EntityManager entityManager;

   ...
}

so a disappointment.

If I can make time to generate a git patch I'll have a go. In the meantime here is the workaround code:

[This code is freely released without copyright or any other restrictions or conditions. Use at your own risk.]

package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A fixed version to use PlasticProxyFactory and not cache the EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator objectLocator)
    {
            final PlasticProxyFactory proxyFactory = objectLocator.getService("PlasticProxyFactory",
            		PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = proxyFactory.createProxy(EntityManager.class, new ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = objectLocator
                            .getService(EntityManagerManager.class);

                    return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

}

                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Comment Edited] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497974#comment-13497974 ] 

John Coleman edited comment on TAP5-2027 at 11/15/12 12:21 PM:
---------------------------------------------------------------

I built a simple test application and reproduced the problem just as in my live app.

I've attached a zip file of the complete app.
                
      was (Author: johncoleman):
    I built a simple test application and reproduced the problem just as in my live app.

I can't see a way of attaching it here so I'll email it if you let me know where to send.
                  
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13498483#comment-13498483 ] 

John Coleman commented on TAP5-2027:
------------------------------------

Well, the fix defo works.

The documentation specifically uses this example that was my model:

public class UserDAOImpl implements UserDAO {
   @Inject
   @PersistenceContext(unitName = "DemoUnit")
   private EntityManager entityManager;

   ...
}

so a disappointment.

If I can make time to generate a git patch I'll have a go. In the meantime here is the workaround code:

[This code is freely released without copyright or any other restrictions or conditions. Use at your own risk.]

package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A fixed version to use PlasticProxyFactory and not cache the EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator objectLocator)
    {
            final PlasticProxyFactory proxyFactory = objectLocator.getService("PlasticProxyFactory",
            		PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = proxyFactory.createProxy(EntityManager.class, new ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = objectLocator
                            .getService(EntityManagerManager.class);

                    return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

}

                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497896#comment-13497896 ] 

John Coleman commented on TAP5-2027:
------------------------------------

Maybe there is some code convention I have missed. Can you share your test code? I will see if I can produce a simple test case to reproduce this issue for sharing.

                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "Lenny Primak (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497167#comment-13497167 ] 

Lenny Primak commented on TAP5-2027:
------------------------------------

I just tested that second scenario, without DAOs, but a different PU on each page directly injected into the page,
and that works fine.

The code you provided still should not make any difference.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Coleman updated TAP5-2027:
-------------------------------

    Description: 
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.





  was:
When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.

By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.

The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.

The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.


package org.apache.tapestry5.internal.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;

/**
 * A patched version to use PlasticProxyFactory and not cache the 
EntityManager as a class member.
 * @author John Coleman
 */
public class EntityManagerObjectProvider implements ObjectProvider
{

    /**
     * {@inheritDoc}
     */
    public <T> T provide(final Class<T> objectType, final AnnotationProvider 
annotationProvider,
            final ObjectLocator locator)
    {
        if (objectType.equals(EntityManager.class))
            return objectType.cast(getOrCreateProxy(annotationProvider, 
locator));

        return null;
    }

    private synchronized EntityManager getOrCreateProxy(
            final AnnotationProvider annotationProvider, final ObjectLocator 
objectLocator)
    {
            final PlasticProxyFactory proxyFactory = 
objectLocator.getService("PlasticProxyFactory",
              PlasticProxyFactory.class);

             final PersistenceContext annotation = annotationProvider
                            .getAnnotation(PersistenceContext.class);

            EntityManager proxy = 
proxyFactory.createProxy(EntityManager.class, new 
ObjectCreator<EntityManager>()
            {
                public EntityManager createObject()
                {
                    final EntityManagerManager entityManagerManager = 
objectLocator
                            .getService(EntityManagerManager.class);

                    return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");

        return proxy;
    }

} 




    
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>         Attachments: TapestryJPATest.zip
>
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496957#comment-13496957 ] 

John Coleman commented on TAP5-2027:
------------------------------------

The case involves injection of EntityManager in separate DAO classes that are used by separate pages, not direct injection of different EntityManager into a single page. It seems the injection handling is different.

i.e.

Page1 -> DAOX -> @PersistenceContext(unitName=A) EntityManager
Page2 -> DAOY -> @PersistenceContext(unitName=B) EntityManager

For DAOY the reference to the EntityManager of unitName=A is provided when the DAO class implementation is built.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (TAP5-2027) EntityManagerObjectProvider always provides the initial EntityManger proxy created

Posted by "John Coleman (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/TAP5-2027?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497974#comment-13497974 ] 

John Coleman commented on TAP5-2027:
------------------------------------

I built a simple test application and reproduced the problem just as in my live app.

I can't see a way of attaching it here so I'll email it if you let me know where to send.
                
> EntityManagerObjectProvider always provides the initial EntityManger proxy created
> ----------------------------------------------------------------------------------
>
>                 Key: TAP5-2027
>                 URL: https://issues.apache.org/jira/browse/TAP5-2027
>             Project: Tapestry 5
>          Issue Type: Bug
>          Components: tapestry-jpa
>    Affects Versions: 5.3.5, 5.3.6
>            Reporter: John Coleman
>              Labels: patch
>
> When persistence.xml defines multiple persistence units, classes injecting EntityManager with @PersistenceContext(unitName=value  crash because the entities associated with the PU in configuration are not recognised at runtime.
> By placing trace in the code I established that the first EntityManager injected gets injected to all my other service classes even though I use different unitName= annotations.
> The EntityManagerObjectProvider class contains a class variable proxy and works like a singleton always injecting the first EntityManager proxy class created for any later EntityManager injections.
> The following code fixes the issue and is provided as-is, free and without copyright or warranty. This is more like a refactor because I have also replaced some depricated code. As a patch it also works just to remove the proxy class member variable and the if (proxy == null) condition.
> package org.apache.tapestry5.internal.jpa;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
> import org.apache.tapestry5.ioc.AnnotationProvider;
> import org.apache.tapestry5.ioc.ObjectCreator;
> import org.apache.tapestry5.ioc.ObjectLocator;
> import org.apache.tapestry5.ioc.ObjectProvider;
> import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
> import org.apache.tapestry5.jpa.EntityManagerManager;
> /**
>  * A patched version to use PlasticProxyFactory and not cache the 
> EntityManager as a class member.
>  * @author John Coleman
>  */
> public class EntityManagerObjectProvider implements ObjectProvider
> {
>     /**
>      * {@inheritDoc}
>      */
>     public <T> T provide(final Class<T> objectType, final AnnotationProvider 
> annotationProvider,
>             final ObjectLocator locator)
>     {
>         if (objectType.equals(EntityManager.class))
>             return objectType.cast(getOrCreateProxy(annotationProvider, 
> locator));
>         return null;
>     }
>     private synchronized EntityManager getOrCreateProxy(
>             final AnnotationProvider annotationProvider, final ObjectLocator 
> objectLocator)
>     {
>             final PlasticProxyFactory proxyFactory = 
> objectLocator.getService("PlasticProxyFactory",
>               PlasticProxyFactory.class);
>              final PersistenceContext annotation = annotationProvider
>                             .getAnnotation(PersistenceContext.class);
>             EntityManager proxy = 
> proxyFactory.createProxy(EntityManager.class, new 
> ObjectCreator<EntityManager>()
>             {
>                 public EntityManager createObject()
>                 {
>                     final EntityManagerManager entityManagerManager = 
> objectLocator
>                             .getService(EntityManagerManager.class);
>                     return 
> JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
>                 }
>             }, "<EntityManagerProxy>");
>         return proxy;
>     }
> } 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira