Rollin onj Rubyv3

Preview:

DESCRIPTION

Using JPA vs. Activerecord for persistence in JrubyChris Bucchere and Pieter Humphrey from Openworld 2009.

Citation preview

Enterprise Ruby on Rails: Rolling with JRuby on Oracle WebLogic SuiteChris BucchereCEO of BDGPieter HumphreyPrincipal Product Manager, Oracle

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Intro Slides

• Chris Bucchere – CEO, BDG– Oracle ACE Director

• Pieter Humphrey– Principal Product Director, Oracle

Ruby 101

Ruby Overview

• Key Features– Purely object-oriented– Dynamically-typed– Compact, yet easy to read syntax– Blocks that are closures– Open classes and objects– Domain Specific Language capabilities

Strong

Weak

staticdynamic

Basics

Iteration, Blocks & Closures

String Handling & Regular Expressions

Guiding Principles

• Duck Typing– If it walks like a duck and quacks like a duck, then it probably

is a duck

• Convention over Configuration– Down with XML!

• Rapid Prototyping– Allows for agile development with short iterations

• Sustainable Productivity

Example Applications

• Twitter• 43Things• 37 Signals– Basecamp– Campfire– Backpack– Highrise

• Social Collective, Inc.– The Social Collective– CrowdCampaign

• Thousands of others. . . .

Migrations

ActiveRecord & Validations

Scaffolding

Built-in MVC

UI Helpers

Show methe money!

JRuby and Database Access

What is JRuby?• JRuby is a port of Ruby onto Java, written in C & ruby• Offers Native Multithreading• Full Access to Java Libraries

Configuration DSL

Exte

rnal

Sys

tem

s

Static Layer - Static typed legacy Java Libraries

Dynamic layer - Application Code

Why JRuby?

• To use Java libraries from code written in Ruby syntax– Swing, etc

• To use Ruby libraries from code written in Java syntax– Via ActiveRecord with Bean Scripting Framework (BSF)– Via “Scripting for the Java Platform” (JSR 223) in Java 6

• Performance vs. C-based interpreter is a hot debate– Depends who you ask, but it seems performance is surprisingly good

• Great for the following use cases– Agile Methodology shops, small teams– REST / Web App for routing data to back end resources– Enterprise Deployments

Why JRuby cont.• Integration with Java libraries:

JRuby classes can• extend Java classes• implement a single Java interface• add methods to existing Java classes

• Native threads• Unicode support• Portability• Can drop in into SQL easily ( find_by_sql )• Many DB accees options – this is where Ruby shines

JRuby Developer Tool support

• IDEs– Eclipse– RDT plug-in supports Ruby development– RadRails IDE– IntelliJ IDEA– NetBeans

• Editors– syntax highlighting (emacs, jEdit and Vim)

• Spring– an IOC + Java EE framework – supports beans implemented in Java, JRuby, Groovy

Rack

• Ruby’s CGI / Servlet equivalent – In web.xml, a rack servlet filter will dispatch params /

requests to the RACK component

• Rack provides a single API to abstracts multiple webservers ( mongrel, fast-cgi, webrick, fusion passenger )• Standardizes the request that a Ruby app expects

(get params), wrapping HTTP requests/responses• Based on WSGI for Python

<Insert Picture Here>

Ruby DBI

Ruby Script

DBI

API

ADO ODBCDatabasesDB2, Informix, mySQL, Oracle

ActiveRecord

• ActiveRecord is …– a Ruby library for accessing relational databases– Simple, yet amazingly powerful / portable DB access– Tricky to tune under load, not for use with legacy schemas

• It can be used from Java through JRuby– install the ActiveRecord gem – under Java 5 and earlier• use Bean Scripting Framework (BSF)– classpath must contain bsf.jar, jruby.jar, jvyaml.jar,

commons-logging-1.1.jar– under Java 6 and later• use JSR 223 Scripting API

• Convention, not configuration• Great for greenfield web apps

Getting Started with JRuby

• Getting JRuby– http://dist.codehaus.org/jruby/1.3.1/ – jRuby1.3.1/bin should be in PATH– JDK 1.5 or more should be in PATH, JAVA_HOME set– Apache Ant (http://jakarta.apache.org/ant/) 1.7 or greater

• Getting Rails (the web tier for Ruby)– Once JRuby environment is set:– 'gem install rails -v 2.2.2'

• Getting Warbler (Java EE deployment unit converter)– 'gem install warbler -v 0.9.12‘– Make sure you aren’t behind a proxy server

Demo Application: Maintain FOD Supplier Catalog

FOD SCHEMA

WebLogicServer

EclipseLinkRun WARBLER

Ruby App

JRubyinterpreter

Ruby App

JRuby, WebLogic, TopLink Database Access

JPA—in a Nutshell

• Concrete classes (POJOs)• No required interfaces or inheritance• new() for instance creation• Mapping using annotations and/or XML• “Managed” by an EntityManager• Can leave the Container (“detached”)

Where does JPA fit?

RelationalDatabase

Java SE/EE/OSGi

JPA Persistence Provider

Design Time Runtime

Mapping Metadata

Java Classes

Database Schema

<Insert Picture Here>

JRuby + JPA = Enterprise friendly Rails

• JPA is better for working with pre-existing database schemas

