EclipseLink JPA Deployed on JBoss using Eclipse WTP

If you want to get a small web application running quickly on JBoss use the services provided by the Web Tools Project plugin in the Eclipse IDE to take care of deployment details for you.

This basic example details how to use Eclipse to run/debug a minimum J2EE web application servlet using EclipseLink JPA as the persistence provider. The goal of this example is to detail the minimum steps needed to run EclipseLink inside JBoss using the Eclipse IDE - at this point no presentation/controller layer such as JSF, Spring or Struts will be used beyond a basic HttpServlet so we can concentrate on the the integration layer JPA setup.

The DALI project was used to generate Entities from a schema with sequences already populated.

Try changing the following jboss:service=Naming port to any available port, keep the RmiPort at 1098. Use [netstat -a] to check free ports - in my case 1099 is taken by the 1521 orcl listener.

TCP yourpc:1099 yourpc....com:1521 ESTABLISHED

<JBOSS_HOME>\server\default\conf\jboss-service.xml|jboss-minimal.xml

<mbeancode="org.jboss.naming.NamingService"name="jboss:service=Naming"xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">
...
from
<attributename="Port">1199</attribute>
to
<attributename="Port">1099</attribute>

Eclipse WTP Server configuration

Open the server configuration on the [servers] view and change [Server Properties/JNDI Port] to 1199

<app>/JNDI.properties

You will also need to sync this port change in your jndi.properties file (mine is off my web application under src/ just above META-INF.

After EAR project creation - reference eclipselink.core and eclipselink.jpa or include a reference to eclipselink.jar in your WAR project.

If you don't reference the eclipselink.* projects then include a classpath reference to persistence.jar and an Oracle JDBC driver jar for your DB - You will need to put this JDBC driver jar in your JBoss /server/default/lib directory as well.

@EJB Injection is not available to Servlets - use a JNDI lookup

In JBoss the JEE @EJB annotation has no effect when trying to inject you session bean that holds the entitymanager.

@EJB(beanName="ApplicationService")// No effect in JBoss

Use the following JNDI name and lookup in your Servlet where UnifiedJBossEAR is the name of your EAR project and ApplicationService is the name of your @Local @Stateless session bean.

Session Customizer

The client will require an implementation of SessionCustomizer that will set the lookupType on the JNDI connector to STRING_LOOKUP instead of Composite.

Refer to the following post for details on this issue with Catalina based containers.

Refer to the following 260383 enhancement request that will make this SessionCustomizer redundant by moving this functionality as standard procedure on the server platform.

importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.sql.DataSource;importorg.eclipse.persistence.config.SessionCustomizer;importorg.eclipse.persistence.sessions.server.ServerSession;importorg.eclipse.persistence.sessions.JNDIConnector;importorg.eclipse.persistence.sessions.Session;/**
* See
* http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG)
* Use for clients that would like to use a JTA SE pu instead of a RESOURCE_LOCAL SE pu.
*/publicclass JPAEclipseLinkSessionCustomizer implements SessionCustomizer {publicstaticfinalString JNDI_DATASOURCE_NAME ="java:/XAOracleDS";/**
* Get a dataSource connection and set it on the session with lookupType=STRING_LOOKUP
*/publicvoid customize(Session session)throwsException{
JNDIConnector connector =null;// Initialize session customizer
DataSource dataSource =null;try{Context context =newInitialContext();if(null== context){thrownewException("Context is null");}// Create a new org.jboss.resource.adapter.jdbc.WrapperDataSource//connector = new JNDIConnector(context, JNDI_DATASOURCE_NAME);
connector =(JNDIConnector)session.getLogin().getConnector();// possible CCE// Lookup this new dataSource
dataSource =(DataSource) context.lookup(JNDI_DATASOURCE_NAME);}catch(Exception e){
e.printStackTrace();}
connector.setDataSource(dataSource);// Change from COMPOSITE_NAME_LOOKUP to STRING_LOOKUP// Note: if both jta and non-jta elements exist this will only change the first one - and may still result in the COMPOSITE_NAME_LOOKUP being set// Make sure only jta-data-source is in persistence.xml with no non-jta-data-source property set
connector.setLookupType(JNDIConnector.STRING_LOOKUP);// if you are specifying both JTA and non-JTA in your persistence.xml then set both connectors to be safe
JNDIConnector writeConnector =(JNDIConnector) session.getLogin().getConnector();
writeConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
JNDIConnector readConnector =(JNDIConnector)((DatabaseLogin)((ServerSession)session).getReadConnectionPool().getLogin()).getConnector();
readConnector.setLookupType(JNDIConnector.STRING_LOOKUP);// Set the new connection on the session
session.getLogin().setConnector(connector);}}

Persistence.xml

Put persistence.xml in yourProjectEJB/ejbModule/META-INF with a reference to your SessionCustomizer or...

If using a separate J2SE project for your business logic, put persistence.xml in yourProjectSE/src/META-INF/ and export the SE jar to yourProjectWeb/WebContent/WEB-INF/lib/yourProjectSE.jar

Note: the property server.platform.class.name has been deprecated - the target-server property should be used for EclipseLink release 1.0+

Start Server

Steps: Select the EAR and [Run on Server].

The first "Run on Server" may not start the server, in this case you "Start Server" and then "Run on Server".

Publish EAR

Publishing to JBoss is very simple, once the server is setup you can just drop/remove an EAR in the deploy directory to deploy/undeploy an application. Or use Eclipse to debug via the 5005 port if the server is managed through WTP.

Eclipse WTP will take care of copying the EAR file to the live deploy directory when you either [re-publish] or modify any files while the server is running.

You may also use any combination of running the JBoss server yourself in run or debug mode and using eclipse to publish EAR changes.