Skip to main content
Jump to: navigation, search

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

(Perform a JPQL query)
(Perform a JPQL query)
Line 242: Line 242:
15:57:41,582 INFO  [STDOUT] [EPS Finest]: 2008.04.14 15:57:41.581--UnitOfWork(5316732)--Thread(Thread[http-,5,jboss])--Register the existing object
15:57:41,582 INFO  [STDOUT] [EPS Finest]: 2008.04.14 15:57:41.581--UnitOfWork(5316732)--Thread(Thread[http-,5,jboss])--Register the existing object
**Browser Output
**Browser Output

Revision as of 15:26, 14 April 2008

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 entitity java classes have been generated using the Eclipse DALI tool.

JBoss configuration Changes

  • 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> 

EclipseLink JAR location

The eclipselink.jar should be placed off of $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
  • Persistence.xml
    • Put persistence.xml in yourProjectWeb\src\META-INF with a reference to your SessionCustomizer
<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
            <property name="" value="org.eclipse.persistence.platform.server.jboss.JBossPlatform"/>
            <property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="" value=""/>            
            <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="" value="true"/>
            <property name="eclipselink.jdbc.write-connections.min" value="3"/>
            <property name="eclipselink.jdbc.write-connections.max" value="8"/>
            <property name="" value="3"/>
            <property name="" value="3"/>
            <property name="eclipselink.cache.size.default" value="500"/>
            <property name="eclipselink.cache.type.default" value="Full"/>
            <property name="eclipselink.orm.throw.exceptions" value="false"/>

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

  • 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.
  • Weaving is turned off when running in SE mode because of [JIRA-572]
  • 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] [EPS 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] [EPS 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] [EPS 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] [EPS 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] [EPS 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] [EPS Finest]: 2008.04.14 15:57:41.391--UnitOfWork(5316732)--Thread(Thread[http-,5,jboss])--Execute query ReadAllQuery(
15:57:41,393 INFO  [STDOUT] [EPS 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] [EPS 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