Skip to main content
Jump to: navigation, search

EclipseLink/Examples/JPA/JBoss Web Tutorial

< EclipseLink‎ | Examples‎ | JPA
Revision as of 12:07, 24 September 2008 by (Talk | contribs) (Persistence.xml)

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 pluggin 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.

Development Environment

Hardware: Windows Vista SP1, HP Core2Quad, 2.4Ghz, 3Gb Ram

Software: Eclipse IDE for Java EE 3.4 M5 Ganymede (Feb 2008) with all 5 packages (DTP 1.6, EMF 2.4, GEF 3.4, WTP 3.0, XSD 2.4), Oracle 11g DB, Java JDK 1.5.0_11, JBoss 4.2.2

This example will run fine with Eclipse 3.3 and any DB that EclipseLink supports.


  • Install Eclipse
    • I installed a clean version of Eclipse Ganymede M5 with all of WTP 3.0
  • Install a Database
    • In this example I am using Oracle 11g, the table schemas have already been created manually and all entity java classes have been generated using the Eclipse DALI tool.

JBoss Server Configuration Changes

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 =  
  "" 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 Address already in use: JVM_Bind
	at Method)

Try changing the following jboss:service=Naming port to any availabe 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  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>/

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


EclipseLink JAR location

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

JDBC JAR location

Copy your jdbc driver jar to $JBOSS_HOME/server/default/lib

Create server in Eclipse

Open the servers view New | Server | JBoss | JBoss v4.2.

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

  • Path changes
	<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.

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.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.eclipse.persistence.internal.sessions.factories.SessionCustomizer;
import org.eclipse.persistence.sessions.JNDIConnector;
import org.eclipse.persistence.sessions.Session;
 * See
 * 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) {
    // Change from Composite to String_Lookup
    // 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="" xmlns:xsi="" xsi:schemaLocation="">
  <persistence-unit name="unifiedJBoss" transaction-type="JTA">
      <!-- non-jta-data-source>java:/OracleDS</non-jta-data-source-->
..other entities
        <!-- deprecated property name="" value="JBoss"/-->
        <property name="" 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 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] 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(
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
  • Browser Output

Eclipselink example jpa jboss web jpql action cap.jpg


Back to the top