Moving Tomcat Toplink Apps to Glassfish v3 Prelude

With Glassfish V3 and Netbeans 6.5, it is easy to move applications which run on Tomcat using Toplink/EclipseLink to Glassfish v3 Prelude. The sample application contained in the zip file JPAWeb_TC6_Toplink-4.zip, is a simple web app using JSTL, Servlet, JSP and TopLink Essentials to store contact information into a MySQL database.

To Deploy and run the application on Tomcat, you need place a copy of the MySQL JDBC driver in $TOMCAT_HOME/lib and have a MySQL database which is accessible and that you have permissions to create tables in. The application uses JNDI to access a Data Source defined on Tomcat (in our case via the context.xml). This requires you to use specify the Toplink property toplink.session.customizer in your persistence.xml indicating that a SessionCustomizer will be used so that the lookup type on the JNDI connector will be a STRING_LOOKUP instead of Composite :

 
<?xml version="1.0" encoding="UTF-8"?>
<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="JPAContactJNDIPU" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
 <non-jta-data-source>java:comp/env/jdbc/mysqlTestDB</non-jta-data-source>
    <class>demo.entity.Contact</class>
    <properties>
      <property name="toplink.session.customizer" value="demo.JNDITopLinkSessionCustomizer"/>
      <property name="toplink.logging.level" value="FINE"/>
    </properties>
  </persistence-unit>
</persistence>

The code for JNDITopLinkSessionCustomizer is:

package demo;

import javax.naming.Context;
import javax.naming.InitialContext;
import oracle.toplink.essentials.jndi.JNDIConnector;
import oracle.toplink.essentials.sessions.Session;
import oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer;


/\*\*
\*
\* @author Owner
\*/
public class JNDITopLinkSessionCustomizer implements SessionCustomizer {

  /\*\*
   \* Get a dataSource connection and set it on the session with lookupType=STRING_LOOKUP
   \*/
  public void customize(Session session) throws Exception {
      JNDIConnector connector = null;
             Context context = null;
      try {
          context = new InitialContext();
          if (null == context) {
              throw new Exception("JNDIEclipseLinkSessionCustomizer: Context is null");
          }
          connector = (JNDIConnector) session.getLogin().getConnector(); // possible CCE
          // Change from Composite to String_Lookup
          connector.setLookupType(JNDIConnector.STRING_LOOKUP);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}
You will also have to modify the context.xml to point to your database:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/JPAWeb_TC6_Toplink_To_V3">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/mysqlTestDB" password="bar" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/mysqltest?autoReconnect=true" username="foo"/>
</Context>
You can now deploy the application using Netbeans to Tomcat and enter some simple data.

Deploytc-1

Run Tc-1

Moving the Application to Glassfish v3 Prelude

Moving the application from Tomcat to Glassfish v3 Prelude is very easy to do:

1. Make a copy of the application in Netbeans by right clicking on the project and selecting copy(call the copy of the project JPAWeb_TC6_Toplink_To_V3).

Copyproject-3

2. Under the project properties, indicate that you are going to deploy to Glassfish V3 Prelude:

Preludeprops-2

3. Add a JNDI Resource Entry to the sun-web.xml:

Sun-Web-1

4. Place the MySQL JDBC driver in $GLASSFISH_V3_HOME/glassfish/domains/domain1/lib.ext

5. Start Glassfish V3 Prelude and add a Connection Pool which points to your MySQL server and database

Connpool-1

Connpool2

5. Add JDBC Resource which uses the Connection Pool

Jdbc Resource-2

6. Deploy the application to Glassfish v3 Prelude

Deployv3-1

7. Run the application deployed on Glassfish

Run V3-1

Migrating from Toplink to Eclipselink It is very easy to change your application to run with EclipseLink.

1. Add the EclipseLink library to your Netbean's project and remove the Toplink Library

Eclipselibrary-1

2. Change your persistence.xml to use EclipseLink (note: that the Design view of persistence.xml will allow you to change Persistence Provider, however it currently does not modify the property names when going from Toplink to Eclipselink ):

<?xml version="1.0" encoding="UTF-8"?>
<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="JPAContactJNDIPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
 <non-jta-data-source>java:comp/env/jdbc/mysqlTestDB</non-jta-data-source>
    <class>demo.entity.Contact</class>
    <properties>
      <property name="eclipselink.session.customizer" value="demo.JNDIEclipseLinkSessionCustomizer"/>
      <property name="eclipselink.logging.level" value="FINE"/>
    </properties>
  </persistence-unit>
</persistence>

3. You will need to change the imports for your SessionCustomizer since EclipseLink is being used (note I changed the name of the class for clarity) JNDIEclipseLinkSessionCustomizer :


package demo;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.sessions.JNDIConnector;
import org.eclipse.persistence.sessions.Session;
/\*\*
\*
\* @author Owner
\*/
public class JNDIEclipseLinkSessionCustomizer implements SessionCustomizer {

  /\*\*
   \* Get a dataSource connection and set it on the session with lookupType=STRING_LOOKUP
   \*/
  public void customize(Session session) throws Exception {
      JNDIConnector connector = null;
             Context context = null;
      try {
          context = new InitialContext();
          if (null == context) {
              throw new Exception("JNDIEclipseLinkSessionCustomizer: Context is null");
          }
          connector = (JNDIConnector) session.getLogin().getConnector(); // possible CCE
          // Change from Composite to String_Lookup
          connector.setLookupType(JNDIConnector.STRING_LOOKUP);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}

You can now run and deploy the application using EclipseLink for your persistence provider.

Doing a little more clean up if you are only going to run on Glassfish v3 Prelude

If you are just going to run on Glassfish v3 Prelude, you can make a few additional minor tweaks to your application:

1. Simplify your persistence.xml by removing the eclipselink.session.customizer (only needed for JNDI look ups on Tomcat) and simplifying your non-jta-datasource entry (you must specify java:comp/env/ when using Tomcat):

<?xml version="1.0" encoding="UTF-8"?>
<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="JPAContactJNDIPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
 <non-jta-data-source>jdbc/mysqlTestDB</non-jta-data-source>
    <class>demo.entity.Contact</class>
    <properties>
      <property name="eclipselink.logging.level" value="FINE"/>
    </properties>
  </persistence-unit>
</persistence>


2. Streamline your context.xml entry to omit the Will ignore everything from DBCP properties so it would just look like:

<Resource
name="jdbc/mysqlTestDB"
auth="Container"
type="javax.sql.DataSource"/>

Technorati Tags: ,

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

user12629431

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks