Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/JBoss Web Tutorial"

m (EclipseLink JPA Deployed on JBoss 6.0.0 M1 using Eclipse 3.5 EE edition)
m (EclipseLink JPA Deployed on JBoss 6.0.0 M1 using Eclipse 3.5 EE edition)
Line 1: Line 1:
=EclipseLink JPA Deployed on JBoss 6.0.0 M1 using Eclipse 3.5 EE edition=
=EclipseLink JPA Deployed on JBoss 6.0.0 M1 using Eclipse 3.5 EE edition=
'''<font color="red">20100214: Update for results of EclipseLink 2.0 on JBoss 6.0.0M1 in progress</font>'''
'''<font color="red">20100217: Update for results of EclipseLink 2.0 on JBoss 6.0.0M1 in progress</font>'''
If you want to get a small web application running quickly on JBoss 6 use the services provided by the '''Web Tools Project''' project as part of the '''EE''' edition of the '''Eclipse IDE''' to take care of deployment details for you.
If you want to get a small web application running quickly on JBoss 6 use the services provided by the '''Web Tools Project''' project as part of the '''EE''' edition of the '''Eclipse IDE''' to take care of deployment details for you.

Revision as of 21:47, 17 February 2010

EclipseLink JPA Deployed on JBoss 6.0.0 M1 using Eclipse 3.5 EE edition

20100217: Update for results of EclipseLink 2.0 on JBoss 6.0.0M1 in progress

If you want to get a small web application running quickly on JBoss 6 use the services provided by the Web Tools Project project as part of the EE edition of 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.

Tutorial Source

The EAR, EJB and WEB Eclipse project source for this EclipseLink JBoss (JPA) tutorial is available online at the following locations. These 3 projects must be imported along with the jpa core eclipselink project or jar.

  • Checkout the following 3 projects together from $trunk/examples.
    • org.eclipse.persistence.example.jpa.server.jboss.EnterpriseEAR
    • org.eclipse.persistence.example.jpa.server.jboss.EnterpriseEJB
    • org.eclipse.persistence.example.jpa.server.jboss.EnterpriseWeb
  • EclipseLink source via SVN committer access


  • EclipseLink source via SVN anonymous access


The following link will put you at the root of the SVN tree using the online version of subclipse.




  • As an alternative if you do not wish to use SVN or the direct html links into SVN - I have attached a version of the EAR including source to enhancement bug # 250477 in versions for JBoss 5.1.0' (JPA 1.0) and JBoss 6.0.0M1 (JPA 2.0) - except that these may not be in sync with the latest SVN repository changes.

Development Environment

Hardware: Windows 7 64-bit, Core i7 920 Quad Core, 2.7Ghz, 12Gb Ram

Software: Eclipse IDE for Java EE 3.5 Galilleo (Nov 2009), Java JDK 1.6.0_17, JBoss 6.0.0M1 including the HSQL database.

This example will run fine with an older version of Eclipse to version 3.4 and any Database that EclipseLink supports.


  • Install Eclipse
    • I installed a clean version of Eclipse 3.5 Galileo SR1 EE edition with all of WTP 3.0
      • Note: The 64-bit edition currently has issues with EE server plugins - stick to the 32 bit version for now.
    • Eclipse arrives standard with JBoss 5.0 support - you may use the 5.0 version to deploy to 6.0 servers.
  • Install a Database
    • In this example I am using the HSQL database shipped with JBoss - therefore no database installation is required for this tutorial.

JBoss Server Configuration Changes

  • Launch the JBoss AS Administration console to view the configuration at
    • Username/password is admin/admin
  • You don't need to change anything if you use the default HSQL database - otherwise you must add your own datasource.

HSQL Hypersonic Default Datasource

  • The included DefaultDS datasource resolved to the URL jdbc:hsqldb:C:\opt\jboss600m1\server\default\data\hypersonic\localDB - replace "C:\opt\jboss600m1" with your $JBOSS_INSTALL_DIR.

Custom Datasource Configuration Details

JNDI JTA/non-JTA Server DataSource Setup
  • Copy $JBOSS_HOME\docs\examples\jca\oracle-*ds.xml to $JBOSS_HOME\server\default\deploy
  • Edit oracle-ds.xml
  • Enter the following into $JBOSS_HOME\server\default\deploy\oracle-ds.xml
  • Edit oracle-xa-ds.xml
  • Enter the following into $JBOSS_HOME\server\default\deploy\oracle-xa-ds.xml
    <xa-datasource-property name="URL">jdbc:oracle:thin:@localhost:1521:orcl</xa-datasource-property>
    <xa-datasource-property name="User">username</xa-datasource-property>
    <xa-datasource-property name="Password">password</xa-datasource-property>
  • Enter the following into $JBOSS_HOME\server\default\conf\login-config.xml
