The participants of the FORPC101 series of courses have been requesting me for a small article related to JRuby; so here it is.
In this article, we shall see how easy it is to connect to a database (MySQL) using ActiveRecord and a JDBC driver using JRuby.
I am assuming that you have Java 1.6, JRuby 1.1.3, MySQL installed and working.
I am using a Windows XP box but these examples will work on a Mac or Linux box too.
For Ruby Developers, JRuby offers a deployment platform that is well understood, particular in corporations. For a Java community, JRuby is important because it offers a chance to experience a powerful language and framework while still taking advantage of Javaâ€™s excellent libraries and the ability to work in both Ruby and Java – Martin Fowler
MySQL is a one of the most popular open source databases around and is used by many prominent organizations from Yahoo to NASA.
ORM and ActiveRecord
ORM, stands for object-relational mapping. ORM libraries map database tables to classes. If a database has a table called orders, our program will have a class named Order. Rows in this table correspond to objects of the class – a particular order is represented as an object of class Order. Within that object, attributes are used to get and set the individual columns. Our Order object has methods to get and set the amount, the sales tax, and so on.
So an ORM layer maps tables to classes, rows to objects, and columns to attributes of those objects. Class methods are used to perform table-level operations, and instance methods perform operations on the individual rows. Active Record is an ORM.
ActiveRecord relieves us of the hassles of dealing with the underlying database, leaving us free to work on business logic. ActiveRecord relies on convention over configuration. Wherever possible, ActiveRecord guesses the correct configuration by reflecting against the data schema. When you do need a specific override, you specify the override directly in your class.
Also, ActiveRecord assumes that:
- database table names, like variable names, have lowercase letters and underscores between the words
- table names are always plural
- files are named in lowercase with underscores
ActiveRecord implements some of its funkiest magic with method_missing because ActiveRecord::Base overrides the Kernelâ€™s method_missing method.
JRuby comes with a fairly loaded standard library from scratch but that does not mean there arenâ€™t other things youâ€™ll need. Almost all of them are installable as Gems. RubyGems is the premier package management tool for Ruby. It works fine with JRuby and JRuby ships with it. You use it through the gem command. Now, we need to run the JRuby version of the gem command and to ensure that, we use the â€“S flag to the interpreter.
We shall install ActiveRecord as a freestanding gem in JRuby:
C:\>jruby -S gem install activerecord
In ActiveRecord there is this concept of an adapter. Each adapter is specific for one database, so thereâ€™s a MySQL adapter, an Oracle adapter, and so on. At the moment, there are few adapters that ship with ActiveRecord.
The activerecord-jdbc-adapter adapter is needed as it allows use of virtually any JDBC-compliant database with your JRuby application. This adapter supports:
- MySQL – Complete support
- PostgreSQL – Complete support
- Oracle – Complete support
- Microsoft SQL Server – Complete support except for change_column_default
DB2 – Complete, except for the migrations:
- FireBird – Complete, except for change_column_default and rename_column
Derby – Complete, except for:
- HSQLDB – Complete
- H2 – Complete
- SQLite3 – work in progress
We will install the activerecord-jdbcxxx-adapter, where xxx would be the database name, for example activerecord-jdbcmysql-adapter for MySQL. We are going to use the adapter for a specific database (MySQL). You can install it directly and a driver gem will be installed as well.
Other databases will require testing and likely a custom configuration module.
Ensure that your MySQL service is running.
Open a command window and type:
C:\>jruby -S gem install activerecord-jdbcmysql-adapter
From your JRuby code, you need to specify only the name of the adapter you want to use; ActiveRecord will provide the Ruby bridge code.
The article is continued in Part 2.