views:

152

answers:

3

Hi, I'm using 2 PU in stateless EJB and each of them is invoked on one method:

@PersistenceContext(unitName="PU")
private EntityManager em;
@PersistenceContext(unitName="PU2")
private EntityManager em2;

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW )
public void getCandidates(final Integer eventId) throws ControllerException {
    ElectionEvent electionEvent = em.find(ElectionEvent.class, eventId);
    ...
    Person person = getPerson(candidate.getLogin());
    ...
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW )
private Person getPerson(String login) throws ControllerException {
    Person person = em2.find(Person.class, login);
    return person;
}

Those methods are annotated with REQUIRES_NEW transcaction to avoid this exception. When I was calling these method from javaFX applet, all worked as expected. Now I'm trying to call them from JAX-RS webservice (I don't see any logical difference as in both cases ejb was looked up in initial context) and I keep getting this exception. When I set up XADatasource in glassfish 2.1 connection pools, I got nullpointer exception on em2.

Any ideas what to try next?

Regards

+1  A: 

I'm using 2 PU in stateless EJB and each of them is invoked on one method

Indeed. But you're calling the second method from the first one so you're doing a distributed transaction and you need to use XA for this (at least for one of the resource since GlassFish supports the last agent optimization allowing to involve one non-XA resource). In other words, setting one of your datasource as a XADataSource is the way to go.

If you get an error when doing this, please add details about what you did exactly and the stacktrace.

Pascal Thivent
Thank you, I will post it asap but in the meantinme, is there a way to specify XADataSource in persistence.xml? I couln't find it anywhere and every time i deploy via netbeans the glassfish setting in connection pool is reverted to plain DataSource.
Zeratul
A: 

Okey, so I deploy modules, go to gf2.1 admin console, resources/jdbc/connectionPools/myCP and i set resource type to XADataSource (tried one or both CP). Then I don't get this exception. But the entity manager2 is not injected. AS you can see, it's on 3rd row from the bottom in my previous post. Quite logical observation is that with omitting transactions everything works. Is there need for configuring something else (it's derby database)? I read quite a stuff but everything points out that this should work.

Regards,

Caused by: java.lang.NullPointerException
    at oracle.toplink.essentials.internal.ejb.cmp3.base.CMP3Policy.initializePrimaryKeyFields(CMP3Policy.java:315)
    at oracle.toplink.essentials.internal.ejb.cmp3.base.CMP3Policy.getPKClass(CMP3Policy.java:133)
    at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.findInternal(EntityManagerImpl.java:327)
    at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.find(EntityManagerImpl.java:148)
    at com.sun.enterprise.util.EntityManagerWrapper.find(EntityManagerWrapper.java:291)
    at ejb.VotingSessionBean.getPerson(VotingSessionBean.java:69)
    at ejb.VotingSessionBean.getCandidates(VotingSessionBean.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)
    at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)
    at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2929)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4020)
    at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
    ... 65 more
javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.RemoteException: null; nested exception is: 
    java.lang.NullPointerException
    at ejb._VotingSessionRemote_Wrapper.getCandidates(ejb/_VotingSessionRemote_Wrapper.java)
    at ws.VotingServiceResource.getCandidates(VotingServiceResource.java:53)
    at ws.VotingServiceResource.get(VotingServiceResource.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
Zeratul
A: 

Ok,

it's solved now. I'll share just in case somebody got tackled by similar thing. Whole problem was with netbeans deploying. They overwrite the settings in glassfish connection pool and when you set them proper at runtime, you got npe's or missing password silly stuff. The place to edit this is sun-resources.xml. XML element has attributes datasource-classname and rs-type. What needs to be done in case of Derby database is:

<jdbc-connection-pool ... datasource-classname="org.apache.derby.jdbc.ClientXADataSource" res-type="javax.sql.XADataSource">
 ...
</jdbc-connection-pool>

Works like a charm now.

Zeratul