0

I have been looking through a lot of similar questions which did not reflect my exact problem. If i overlooked that someone already had this problem solved, please let me know.

I am currently migrating an old EJB 2.1 application on JBoss 3.x to EJB 3.x on JBoss 7.x. Due to the changes in communication between the application client and the server, i created a small testclient to check for pitfalls.

One of these is that the entitymanager in my stateless session beans is not injected, leaving the EntityManager NULL. I tried working around this using the EntityManagerFactory, but neither is injected. The datasource referenced is available and the connection has been tested against the database.

I hope that some of you have come across this problem, can think of where i can look next or what i might do to track this issue down. Enclosed are the persistence.xml, RemoteInterface and Bean code from server side as well as my test client code.

Please let me know if i can provide more information. (The classes have mostly retained their name from the EJB 2 application in case you are wondering).

Stateless Session Bean (GUITabUsersDao):

@Stateless
@Remote(GUITabUsersDaoRemote.class)
public class GUITabUsersDao implements GUITabUsersDaoRemote {
    Logger logger = Logger.getLogger(GUITabUsersDao.class.getName());

    @PersistenceContext(name = "OracleGUIDS", type =   PersistenceContextType.TRANSACTION)
    EntityManager entityManager;

    @Override
    public List<GUITabUsers> findAll() throws FinderException {
         List<GUITabUsers> resultList = null;
         TypedQuery<GUITabUsers> namedQuery;
         if (entityManager == null) {
             logger.severe("**** CKU: This is not going to end well. entitymanager is null. :( ****");
         } else {
            namedQuery = entityManager.createNamedQuery(GUITabUsers.FIND_ALL, GUITabUsers.class);
            resultList = namedQuery.getResultList();
        }
        return resultList;
    }
}

The Remote Interface

public interface GUITabUsersDaoRemote {
    List<GUITabUsers> findAll() throws FinderException;
}

The persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="OracleGUIDS" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/OracleGUIDS</jta-data-source>
        <class>de.printcom.loginmodule.GUITabUsers</class>
        <properties>
            <!-- Properties for Hibernate  -->
            <property name="hibernate.hbm2ddl.auto" value="verify"/>
            <property name="hibernate.show_sql" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

The client code

package de.my.test;

public class TestClient {
    public static final String URL = "localhost";
    public static final String PORT = "4447";

    private static final Logger LOGGER = Logger.getLogger(TestClient.class.getName());

    public static void main(String[] args) {
        IDPLookupRemote bean;
        LAEC_MachineControlSES bean2;
        GUITabUsersDaoRemote guiTabUsersDao;
        try {
            EJBHomeFactory factory = EJBHomeFactory.getInstance(URL, PORT);
            guiTabUsersDao = (GUITabUsersDaoRemote) lookup("GUITabUsersDao", GUITabUsersDaoRemote.class);
            LOGGER.info("Bean '" + guiTabUsersDao.toString() + "' received.");
            List<GUITabUsers> col = null;
            try {
                col = guiTabUsersDao.findAll();
            } catch (FinderException | NullPointerException e) {
                e.printStackTrace();
            }
            if (null != col) {
                LOGGER.info(col.get(0).toString());
            } else {
                LOGGER.info("Col is empty!");
            }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Object lookup(String jndiName, Class<?> remoteInterfaceClass) throws NamingException {
        Object object = new Object();
        try {
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
            properties.put("jboss.naming.client.ejb.context", true);
            properties.put(Context.PROVIDER_URL, "remote://" + URL + ":" + PORT);
            Context context = new InitialContext(properties);
            final String appName = "appName";
            final String moduleName = "moduleName";
            final String distinctName = "";
            final String beanName = jndiName;
            final String viewClassName = remoteInterfaceClass.getName();
            LOGGER.info("Looking EJB via JNDI ");
            String jndiLookupName = appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
            LOGGER.info(jndiLookupName);
            object = context.lookup(jndiLookupName);
            LOGGER.info("Calling 'context.lookup(" + jndiLookupName + ")'");
            // object = context.lookup(jndiName);
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return object;
    }
}
Christian Kullmann
  • 558
  • 1
  • 7
  • 21
  • I hate to ask the obvious question, but are you sure the persistence context was successfully created on startup? Are there any errors in the log? – jgitter May 06 '15 at 19:53
  • I am as sure as i can be with no errors in the logfile showing. Additionally, it states that all my datasources have been deployed. Also i set the org.jboss.as.jpa to TRACE log level hoping to catch anything awry. Could it be that it doesn't like the -ds.xml files? – Christian Kullmann May 07 '15 at 07:18
  • in addition to cranking log levels for JPA, try setting log levels to trace for the implementation. I assume you're using hibernate? – jgitter May 07 '15 at 17:17
  • I tried creating the EntityManagerFactory manually using Persistence.createEntityManagerFactory which went down the drain with a massive exception stating that there was no PersistenceContext OracleGUIDS (as you already assumed). Yes, i am using hibernate or at least think i am since i haven't configured otherwise and hibernate is supposed to be default with the JBoss. I am using the JBoss EAP 6.4 and stumbled across the usage of standalone.xml and standalone-full.xml. Might that just be the cause for all my troubles? – Christian Kullmann May 07 '15 at 18:38
  • I played around at home with it and it seems i dug my own hole. This will be a walk of shame for me i am sure. The summary will be added to the question. Feel free to downvote. :( – Christian Kullmann May 07 '15 at 19:35
  • Please post the answer as a real answer, not as part of question. – BalusC May 07 '15 at 19:55
  • I agree with BalusC on that one. I'll vote for your answer. Nice job tracking that down. – jgitter May 07 '15 at 20:03

1 Answers1

3

As suggested by BalusC and jgitter, here is my previous edit as an answer:

If you come across a similar problem feel free to bathe in my stupidity for the problem mentioned below.

In the application i needed to migrate from JBoss 3.x to 7.x a number of projects were thrown together. One of these projects had two persistence units declared in the persistence.xml while the Stateless Session Beans all were declared similar to this:

@Stateless
@Remote(SomeStatelessRemote.class)
public class SomeStatelessBean implements SomeStatelessRemote {

    @PersistenceContext(name = "OracleGUIDS")
    EntityManager entitymanager;
    ...
}

Now if you deploy this, you will get the error message

JBAS011470: Persistence unitName was not specified

In some forums someone suggested to disable a specific subsystem in the standalone

<subsystem xmlns="urn:jboss:domain:jpa:1.1">
   <jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
</subsystem>

as you can see this will disable ANY possibility of using JPA at all.

Now i enabled the subsystem and used the correct syntax to inject the entitymanager:

@Stateless
@Remote(SomeStatelessRemote.class)
public class SomeStatelessBean implements SomeStatelessRemote {

    @PersistenceContext(unitName= "OracleGUIDS")
    EntityManager entitymanager;
    ...
}

and, oh wonder, i am able to access the entitymanager and have at it. Thanks to everyone working their brains to help me. Hopefully this entry will help you AVOID this error.

Christian Kullmann
  • 558
  • 1
  • 7
  • 21