Already a member?
Sign in
Welcome! This is a website that everyone can build together. It's easy!
JRuby on Rails: Deploying to Oracle Containers for Java EE (OC4J)
This is a basic howto on deploying a JRuby on Rails application to Oracle Containers for Java EE (OC4J) Standalone.
Prerequisites
Note: We used Rails 2.x in this guide, although there will be little changes in Rails 1.x
Installation
Note: JRuby leverages JDBC to access various JDBC compliant database systems and Ruby-OCI8 can't work with JRuby because "Many Gems will work fine in JRuby however some Gems build native C libraries as part of their install process. These Gems will not work in JRuby unless the Gem has also provided a Java equivalent to the native library." as stated in the JRuby Wiki. The latest ActiveRecord JDBC Adapter (v0.8.0) only works with JRuby 1.1RC3 or future version.
Performing the following steps to create a simple Rails 2.0 application.
OC4J stands for "Oracle Containers for J2EE". OC4J standalone is the Oracle AS Containers for J2EE distribution which can be executed outside of the standard OracleAS environment. It is very easy to install(simply unzip it). Typically OC4J standalone would be used for development purposes and for small scale web solutions. OC4J 10g Standalone ships with Oracle JDBC driver for Java 1.4, so deploying JRuby on Rails application on it is made much easier:
Troubleshooting
Contents
- Prerequisites
- Installation
- Create a Rails 2.0 Application
- Packaging Rails app into WAR
- Deploy on OC4J Standalone
- Troubleshooting
- References
Prerequisites
- Windows 2000/XP / Linux
- Java 5
- JRuby 1.1RC3
- Rails 2.0/1.2
- activerecord-jdbc-adapter 0.8.0
- Oracle JDBC Driver
- Warbler
- Oracle Database 11gR1 / Oracle Database 10g XE
- Oracle Containers for J2EE (OC4J) 10gR3
Note: We used Rails 2.x in this guide, although there will be little changes in Rails 1.x
Installation
- First download and install JDK 5, JRuby 1.1 and make sure you set $JAVA_HOME/bin and $JRUBY_HOME/bin in your $PATH variable.
- Install Rails 2.0:
sudo jruby -S gem install rails [-y]
- Install activerecord-jdbc-adapter:
sudo jruby -S gem install activerecord-jdbc-adapter
- Download appropriate Oracle JDBC Driver jar file (ojdbc14.jar for DB 10g and ojdbc5.jar for DB 11g) and copy it into $JRUBY_HOME/lib.
Note: JRuby leverages JDBC to access various JDBC compliant database systems and Ruby-OCI8 can't work with JRuby because "Many Gems will work fine in JRuby however some Gems build native C libraries as part of their install process. These Gems will not work in JRuby unless the Gem has also provided a Java equivalent to the native library." as stated in the JRuby Wiki. The latest ActiveRecord JDBC Adapter (v0.8.0) only works with JRuby 1.1RC3 or future version.
Create a Rails 2.0 Application
Performing the following steps to create a simple Rails 2.0 application.
- Create the app:
jruby -S rails bookstore
cd bookstore
- Modify config/database.yml as any of the three forms below:
We can use both the "jdbc" adapter or the "oracle" adapter as when using C Ruby, and set the production configuration the same as development configuration because when deployed Rails uses production configuration.development: adapter: jdbc
driver: oracle.jdbc.driver.OracleDriver
url:jdbc:oracle:thin:@<hostname>:<port>:<sid>
username: railsuser
password: railspasswd
production:development
development: adapter: oracle
url:jdbc:oracle:thin:@<hostname>:<port>:<sid> username: railsuser
password: railspasswd
production:development
development: adapter: oracle
host: <hostname>
port: 1521 (by default)
database: <sid>
username: railsuser
password: railspasswd
production:development
In Rails 1.2, the jdbc adapter gem is not required by default by ActiveRecord. We need to add "jdbc" into %JRUBY_HOME%\lib\ruby\gems\1.8\gems\activerecord-x.y.z\lib\active_record.rb as below:unless defined?(RAILS_CONNECTION_ADAPTERS)
RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase jdbc)
end
Thus all Rails application can share this configuration and users don't need to config each newly created Rails application to use JDBC adapter.
- Generate scaffold:
ruby script/generate scaffold book name:string description:text price:float
The scaffold API in Rails 2.0 is different from Rails 1.2. We need to specify field name and data type in the command line. This command will generate the model, view, controller and db migration files automatically.
- Perform database migration:
jruby -S rake db:migrate
- Launch the default web server:
jruby script/server
- Navigate to http://localhost:3000/books to access the application.
Packaging Rails app into WAR
Warbler is a gem to make a single WAR file out of a Rails project and provides a minimal, flexible, Ruby-like way to bundle up all of your application files for deployment to a Java application server. It's developed by Nick Sieger, one of the core developer of JRuby. Warbler bundles JRuby and the Goldspike servlet for dispatching requests to your application inside the java application server, and assembles all jar files in $WARBLER_HOME/lib/*.jar into your application. No external dependencies are needed.
- Install Warbler :
jruby -S gem install warbler
- Generate the Warbler configuration file:
jruby -S warbler config
and change the 13th line of config/warble.rb file toconfig.gems = ["activerecord-jdbc-adapter", "rails" ]
because Warbler doesn't automatically package the activerecord JDBC driver.
- Package the app:
jruby -S warbler [war]
A WAR file(bookstore.war) is generated under the top directory of the Rails app.
Deploy on OC4J Standalone
OC4J stands for "Oracle Containers for J2EE". OC4J standalone is the Oracle AS Containers for J2EE distribution which can be executed outside of the standard OracleAS environment. It is very easy to install(simply unzip it). Typically OC4J standalone would be used for development purposes and for small scale web solutions. OC4J 10g Standalone ships with Oracle JDBC driver for Java 1.4, so deploying JRuby on Rails application on it is made much easier:
- Use the Application Server Control Console to deploy (access http://localhost:8888/em), or
- Use the command line :
java -jar $OC4J_ROOT/j2ee/home/admin_client.jar deployer:oc4j:localhost:23791 oc4jadmin -deploy -file /path/to/bookstore.war -deploymentName bookstore -bindAllWebApps
- Navigate to http://localhost:8888/bookstore
Troubleshooting
- While navigating to http://localhost:8888/bookstore/books after deploying the application on OC4J, the browser displays the following error message:
No :secret given to the #protect_from_forgery call.
Set that or use a session store capable of generating its own keys (Cookie Session Store)
Solution: Uncomment the secret in line 9 in app/controllers/application.rb then re-deploy the app. See link.
- While navigating to http://localhost:3000/bookstore/books, the browser displays the following error message:
driver encountered an error: java.sql.SQLException: Io exception: SO Exception was generated.
Solution: This is due to malformed database connection string in database.yml which should look like this:url: jdbc:oracle:thin:@hostname:1521:XE
The port 1521 cannot be omitted with ojdbc14.jar but it works with ojdbc5.jar.
References
- OC4J FAQ
- JRuby on Rails on Tomcat
- Deploying a Rails Application on Tomcat
- Warbler, A Little Birdie To Introduce Your Rails App To Java
- JRuby .war packaging for Rails 2.0
- Warbler on JRuby Wiki
- Warbler RDoc
- Documentation of Activerecord-jdbc-adapter
Latest page update: made by jesse.hu
, May 8 2008, 3:20 AM EDT
(about this update
About This Update
fix a format error
- jesse.hu
3 words added
2 words deleted
view changes
- complete history)
fix a format error
- jesse.hu
3 words added
2 words deleted
view changes
- complete history)
More Info: links to this page
