tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thiago Veronezi <thi...@veronezi.org>
Subject Re: Problem with testing bean with persistence context type EXTENDED
Date Fri, 13 Aug 2010 10:41:48 GMT
The code that throws that exception is...
***************************************************************************
 JtaEntityManagerRegistry.java

       private void addEntityManagers(InstanceId instanceId,
Map<EntityManagerFactory, EntityManager> entityManagers) throws
EntityManagerAlreadyRegisteredException {
            if (instanceId == null) {
                throw new NullPointerException("instanceId is null");
            }
            if (entityManagers == null) {
                throw new NullPointerException("entityManagers is null");
            }

            if (isTransactionActive()) {
                for (Map.Entry<EntityManagerFactory, EntityManager> entry :
entityManagers.entrySet()) {
                    EntityManagerFactory entityManagerFactory =
entry.getKey();
                    EntityManager entityManager = entry.getValue();
                    EntityManagerTxKey txKey = new
EntityManagerTxKey(entityManagerFactory);
                    EntityManager oldEntityManager = (EntityManager)
transactionRegistry.getResource(txKey);
                    if (entityManager == oldEntityManager) {
                        break;
                    }
                    if (oldEntityManager != null) {
                        throw new
EntityManagerAlreadyRegisteredException("Another entity manager is already
registered for this persistence unit");
                    }

                    entityManager.joinTransaction();
                    transactionRegistry.putResource(txKey, entityManager);
                }
            }
            entityManagersByDeploymentId.put(instanceId, entityManagers);
        }
***************************************************************************
The problem is that to join an active transaction, the entityManager should
be unique. My guess is that when you annotate...
@PersistenceContext(unitName = "unitNamePU", name= "entityManager",
type = PersistenceContextType.EXTENDED)...
to the class, you're actually binding annother entityManager to it (please
correct me if thats not true). This new entityManager will then conflict
with the entityManager from your DAO object.

[]s,
Thiago.


On Fri, Aug 13, 2010 at 5:05 AM, Alexander Gavrilov <ljevik@gmail.com>wrote:

