Skip to main content
Jump to: navigation, search

EclipseLink/Examples/JPA/JBoss Web Tutorial

< EclipseLink‎ | Examples‎ | JPA
Revision as of 16:19, 14 April 2008 by Michael.obrien.oracle.com (Talk | contribs) (JBoss configuration Changes)

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 11.1.0.6.0, 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.

Prerequisites

  • 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.
C:\opt\jboss422
Where 
JBOSS_HOME=C:/opt/jboss422

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

<datasources>
  <no-tx-datasource>
    <jndi-name>OracleDS</jndi-name>
	<use-java-context>true</use-java-context>
    <connection-url>jdbc:oracle:thin:@localhost:1521:orcl</connection-url>
    <driver-class>oracle.jdbc.OracleDriver</driver-class>
    <user-name>username</user-name>
    <password>password</password>
<!--min-pool-size>20</min-pool-size>
<max-pool-size>100</max-pool-size>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>1</idle-timeout-minutes-->
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
      <metadata>
         <type-mapping>Oracle9i</type-mapping>
      </metadata>
  </no-tx-datasource>
  • Edit oracle-xa-ds.xml
  • Enter the following into $JBOSS_HOME\server\default\deploy\oracle-xa-ds.xml
<datasources>
  <xa-datasource>
    <jndi-name>XAOracleDS</jndi-name>
    <use-java-context>true</use-java-context>
    <track-connection-by-tx>true</track-connection-by-tx>
	<isSameRM-override-value>false</isSameRM-override-value>
    <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
    <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>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
    <no-tx-separate-pools/>
      <metadata>
        <type-mapping>Oracle9i</type-mapping>
      </metadata>
  </xa-datasource>
  • Enter the following into $JBOSS_HOME\server\default\conf\login-config.xml
<policy>
<application-policy name = "OracleDbRealm"> 
   <authentication> 
      <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> 
      </login-module> 
   </authentication> 
</application-policy>

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
 * 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) {
			e.printStackTrace();
		}
		connector.setDataSource(dataSource);
		//connector.setContext(context);
		// Change from Composite to String_Lookup
		connector.setLookupType(JNDIConnector.STRING_LOOKUP);
		// Set the new connection on the session
		session.getLogin().setConnector(connector);
	}
}
  • Persistence.xml
    • Put persistence.xml in yourProjectWeb\src\META-INF with a reference to your SessionCustomizer
<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="unifiedJBoss" transaction-type="JTA">
       <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:/XAOracleDS</jta-data-source>
        <!-- non-jta-data-source>java:/OracleDS</non-jta-data-source-->
        <class>org.eclipse.persistence.example.unified.business.StatClass</class>
..other entities
        <properties>
            <property name="eclipselink.server.platform.class.name" value="org.eclipse.persistence.platform.server.jboss.JBossPlatform"/>
            <property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.OraclePlatform"/>            
            <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.jdbc.read-connections.shared" value="true"/>
            <property name="eclipselink.jdbc.write-connections.min" value="3"/>
            <property name="eclipselink.jdbc.write-connections.max" value="8"/>
            <property name="eclipselink.jdbc.read-connections.min" value="3"/>
            <property name="eclipselink.jdbc.read-connections.max" 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"/>
        </properties>       
    </persistence-unit>
</persistence>


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-127.0.0.1-8080-2,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-127.0.0.1-8080-2,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-127.0.0.1-8080-2,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] [EPS Finest]: 2008.04.14 15:57:23.246--ServerSession(28649942)--Thread(Thread[http-127.0.0.1-8080-2,5,jboss])--End predeploying Persistence Unit unifiedJBoss; state Predeployed; factoryCount 0
15:57:41,175 INFO  [STDOUT] [EPS Config]: 2008.04.14 15:57:41.175--ServerSession(28649942)--Connection(12593356)--Thread(Thread[http-127.0.0.1-8080-2,5,jboss])--Connected: jdbc:oracle:thin:@localhost:1521:orcl
	User: username
	Database: Oracle  Version: Oracle Database 11g Release 11.1.0.0.0 - Production
	Driver: Oracle JDBC driver  Version: 11.1.0.0.0-Beta5
15:57:41,391 INFO  [STDOUT] [EPS Finest]: 2008.04.14 15:57:41.391--UnitOfWork(5316732)--Thread(Thread[http-127.0.0.1-8080-2,5,jboss])--Execute query ReadAllQuery(org.eclipse.persistence.example.unified.business.StatLabel)
15:57:41,393 INFO  [STDOUT] [EPS Fine]: 2008.04.14 15:57:41.393--ServerSession(28649942)--Connection(32240107)--Thread(Thread[http-127.0.0.1-8080-2,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-127.0.0.1-8080-2,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


References

Back to the top