<application-policy name = "OracleDbRealm"> 
      <login-module code =  
  "org.jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required"> 
         <module-option name = "principal">sa</module-option> 
         <module-option name = "userName">sa</module-option> 
         <module-option name = "password"></module-option> 
         <module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=OracleDS</module-option> 
JNDI/JNP Port Conflict Workarounds

You may get the following port 1099 conflict if you are also running an Oracle Database.

14:56:41,597 ERROR [Naming] Could not start on port 1099
java.net.BindException: Address already in use: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method)

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


   <mbean code="org.jboss.naming.NamingService"
      <attribute name="Port">1199</attribute>
      <attribute name="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.


Downloading EclipseLink Libraries

Download EclipseLink using HTTP - recommended

Download EclipseLink using Maven

See the repository on http://www.eclipse.org/eclipselink/downloads/index.php

Download EclipseLink using SVN - developers only

EclipseLink JAR location

All Servers

The eclipselink.jar should be placed next to the ejb3-persistence.jar (JBoss 5.1.0) off of $JBOSS_HOME/common/lib

Specific Server

The eclipselink.jar should be placed off of $JBOSS_HOME/server/default/lib

JDBC JAR location

Copy your jdbc driver jar to $JBOSS_HOME/common/lib

Create server in Eclipse

Open the servers view New | Server | JBoss | JBoss v5.0 - ok for 6.0 use.

Create J2EE application

Check out the 3 example projects in the trunk '(TBD) or create your own J2EE Enterprise Application as below. File | new | project | J2EE | Enterprise Application Project Select server, use 5.0 Ear version

Create a new Web and an optional EJB project

Select generate deployment descriptor if you want to change the context-root

Reference EclipseLink and JPA - Path changes

  • Either link to the 2 SVN projects below or link to an external eclipselink.jar and javax-persistence.jar.
	<classpathentry combineaccessrules="false" kind="src" path="/eclipselink.core"/>
	<classpathentry combineaccessrules="false" kind="src" path="/eclipselink.jpa"/>
  • 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.

