Weblog

Out of Container EJB3 with OC4J

One big improvement in EJB3 is that you can test your entity beans outside the EJB container. You can find a description how to do this with oc4j (10.1.3) here: Using EJB 3.0 outside the container and here: How-To Build out-of-container example using EJB 3.0.

One thing these articles fail to document is how to use EJB3 out of container when you also have a container configuration file ejb3-toplink-sessions.xml. For out of container testing you don’t need this file, it is used by oc4j when deploying the application. I made a project which uses the out of container method for unit tests, but when i added the ejb3-toplink-sessions.xml file, the unit tests stopped working. In my case i got the following error:

Omschrijving uitzondering: Er is een fout opgetreden bij het opzoeken van de resource van de externe transactie onder de JNDI-naam [java:comp/pm/TransactionManager].
Interne uitzondering: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at oracle.toplink.exceptions.TransactionException.errorObtainingTransactionManager(TransactionException.java:112)
	at oracle.toplink.transaction.JTATransactionController.<init>(JTATransactionController.java:53)
...
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)

You can avoid these errors by setting the property EntityManagerFactoryProvider.EJB30_SESSIONS_XML_NAME_PROPERTY to some non existing file name like not-there.xml when you create an EntityManager. So the resulting code will be something like this:

public static EntityManager getEntityManager() {
    Map conf = new HashMap();
    EntityManagerFactory emf;
    conf.put("toplink.session.name", "hotel");
    conf.put("java.persistence.setup.config",
             "nl.hva.hotel.model.tests.TestEntities");

    conf.put("javax.persistence.provider",
             "oracle.toplink.ejb.cmp3.EntityManagerFactoryProvider");
    conf.put("jdbc.driver", "com.mysql.jdbc.Driver");
    conf.put("jdbc.connection.string", "jdbc:mysql://localhost/hotel");
    conf.put("jdbc.user", "hotel_owner");
    conf.put("jdbc.password", "hotel_owner");
    conf.put("toplink.platform.class.name",
             "oracle.toplink.platform.database.MySQL4Platform");

    conf.put(EntityManagerFactoryProvider.EJB30_SESSIONS_XML_NAME_PROPERTY,"not-there.xml");

    emf = Persistence.createEntityManagerFactory(conf);
    return emf.createEntityManager();
}
Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • LinkedIn
  • SphereIt
  • StumbleUpon
  • Technorati

One Response to “Out of Container EJB3 with OC4J”

  1. Rohit Thukral Says:

    Hi Dear,
    My Self Rohit Thukral, Software Engineer (Java/J2EE).i have a small problem. i want to create enitity manager for mysql.so will you plz tell me the required properties and its value which i need to set before creating entity manager object for a specific database.
    Thanks & Regards
    Rohit Thukral

Leave a Reply

Technology