> Test was fixed by taking out BaseUtils.getDialog() method out of
> transaction
> block:
> transaction.begin()
> ...
> transaction.commit()
> can someone, please explain such behavior?
>
> Alexander.
>
> On 12 August 2010 22:10, Alexander Gavrilov <ljevik@gmail.com> wrote:
>
> > Hi, Thiago,
> >
> > Neither, BaseDialogImpl nor SpecificDialog does not have entityManagers.
> >
> > Alexander.
> >
> >
> > On 12 August 2010 17:24, Thiago Veronezi <thiago@veronezi.org> wrote:
> >
> >> Hi Alexander,
> >> Just to give me some directions: Does your BaseDialogImpl class have an
> >> entityManager too?
> >>
> >> []s,
> >> Thiago.
> >>
> >>
> >> On Thu, Aug 12, 2010 at 5:35 AM, Alexander Gavrilov <ljevik@gmail.com
> >> >wrote:
> >>
> >> > Hello!
> >> >
> >> > I have a problem, I need to test a STATEFUL bean, which has EXTENDED
> >> > persistence context type. This bean is created from another bean,
> which
> >> is
> >> > SINGELTON and has a
> >> @TransactionManagement(TransactionManagementType.BEAN).
> >> > Shortly the code is following:
> >> >
> >> > @Singleton(name = "controller")
> >> > @Service(name =  "controller")
> >> > @Local(ControllerLocal.class)
> >> > @Remote(ControllerRemote.class)
> >> > @LocalBinding(jndiBinding =  "controller/local")
> >> > @RemoteBinding(jndiBinding =  "controller/remote")
> >> > @TransactionManagement(TransactionManagementType.BEAN)
> >> > public class ControllerEjb extends AbstractController implements
> >> > ControllerLocal, ControllerRemote {
> >> >
> >> >    @EJB
> >> >    private ControllerDaoLocal dao;
> >> >
> >> >    @Resource
> >> >    private SessionContext ejbContext;
> >> >
> >> > public String doWork(String arg1, Long arg2) {
> >> >        UserTransaction transaction = ejbContext.getUserTransaction();
> >> >        boolean started = false;
> >> >        try {
> >> >            transaction.begin();
> >> >            SomeEntity entity = dao.getEntity(arg1, true);
> >> >
> >> >             .....
> >> >
> >> >            // here test fails
> >> >            IDialog dialog = BaseUtils.getNewDialog("dialog_name");
> >> >
> >> >            ....
> >> >          }
> >> >  }
> >> >
> >> > }
> >> >
> >> > public class BaseUtils {
> >> >
> >> > private static final InitialContext context;
> >> >
> >> > public static IDialog getNewDialog(String name) {
> >> >        try {
> >> >            return (IDialog) context.lookup(name);
> >> >        } catch (NamingException e) {
> >> >            logger.error(" no ejb with name : " + name + " is
> defined");
> >> >            throw new IllegalArgumentException("ejb not found",e);
> >> >        }
> >> >    }
> >> >
> >> > }
> >> >
> >> > The dialog, which should be returned by getDialog method:
> >> >
> >> > @Stateful(name = "dialog_name")
> >> > @Local(IDialog.class)
> >> > @LocalBinding(jndiBinding = "dialog_name/local")
> >> > @PersistenceContext(unitName = "unitNamePU", name= "entityManager",
> type
> >> =
> >> > PersistenceContextType.EXTENDED)
> >> > public class SpecificDialog extends BaseDialogImpl {
> >> >
> >> > // a lot of business logic here
> >> >
> >> > }
> >> >
> >> >
> >> > And the test, which is trying to test all this stuff looks like that:
> >> >
> >> > @RunWith(PowerMockRunner.class)
> >> > @PrepareForTest({MockClass1.class, MockClass2.class})
> >> > public class SpecificDialogTest extends BasicBeanTest {
> >> >
> >> >    @Test
> >> >    public void testSpecificDialog() throws Exception {
> >> >
> >> >     // mock and expect something here
> >> >
> >> >    ControllerLocal controllerLocal = (ControllerLocal)
> >> > context.lookup("controller/local");
> >> >    controllerLocal.doWork(arg1, arg2);
> >> >
> >> >
> >> >   // assertions & verifications
> >> >
> >> > }
> >> >
> >> > Test fails, when it is trying to get dialog by its JNDI name with
> >> following
> >> > exception:
> >> >
> >> > javax.ejb.EJBException:
> >> >
> org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException:
> >> > Another entity manager is already registered for this persistence unit
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.stateful.StatefulContainer.createEJBObject(StatefulContainer.java:365)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.stateful.StatefulContainer.invoke(StatefulContainer.java:338)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.EjbHomeProxyHandler.create(EjbHomeProxyHandler.java:284)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.EjbHomeProxyHandler._invoke(EjbHomeProxyHandler.java:169)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:282)
> >> >        at $Proxy63.create(Unknown Source)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.naming.BusinessLocalReference.getObject(BusinessLocalReference.java:33)
> >> >        at
> >> >
> >>
> org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext.java:171)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.naming.ContextWrapper.lookup(ContextWrapper.java:115)
> >> >        at javax.naming.InitialContext.lookup(InitialContext.java:392)
> >> >        at BaseUtils.getNewDialog(BaseUtils.java:137)
> >> >        at ControllerEjb.doWork(ControllerEjb.java:362)
> >> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >> >        at java.lang.reflect.Method.invoke(Method.java:597)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:175)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:157)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:174)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:93)
> >> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >> >        at java.lang.reflect.Method.invoke(Method.java:597)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:175)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:157)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:122)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.singleton.SingletonContainer._invoke(SingletonContainer.java:264)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.singleton.SingletonContainer.invoke(SingletonContainer.java:227)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:217)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:77)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:282)
> >> >        at $Proxy77.startEnrollment(Unknown Source)
> >> >        at
> >> > SpecificDialogTest.testSpecificDialog(SpecificDialogTest.java:119)
> >> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >> >        at java.lang.reflect.Method.invoke(Method.java:597)
> >> >        at
> >> org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:322)
> >> >        at
> >> > org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
> >> >        at
> >> >
> >> >
> >>
> org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:309)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:297)
> >> >        at
> >> > org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
> >> >        at
> >> org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
> >> >        at
> >> >
> >>
> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
> >> >        at
> >> >
> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:55)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:165)
> >> >        at org.apache.maven.surefire.Surefire.run(Surefire.java:107)
> >> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >> >        at java.lang.reflect.Method.invoke(Method.java:597)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:289)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:993)
> >> > Caused by:
> >> >
> org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException:
> >> > Another entity manager is already registered for this persistence unit
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.persistence.JtaEntityManagerRegistry$ExtendedRegistry.addEntityManagers(JtaEntityManagerRegistry.java:203)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.persistence.JtaEntityManagerRegistry$ExtendedRegistry.access$100(JtaEntityManagerRegistry.java:181)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.persistence.JtaEntityManagerRegistry.addEntityManagers(JtaEntityManagerRegistry.java:141)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.stateful.StatefulContainer.createEJBObject(StatefulContainer.java:363)
> >> >        ... 65 more
> >> >        at
> >> > org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
> >> >        at
> >> org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
> >> >        at
> >> >
> >>
> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
> >> >        at
> >> >
> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
> >> >        at
> >> >
> >> >
> >>
> org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:55)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:165)
> >> >        at org.apache.maven.surefire.Surefire.run(Surefire.java:107)
> >> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> >> >        at
> >> >
> >> >
> >>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> >> >        at java.lang.reflect.Method.invoke(Method.java:597)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:289)
> >> >        at
> >> >
> >> >
> >>
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:993)
> >> > Caused by:
> >> >
> org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException:
> >> > Another entity manager is already registered for this persistence unit
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.persistence.JtaEntityManagerRegistry$ExtendedRegistry.addEntityManagers(JtaEntityManagerRegistry.java:203)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.persistence.JtaEntityManagerRegistry$ExtendedRegistry.access$100(JtaEntityManagerRegistry.java:181)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.persistence.JtaEntityManagerRegistry.addEntityManagers(JtaEntityManagerRegistry.java:141)
> >> >        at
> >> >
> >> >
> >>
> org.apache.openejb.core.stateful.StatefulContainer.createEJBObject(StatefulContainer.java:363)
> >> >        ... 88 more
> >> >
> >> >
> >> > I was also trying to do as was shown in tutorial, using
> TransactionBean:
> >> > http://openejb.apache.org/3.0/unit-testing-transactions.html, but
> >> without
> >> > luck.
> >> >
> >> > Any help would be appreciated.
> >> >
> >> > Best.
> >> > Alexander.
> >> >
> >>
> >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message