public class FrontController extends HttpServlet implements Servlet {
  public static final String APPLICATION_SERVICE_JNDI_NAME = "UnifiedJBossEAR/ApplicationService/local";
  @EJB(beanName="ApplicationService") // No effect in JBoss
  public ApplicationServiceLocal applicationService;
  public ApplicationServiceLocal getApplicationService() {
    if(null == applicationService) {
      try {
        InitialContext ctx = new InitialContext();
        return (ApplicationServiceLocal) ctx.lookup(APPLICATION_SERVICE_JNDI_NAME);            
      } catch (Exception e) {
        throw new RuntimeException("couldn't lookup DAO", e);
    } else {
      return applicationService;

Session Customizer

  • Note: The session customizer is only required for JBoss 4.2.2
  • 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.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.sessions.server.ServerSession;
import org.eclipse.persistence.sessions.JNDIConnector;
import org.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.
public class JPAEclipseLinkSessionCustomizer implements SessionCustomizer {
  public static final String JNDI_DATASOURCE_NAME = "java:/XAOracleDS";
   * Get a dataSource connection and set it on the session with lookupType=STRING_LOOKUP
  public void customize(Session session) throws Exception {
    JNDIConnector connector = null;
    // Initialize session customizer
    DataSource dataSource = null;
      try {
        Context context = new InitialContext();
        if (null == context) {
          throw new Exception("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) {
    // 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
    // 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();
    JNDIConnector readConnector = (JNDIConnector) ((DatabaseLogin)((ServerSession)session).getReadConnectionPool().getLogin()).getConnector();
    // Set the new connection on the session


  • 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
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="unified" transaction-type="JTA">
      <!-- non-jta-data-source>java:/OracleDS</non-jta-data-source-->
..other entities
        <!-- deprecated property name="eclipselink.server.platform.class.name" value="JBoss"/-->
        <property name="eclipselink.target-server" value="JBoss"/>
        <property name="eclipselink.weaving" value="false"/>
        <property name="eclipselink.session-name" value="default-session"/>            
        <property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer"/>
        <property name="eclipselink.logging.level" value="FINEST"/>
        <property name="eclipselink.cache.size.default" value="500"/>
        <property name="eclipselink.cache.type.default" value="Full"/>
        <!--property name="eclipselink.orm.throw.exceptions" value="false"/-->
  • 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.
  • Dynamic weaving is disabled for now because of [JIRA-572] - Use static weaving workaround before deploying if required.
  • Depending on your application you will see the following after running [start server]
    • In your console window you will see one process both for the server and the deploy target.
15:56:57,667 INFO  [Server] Starting JBoss (MX MicroKernel)...
15:56:57,668 INFO  [Server] Release ID: JBoss [Trinity] 4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)
15:57:08,630 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=OracleDS' to JNDI name 'java:OracleDS'
15:57:08,634 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=TXOracleDS' to JNDI name 'java:TXOracleDS'
15:57:08,690 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=XAOracleDS' to JNDI name 'java:XAOracleDS'
15:57:08,806 INFO  [EARDeployer] Init J2EE application: file:/C:/opt/jboss422/server/default/deploy/unifiedEAR.ear
15:57:08,835 INFO  [TomcatDeployer] deploy, ctxPath=/unified, warUrl=.../tmp/deploy/tmp35385unifiedEAR.ear-contents/unifiedWeb-exp.war/
15:57:08,926 INFO  [EARDeployer] Started J2EE application: file:/C:/opt/jboss422/server/default/deploy/unifiedEAR.ear
15:57:09,026 INFO  [Server] JBoss (MX MicroKernel) [4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)] Started in 11s:350ms

Perform a JPQL query

    emf = Persistence.createEntityManagerFactory(puName);
  • Console Output
15:57:23,046 INFO  [STDOUT] [EL Finest]: 2008.04.14 15:57:23.031--ServerSession(28649942)--Thread(Thread[http-,5,jboss])--Begin predeploying Persistence Unit unifiedJBoss; state Initial; factoryCount 0
15:57:23,046 INFO  [STDOUT] [EL Finest]: 2008.04.14 15:57:23.046--ServerSession(28649942)--Thread(Thread[http-,5,jboss])--property=eclipselink.weaving; value=false
15:57:23,244 INFO  [STDOUT] [EL Config]: 2008.04.14 15:57:23.243--ServerSession(28649942)--Thread(Thread[http-,5,jboss])--The primary key column name for the mapping element [private org.eclipse.persistence.example.unified.business.StatClass org.eclipse.persistence.example.unified.business.StatInheritance.toClass] is being defaulted to: ID.
15:57:23,246 INFO  [STDOUT] [EL Finest]: 2008.04.14 15:57:23.246--ServerSession(28649942)--Thread(Thread[http-,5,jboss])--End predeploying Persistence Unit unifiedJBoss; state Predeployed; factoryCount 0
  • Exercise your application in a browser by requesting a JPQL query
    • Launch<100
  • Console Output
15:57:41,175 INFO  [STDOUT] [EL Config]: 2008.04.14 15:57:41.175--ServerSession(28649942)--Connection(12593356)--Thread(Thread[http-,5,jboss])--Connected: jdbc:oracle:thin:@localhost:1521:orcl
	User: username
	Database: Oracle  Version: Oracle Database 11g Release - Production
	Driver: Oracle JDBC driver  Version:
15:57:41,391 INFO  [STDOUT] [EL Finest]: 2008.04.14 15:57:41.391--UnitOfWork(5316732)--Thread(Thread[http-,5,jboss])--Execute query ReadAllQuery(org.eclipse.persistence.example.unified.business.StatLabel)
15:57:41,393 INFO  [STDOUT] [EL Fine]  : 2008.04.14 15:57:41.393--ServerSession(28649942)--Connection(32240107)--Thread(Thread[http-,5,jboss])--SELECT ID, DATE_STAMP FROM STAT_LABEL
15:57:41,582 INFO  [STDOUT] [EL Finest]: 2008.04.14 15:57:41.581--UnitOfWork(5316732)--Thread(Thread[http-,5,jboss])--Register the existing object org.eclipse.persistence.example.unified.business.StatLabel@10ae2bf
  • Browser Output

Eclipselink example jpa jboss web jpql action cap.jpg


  • 20090329 - last edit for JBoss 4.2
  • 20091115 - Updating site for JBoss 5.1.0
    • Notes (to be integrated into main page)
      • Disable JBossPlatform public JPAClassLoaderHolder getNewTempClassLoader(PersistenceUnitInfo puInfo) workaround
      • Increase server start timeout past 50 sec (60+) for a slower T4200 2.2GHz Core2 Win7-64 machine