• JRuby/JPA/WebLogic = easy Ops compliance• Development tools: Standalone, JDev, Eclipse• WebLogic provides connection pooling• WebLogic provides Oracle RAC integration• WebLogic provides Oracle JPA / Coherence integration• WebLogic provides XA with Messaging & DB• WebLogic provides easy ways to expose as JAX-WS, JAX-

RPC, Spring Beans• WebLogic provides SUN RI: EclipseLink, Kodo

WebLogic Server JDBC Architecture

JDBC 4.0 - Connection Management

• Additional enhancements to Connection interface to permit improved connection state tracking in pool environments.

• Connection new methods– isValid Check whether the current connection has not been closed and is still valid.– getClientInfo & setClientInfo Get and set current value of client info property supported by the driver. The values

supplied to these methods are used for accounting, diagnostics and debugging purposes only.

• Code example

JDBC 4.0 - Statement Management

• Additional enhancements to Statement interface to get greater flexibility when managing Statement objects in pool environments.

• Statement new methods– isClosed: Check whether this Statement object has been closed. – isPoolable & setPoolable: Get and set poolable state of this statement

object .• Code example

JDBC 4.0 - Wrapper Pattern

• Added the ability to unwrap implementation of JDBC classes to make use of non-standard JDBC methods provided by vendor implementations.

• New interface java.sql.Wrapper– isWrapperFor It can be used to determine if the instance implements the specified interface or

if the instance is a wrapper for an object that does.– unwrap It is used to return an object that implements the specified interface allowing

access to vendor-specific methods.• Subinterfaces derived from Wrapper

– Connection– Statement– PreparedStatement– CallableStatement– ResultSet– DatabaseMetaData– …

JDBC 4.0 - New data types

• Add the data type java.sql.RowId. This allows JDBC programs to access a SQL ROWID.

• Resultset new methods– getRowId– updateRowId

• Add new interface java.sql.SQLXML. Additional APIs have been added to allow applications access to this data.

• Connection new method– createSQLXML

• PreparedStatement new method– setSQLXML

• CallableStatement new methods– getSQLXML– setSQLXML

• ResultSet new methods– getSQLXML– updateSQLXML

TopLink 11g (includes EclipseLink 1.0.1)

Eclipse Persistence Services Project (EclipseLink)

DBWSDBWS

SDOSDO

EISEIS

MOXyMOXy

JPAJPA

XML Data Legacy SystemsDatabases

Java SEJava SE Java EEJava EE OSGiOSGi SpringSpring ADFADF

Scaling with JPA and ActiveCache • A clustered Enterprise Java application can experience slower

response time due to database latency as it scales its number of instances– As the number of concurrent application instance increase the

number of read and write requests that must use the database also increase

– Independent caching in each application node must be minimized where concurrent writing is involved

– Reduced caching comes at a cost of increasing backend database load

App Machines

WebLogic Cluster

Enterprise App

JDBC

Enterprise App

JDBC

Web App

JDBC

Databases

Persistence Persistence Persistence

Scaling with JPA and ActiveCache • TopLink Grid integrates Coherence as shared cache for TopLink

11g JPA between Application Tier and database• Single Coherence cache shared by all application nodes

supports maximum caching even with concurrent writing• Coherence Data Grid caching minimizes database load to allow

for predictable scalability

Named Cache Named Cache Named Cache Named Cache

Cache Store Cache Store Cache Store Cache Store

Named Cache

Cache Store

Oracle Coherence In Memory Data Grid

Database

App Machines

WebLogic Cluster

Enterprise App

JDBC

Enterprise App

JDBC

Web App

JDBCJPA JPA JPA

TopLink Grid

• Combines:– the simplicity of application development using the Java

standard Java Persistence API (JPA) with– the scalability and distributed processing power of Oracle’s

Coherence Data Grid.

TopLink Grid Features

• Simple configuration using annotations that align with standard JPA, e.g.,

@Entity@Customizer(CoherenceReadWriteCustomizer.class)public class Title implements Serializable {

• Ability to choose which entities are stored in the grid versus those stored directly in the backing data store• Support for executing queries against either the Grid

or directly against the data store– JPQL queries translated into Coherence filters– Queries executed in parallel across all members in grid

• Support for all Coherence distribution schemes

TopLink Grid Features (2)

• Support for sequence generation and locking using either the Grid or the backing data store– Optimistic Locking supported against Grid

• Store entities in their object representation– Avoid object building cost when retrieving entity from grid– Once in the grid, all member save on building cost, not just

initial reader

• Leverage the power of the Coherence Data Grid, e.g.,– Parallel query processing– Linear scalability– Change listeners, etc.

TopLink Grid CacheStore

• Coherence backed by TopLink Grid for DB Access

(c) Copyright 2007. Oracle Corporation

TopLink Development Tool Support

• JDeveloper 11g– Advanced JPA Tooling– Native ORM, OXM, and EIS mapping

• Eclipse IDE– EclipseLink JPA support provided by Dali in Eclipse 3.4 (Ganymede)– Oracle Enterprise Pack for Eclipse (OEPE) offers additional support

for building and deploying to WebLogic• Standalone Workbench– TopLink 11g Workbench for using oracle.toplink.* (toplink.jar)• Native ORM, OXM, and EIS mapping

– EclipseLink Workbench (not shipped in 11g) for org.eclipse.persistence.* (eclipselink.jar)• Native ORM, OXM, and EIS mapping