Tuesday Jun 10, 2008

Using Java2DB feature of GlassFish v2

GlassFish V2 provides Java2DB feature which let users to specify database schema information in a JPA application. The persistence provider will use the information to define the corresponding schema in a database. I am using a Datatype test application as an example below to show how to use the Java2DB feature of GlassFish V2.

Install GlassFish V2 and Create JDBC connection

  1. Download GlassFish V2 and install at [GF_Install_Dir].
  2. Include [GF_Install_Dir]/bin in PATH for convenience.
  3. Start GlassFish V2 domain and Derby DB with
    ~/testbed > asadmin start-domain

    ~/testbed > asadmin start-database

  4. Create JDBC connection-pool:
    ~/testbed > asadmin create-jdbc-connection-pool --user admin --terse=false --target server --property User=dbuser:Password=dbpassword:dataBaseName=testdb:serverName=localhost:portNumber=1527:create=true --echo=true --port 4848 --allownoncomponentcallers=false --interactive=true --passwordfile ~/testbed/adminpassword.txt --host localhost --datasourceclassname org.apache.derby.jdbc.ClientDataSource --nontransactionalconnections=false --restype javax.sql.DataSource datatype-pool1

  5. Create JDBC resource:
    ~/testbed > asadmin create-jdbc-resource --interactive=true --passwordfile ~/testbed/adminpassword.txt --user admin --terse=false --enabled=true --host localhost --target server --echo=true --connectionpoolid datatype-pool1 --port 4848 jdbc/datatype1

Create the Datatype Test Application to Use Java2DB

Download the Datatype test and unzip into ~/testbed directory. Or use the following steps to create your own Datatype test application:
  1. Create the Datatypes entity beans and session bean.
  2. Create the persistence.xml and set property "toplink.ddl-generation" to "drop-and-create-tables" to use the Java2DB feature:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
        <persistence-unit name ="manager">
                <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
  3. Package the Datatype Test Application into an EAR file. The file/directory structure of ejb-cmp-datatypesApp.ear in the zip file above can be used as an example.

Deploy the Datatype Test Application and Run

  1. Deploy the Datatype Test App to GlassFish V2 and the schema will be created during deployment:
    ~/testbed > asadmin deploy --user admin --terse=false --target server --echo=true --upload=true --port 4848 --interactive=true --passwordfile ~/testbed/adminpassword.txt --availabilityenabled=false --enabled=true --retrieve ~/testbed/ejb-cmp/archive --host localhost --generatermistubs=false --precompilejsp=false --force=true --name ejb-cmp-datatypesApp --verify=false ~/testbed/ejb-cmp/archive/ejb-cmp-datatypesApp.ear

  2. Run the application with appclient:
    ~/testbed > appclient -client ~/testbed/ejb-cmp/archive/ejb-cmp-datatypesAppClient.jar EJB3_DataTypes_Java2DB

The following output from the Datatype test will be printed on console:
-         Update_row : PASS     -
-         Query_Integer : PASS  -
-         Read_row: PASS        -
-         Populate_INTEGER_MAXVAL: PASS -
-         Query_Long : PASS     -
-         : PASS        -
-         Create_row_ZEROVAL: PASS      -
-         Average_LongData : PASS       -
-         Average_FloatData : PASS      -
-         Query_Short : PASS    -
-         Query_Byte : PASS     -
-         Query_SqlDate : PASS  -
-         Average_ByteData : PASS       -
-         Populate_LONG_MAXVAL: PASS    -
-         Create_row_MEDIANVAL : PASS   -
-         Average_IntegerData : PASS    -
-         Query_Float : PASS    -
-         Populate_BYTE_MAXVAL: PASS    -
-         Populate_SHORT_MAXVAL: PASS   -
-         Average_ShortData : PASS      -
-         Query_Row_MAX_Value : PASS    -
-         Query_UtilDate : PASS -
-         Query_TimeStamp : PASS        -
-         Query_Character : PASS        -
-         Delete_row : PASS     -
Total PASS: 25
Total FAIL: 0
Total DNR: 0

Friday May 23, 2008

JRuby/Rails with JPA on Glassfish V3

JRuby/Rails by default uses ActiveRecords package for the purposes of Object Relational Mapping (ORM). JPA is a JavaEE ORM technology and the advantage of JPA is its pluggability. Modeling class and database migration in Rails are what Entity class and persistence provider do in JavaEE.

The TP2 of GlassFish V3 was released just before 2008 JavaONE and the default persistence provider of V3 is Eclipse Link. It'll be interesting to see how easy it is to use JPA in a classic blog rails application. Vivek wrote in his blog that he used JPA and Rails on GlassFish V2 with TopLink as persistence provider. Here I would modify the blog demo application in his blog to fit for GlassFish V3 and Eclipse Link.

Install JRuby and Rails

  1. Download the JRuby 1.0.3 zip file . I am setting up everything on Solaris 10 Sparc platform.
  2. Unzip the file and set the JRUBY_HOME to [Install_Dir]/jruby-1.0.3
  3. Include [JRUBY_HOME]/bin in PATH for convenience.
  4. Install Rails 1.2.6:
    gem install rails -v=1.2.6 --no-rdoc --no-ri -–include-dependencies
    The latest Rails release 2.0.2 has a lot of differences with Rails 1.2.6. In this blog, JRuby1.0.3/Rails1.2.6 will be used and I'll write a follow up blog for the combination of JRuby 1.1.1/Rails 2.0.2.
  5. Install warbler:
    gem install warbler
    Warbler is needed for creating war file from JRuby application.

Setup GlassFish V3 and MySQL

  1. Download TP2 of GlassFish V3 and unzip.
  2. Add JRUBY_HOME value to the GlassFish V3 config file. For example, in my case add
    to the bottom of [Install_Dir]/glassfishv3-tp2/glassfish/config/asenv.conf
  3. Setup the MySQL community server by following the MySQL menu.
  4. Create a new DataBase instance named "test" in MySQL.
  5. Copy the MySQL connector j driver to [Install_Dir]/glassfishv3-tp2/glassfish/lib.
  6. Set JAVA_HOME and include [JAVA_HOME]/bin to path. JDK 1.6.0_06 was used in this blog.
  7. Start the GlassFish V3:
    [Install_Dir]/glassfishv3-tp2/glassfish/asadmin start-domain.

Create Java Entity Beans

Create Java Entity bean by following the steps described in Vivek’s blog. A short cut is to download the Entity code and import the code to Netbeans 6.1. The persistence provider in persistence.xml needs to be changed to Eclipse Link as shown below:

If the image appears small in your browser, please right click the image and choose "View Image" to see details. Build the project with “Shift + F11” and a new Post.jar will be created under Post/dist dir.

Create the Rails Application

  1. Download the blog demo Rails application. Unzip the file and change directory to [Install_Dir]/jpa_blog/WEB_INF. Create a directory called classes.
  2. Add the Java Entity beans by unjar the Post.jar.
  3. Use warbler to package the Rails app into a war file: cd [Install_Dir]/jpa_blog; jruby –S warble
The jpa_blog.war will be create under [Install_Dir]/jpa_blog.

Deploy the Blog Demo Rails Application

Deploy the repackaged Rails application onto GlassFish V3:

[Install_Dir]/glassfishv3-tp2/glassfish/asadmin deploy jpa_blog.war
In the GlassFish V3 server.log, the following message should display if the deployment is successful:
[#|2008-05-22T18:42:08.977-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Info: using runtime pool timeout of 30 seconds|#]
[#|2008-05-22T18:42:08.979-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Warning: no initial runtimes specified.|#]
[#|2008-05-22T18:42:08.981-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Warning: no max runtimes specified.|#]
[#|2008-05-22T18:42:09.009-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|Loading application jpa_blog at /jpa_blog|#]
[#|2008-05-22T18:42:09.034-0700|INFO|GlassFish10.0|javax.enterprise.system.tools.deployment|_ThreadID=12;_ThreadName=Thread-5;|Deployment of jpa_blog done is 15526 ms|#]

Then, access the blog application at: http://[HOST_NAME]:8080/jpa_blog/blog and the application should display:

Click “New post” link to create new blog entry:

Click “Create” button to submit the blog and the application will show the new blog created and a list of blogs created before:


Ming Zhang


« April 2014