Rollin onj Rubyv3


Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • The Java version is tightly integrated with Java to allow both to script any Java class and to embed the interpreter into any Java application.
  • Integration with Java libraries JRuby classes can extend Java classes implement a single Java interface add methods to existing Java classes (visible from JRuby, but not Java) Native threads JRuby uses native threads, Ruby uses green threads can result in different behaviors between JRuby and Ruby Portability JRuby runs on any machine with a JVM Unicode support JRuby uses the unicode support in Java Ruby has some support for unicode, but it’s not built-in
  • When you start JRuby  starting Java – based ruby interpreter on a JVM. C based or J based Ruby doesn’t care which one (due to how Jruby is implemeted) Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call. • An abstract interface on top of HTTP allows for • code reuse • easier testing • new ways of combining code/applications
  • The general architecture for Ruby DBI uses two layers: The database interface (DBI) layer. This layer is database independent and provides a set of common access methods that are used the same way regardless of the type of database server with which you're communicating. The database driver (DBD) layer. This layer is database dependent; different drivers provide access to different database engines. There is one driver for MySQL, another for PostgreSQL, another for InterBase, another for Oracle, and so forth. Each driver interprets requests from the DBI layer and maps them onto requests appropriate for a given type of database server. ADO (ActiveX Data Objects) DB2 Frontbase mSQL MySQL ODBC Oracle OCI8 (Oracle) PostgreSQL Proxy/Server SQLite SQLRelay The database drivers are each hand crafted to access a specific database. They have different method names from each other, meaning that if you switch back end databases, you'll need to rewrite substantial portions of your program. The only reason to use the database specific drivers is to take advantage of database specific features, and possibly to make the program run a little faster and use a little less memory. With the database specific drivers, you manipulate the database by passing SQL statements as strings via the interface methods. Ruby-DBI is a wrapper around all the database drivers, giving you a united method set to do the majority of your database activities. The only remaining database dependencies are caused by the fact that the different databases support slightly different SQL syntaxes. With DBI, you manipulate the database by passing SQL statements as strings via the interface methods. Whereas the database drivers and DBI provide a simple vehicle to use an SQL statement, the ActiveRecord technique provides database access without the need for SQL statements. The ActiveRecord techniques match objects to database rows. The advantages are almost zero database dependencies within the subset of capabilities provided by ActiveRecord, and a set of defaults (such as automatic naming of tables based on class names) that make coding a database app much quicker and easier. If you know SQL, you can pretty much write an app using database drivers or DBI today. With ActiveRecord, you'll need to learn a new set of skills, complete with a set of methods specific to Ruby's ActiveRecord class. You wouldn't do this to create a one-off app, but if you are or plan to be a frequent Ruby programmer and you plan to frequently hit databases from your Ruby programs, learning ActiveRecord will benefit you greatly.  Ruby on Rails uses ActiveRecord for the vast majority of its database access, so if you use or plan to use Ruby on Rails, ActiveRecord is a must.
  • Database.yml – only config file.. Just fill in the value and go! Here you've only scratched the surface of Active Record. It's a powerful framework—much more powerful than other frameworks that implement the Active Record design pattern. It leverages the strengths of the Ruby framework—a simple syntax, metaprogramming, and the ability to open and manipulate classes at run time—to form a marvelously effective and productive environment for green-field development. But Active Record is not for every application. It does not handle legacy schemas nearly as well. For those, you need a mapping framework, not one that merely wraps tables with simple classes. Active Record also is prone to performance problems for those who don't have the discipline or skill to tune it properly. Active Record, and the Ruby on Rails framework, represent what founder David Heinemeier-Hannsen calls opinionated software. Rails, and Active Record by extension, represents hundreds of compromises, with most of them favoring a beautiful, productive programmer experience.
  • Solve WAR file,
  • Oracle TopLink 11g includes EclipseLink 1.0.1 JPA: Object-Relational JPA 1.0 with advanced persistence Simplified configuration of advanced features using annotations and/or XML All leading RDMS with platform specific features, best ORM for the Oracle Database MOXy: Object-XML Binding JAXB and advanced native SDO: Service Data Objects EIS using JCA Resource Adapters Containers WebLogic, OracleAS, WebSphere, GlassFish/SunAS, JBoss
  • This is an example of a scalability problem that can be encountered in JEE applications, and that Coherence can help address.
  • Oracle TopLink 11g ( introduced in 2008 includes the new TopLink Grid feature that addresses the potential scalability problem discussed on the prior slide. TopLinkGrid which offers tight integration with Oracle Coherence that marries the standardization and simplicity of application development using the Java Persistence API (JPA) with the scalability and distributed processing power of Oracle’s Coherence Data Grid. This feature is integrated and included with WebLogic Suite. Developers can use TopLink Grid to easily leverage the Coherence grid as their primary storage for Entities as well as using direct connectivity with the backing data store for some Entity types of for specific operations. This flexibility allows application to easily adjust their usage of Coherence to meet changing requirements and usage patterns. The real focus of TopLink Grid is the delivery of functionality where a developer can easily configure TopLink JPA to use Coherence as the primary data store, execute queries against the grid, and choose to have Coherence manage the persisting of new and modified Entities. This integrated solution involves a layer between JPA and the data store where the grid can be leveraged in order to off-load direct database calls from every application instance and therefore offer these clustered application deployments the capability of scaling beyond and database bound operations.
  • Rollin onj Rubyv3

    1. 2. Enterprise Ruby on Rails: Rolling with JRuby on Oracle WebLogic Suite Chris Bucchere CEO of BDG Pieter Humphrey Principal Product Manager, Oracle
    2. 3. 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.
    3. 4. Intro Slides <ul><li>Chris Bucchere </li></ul><ul><ul><li>CEO, BDG </li></ul></ul><ul><ul><li>Oracle ACE Director </li></ul></ul><ul><li>Pieter Humphrey </li></ul><ul><ul><li>Principal Product Director, Oracle </li></ul></ul>
    4. 5. Ruby 101
    5. 6. Ruby Overview <ul><li>Key Features </li></ul><ul><ul><li>Purely object-oriented </li></ul></ul><ul><ul><li>Dynamically-typed </li></ul></ul><ul><ul><li>Compact, yet easy to read syntax </li></ul></ul><ul><ul><li>Blocks that are closures </li></ul></ul><ul><ul><li>Open classes and objects </li></ul></ul><ul><ul><li>Domain Specific Language capabilities </li></ul></ul>Strong Weak static dynamic
    6. 7. Basics
    7. 8. Iteration, Blocks & Closures
    8. 9. String Handling & Regular Expressions
    9. 11. Guiding Principles <ul><li>Duck Typing </li></ul><ul><ul><li>If it walks like a duck and quacks like a duck, then it probably is a duck </li></ul></ul><ul><li>Convention over Configuration </li></ul><ul><ul><li>Down with XML! </li></ul></ul><ul><li>Rapid Prototyping </li></ul><ul><ul><li>Allows for agile development with short iterations </li></ul></ul><ul><li>Sustainable Productivity </li></ul>
    10. 12. Example Applications <ul><li>Twitter </li></ul><ul><li>43Things </li></ul><ul><li>37 Signals </li></ul><ul><ul><li>Basecamp </li></ul></ul><ul><ul><li>Campfire </li></ul></ul><ul><ul><li>Backpack </li></ul></ul><ul><ul><li>Highrise </li></ul></ul><ul><li>Social Collective, Inc. </li></ul><ul><ul><li>The Social Collective </li></ul></ul><ul><ul><li>CrowdCampaign </li></ul></ul><ul><li>Thousands of others. . . . </li></ul>
    11. 13. Migrations
    12. 14. ActiveRecord & Validations
    13. 15. Scaffolding
    14. 16. Built-in MVC
    15. 17. UI Helpers
    16. 18. Show me the money!
    17. 19. JRuby and Database Access
    18. 20. What is JRuby? <ul><li>JRuby is a port of Ruby onto Java, written in C & ruby </li></ul><ul><li>Offers Native Multithreading </li></ul><ul><li>Full Access to Java Libraries </li></ul>Configuration DSL External Systems Static Layer - Static typed legacy Java Libraries Dynamic layer - Application Code
    19. 21. Why JRuby? <ul><li>To use Java libraries from code written in Ruby syntax </li></ul><ul><ul><li>Swing, etc </li></ul></ul><ul><li>To use Ruby libraries from code written in Java syntax </li></ul><ul><ul><li>Via ActiveRecord with Bean Scripting Framework (BSF) </li></ul></ul><ul><ul><li>Via “Scripting for the Java Platform” (JSR 223) in Java 6 </li></ul></ul><ul><li>Performance vs. C-based interpreter is a hot debate </li></ul><ul><ul><li>Depends who you ask, but it seems performance is surprisingly good </li></ul></ul><ul><li>Great for the following use cases </li></ul><ul><ul><li>Agile Methodology shops, small teams </li></ul></ul><ul><ul><li>REST / Web App for routing data to back end resources </li></ul></ul><ul><ul><li>Enterprise Deployments </li></ul></ul>
    20. 22. Why JRuby cont. <ul><li>Integration with Java libraries: </li></ul><ul><li>JRuby classes can </li></ul><ul><ul><ul><li>extend Java classes </li></ul></ul></ul><ul><ul><ul><li>implement a single Java interface </li></ul></ul></ul><ul><ul><ul><li>add methods to existing Java classes </li></ul></ul></ul><ul><li>Native threads </li></ul><ul><li>Unicode support </li></ul><ul><li>Portability </li></ul><ul><li>Can drop in into SQL easily ( find_by_sql ) </li></ul><ul><li>Many DB accees options – this is where Ruby shines </li></ul>
    21. 23. JRuby Developer Tool support <ul><li>IDEs </li></ul><ul><ul><li>Eclipse </li></ul></ul><ul><ul><li>RDT plug-in supports Ruby development </li></ul></ul><ul><ul><li>RadRails IDE </li></ul></ul><ul><ul><li>IntelliJ IDEA </li></ul></ul><ul><ul><li>NetBeans </li></ul></ul><ul><li>Editors </li></ul><ul><ul><li>syntax highlighting (emacs, jEdit and Vim) </li></ul></ul><ul><li>Spring </li></ul><ul><ul><li>an IOC + Java EE framework </li></ul></ul><ul><ul><li>supports beans implemented in Java, JRuby, Groovy </li></ul></ul>
    22. 24. Rack <ul><li>Ruby’s CGI / Servlet equivalent </li></ul><ul><ul><li>In web.xml, a rack servlet filter will dispatch params / requests to the RACK component </li></ul></ul><ul><li>Rack provides a single API to abstracts multiple webservers ( mongrel, fast-cgi, webrick, fusion passenger ) </li></ul><ul><li>Standardizes the request that a Ruby app expects (get params), wrapping HTTP requests/responses </li></ul><ul><li>Based on WSGI for Python </li></ul>
    23. 25. Ruby DBI <Insert Picture Here> Ruby Script DBI API ADO ODBC Databases DB2, Informix, mySQL, Oracle
    24. 26. ActiveRecord <ul><li>ActiveRecord is … </li></ul><ul><ul><li>a Ruby library for accessing relational databases </li></ul></ul><ul><ul><li>Simple, yet amazingly powerful / portable DB access </li></ul></ul><ul><ul><li>Tricky to tune under load, not for use with legacy schemas </li></ul></ul><ul><li>It can be used from Java through JRuby </li></ul><ul><ul><li>install the ActiveRecord gem </li></ul></ul><ul><ul><li>under Java 5 and earlier </li></ul></ul><ul><ul><ul><li>use Bean Scripting Framework (BSF) </li></ul></ul></ul><ul><ul><ul><ul><li>classpath must contain bsf.jar, jruby.jar, jvyaml.jar, commons-logging-1.1.jar </li></ul></ul></ul></ul><ul><ul><li>under Java 6 and later </li></ul></ul><ul><ul><ul><li>use JSR 223 Scripting API </li></ul></ul></ul><ul><li>Convention, not configuration </li></ul><ul><li>Great for greenfield web apps </li></ul>
    25. 27. Getting Started with JRuby <ul><li>Getting JRuby </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li>jRuby1.3.1/bin should be in PATH </li></ul></ul><ul><ul><li>JDK 1.5 or more should be in PATH, JAVA_HOME set </li></ul></ul><ul><ul><li>Apache Ant ( 1.7 or greater </li></ul></ul><ul><li>Getting Rails (the web tier for Ruby) </li></ul><ul><ul><li>Once JRuby environment is set: </li></ul></ul><ul><ul><li>'gem install rails -v 2.2.2' </li></ul></ul><ul><li>Getting Warbler (Java EE deployment unit converter) </li></ul><ul><ul><li>'gem install warbler -v 0.9.12‘ </li></ul></ul><ul><ul><li>Make sure you aren’t behind a proxy server </li></ul></ul>
    26. 28. Demo Application: Maintain FOD Supplier Catalog FOD SCHEMA WebLogic Server EclipseLink Run WARBLER Ruby App JRuby interpreter Ruby App
    27. 29. JRuby, WebLogic, TopLink Database Access
    28. 30. JPA—in a Nutshell <ul><li>Concrete classes (POJOs) </li></ul><ul><li>No required interfaces or inheritance </li></ul><ul><li>new() for instance creation </li></ul><ul><li>Mapping using annotations and/or XML </li></ul><ul><li>“ Managed” by an EntityManager </li></ul><ul><li>Can leave the Container (“detached”) </li></ul>
    29. 31. Where does JPA fit? Relational Database Java SE/EE/OSGi JPA Persistence Provider Design Time Runtime Mapping Metadata Java Classes Database Schema
    30. 32. JRuby + JPA = Enterprise friendly Rails <ul><li>JPA is better for working with pre-existing database schemas </li></ul><ul><li>JRuby/JPA/WebLogic = easy Ops compliance </li></ul><ul><li>Development tools: Standalone, JDev, Eclipse </li></ul><ul><li>WebLogic provides connection pooling </li></ul><ul><li>WebLogic provides Oracle RAC integration </li></ul><ul><li>WebLogic provides Oracle JPA / Coherence integration </li></ul><ul><li>WebLogic provides XA with Messaging & DB </li></ul><ul><li>WebLogic provides easy ways to expose as JAX-WS, JAX-RPC, Spring Beans </li></ul><ul><li>WebLogic provides SUN RI: EclipseLink, Kodo </li></ul><Insert Picture Here>
    31. 33. WebLogic Server JDBC Architecture
    32. 35. JDBC 4.0 - Connection Management <ul><li>Additional enhancements to Connection interface to permit improved connection state tracking in pool environments. </li></ul><ul><li>Connection new methods </li></ul><ul><ul><li>isValid </li></ul></ul><ul><ul><li>Check whether the current connection has not been closed and is still valid. </li></ul></ul><ul><ul><li>getClientInfo & setClientInfo </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><li>Code example </li></ul>
    33. 36. JDBC 4.0 - Statement Management <ul><li>Additional enhancements to Statement interface to get greater flexibility when managing Statement objects in pool environments. </li></ul><ul><li>Statement new methods </li></ul><ul><ul><li>isClosed: Check whether this Statement object has been closed. </li></ul></ul><ul><ul><li>isPoolable & setPoolable: Get and set poolable state of this statement object . </li></ul></ul><ul><li>Code example </li></ul>
    34. 37. JDBC 4.0 - Wrapper Pattern <ul><li>Added the ability to unwrap implementation of JDBC classes to make use of non-standard JDBC methods provided by vendor implementations. </li></ul><ul><li>New interface java.sql.Wrapper </li></ul><ul><ul><li>isWrapperFor </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>unwrap </li></ul></ul><ul><ul><li>It is used to return an object that implements the specified interface allowing access to vendor-specific methods. </li></ul></ul><ul><li>Subinterfaces derived from Wrapper </li></ul><ul><ul><li>Connection </li></ul></ul><ul><ul><li>Statement </li></ul></ul><ul><ul><li>PreparedStatement </li></ul></ul><ul><ul><li>CallableStatement </li></ul></ul><ul><ul><li>ResultSet </li></ul></ul><ul><ul><li>DatabaseMetaData </li></ul></ul><ul><ul><li>… </li></ul></ul>
    35. 38. JDBC 4.0 - New data types <ul><li>Add the data type java.sql.RowId. This allows JDBC programs to access a SQL ROWID. </li></ul><ul><li>Resultset new methods </li></ul><ul><ul><li>getRowId </li></ul></ul><ul><ul><li>updateRowId </li></ul></ul><ul><li>Add new interface java.sql.SQLXML. Additional APIs have been added to allow applications access to this data. </li></ul><ul><li>Connection new method </li></ul><ul><ul><li>createSQLXML </li></ul></ul><ul><li>PreparedStatement new method </li></ul><ul><ul><li>setSQLXML </li></ul></ul><ul><li>CallableStatement new methods </li></ul><ul><ul><li>getSQLXML </li></ul></ul><ul><ul><li>setSQLXML </li></ul></ul><ul><li>ResultSet new methods </li></ul><ul><ul><li>getSQLXML </li></ul></ul><ul><ul><li>updateSQLXML </li></ul></ul>
    36. 39. TopLink 11g (includes EclipseLink 1.0.1) Eclipse Persistence Services Project (EclipseLink)‏ DBWS SDO EIS MOXy JPA XML Data Legacy Systems Databases Java SE Java EE OSGi Spring ADF
    37. 40. Scaling with JPA and ActiveCache <ul><li>A clustered Enterprise Java application can experience slower response time due to database latency as it scales its number of instances </li></ul><ul><ul><li>As the number of concurrent application instance increase the number of read and write requests that must use the database also increase </li></ul></ul><ul><ul><li>Independent caching in each application node must be minimized where concurrent writing is involved </li></ul></ul><ul><ul><li>Reduced caching comes at a cost of increasing backend database load </li></ul></ul>App Machines WebLogic Cluster Enterprise App JDBC Enterprise App JDBC Web App JDBC Databases Persistence Persistence Persistence
    38. 41. Scaling with JPA and ActiveCache <ul><li>TopLink Grid integrates Coherence as shared cache for TopLink 11g JPA between Application Tier and database </li></ul><ul><li>Single Coherence cache shared by all application nodes supports maximum caching even with concurrent writing </li></ul><ul><li>Coherence Data Grid caching minimizes database load to allow for predictable scalability </li></ul>Named Cache Named Cache Named Cache Cache Store Cache Store Cache Store Cache Store Cache Store Oracle Coherence In Memory Data Grid Database App Machines WebLogic Cluster Enterprise App JDBC Enterprise App JDBC Web App JDBC JPA JPA JPA Named Cache Named Cache
    39. 42. TopLink Grid <ul><li>Combines: </li></ul><ul><ul><li>the simplicity of application development using the Java standard Java Persistence API (JPA) with </li></ul></ul><ul><ul><li>the scalability and distributed processing power of Oracle’s Coherence Data Grid. </li></ul></ul>
    40. 43. TopLink Grid Features <ul><li>Simple configuration using annotations that align with standard JPA, e.g., </li></ul><ul><ul><ul><li>@Entity </li></ul></ul></ul><ul><ul><ul><li>@Customizer (CoherenceReadWriteCustomizer. class ) </li></ul></ul></ul><ul><ul><ul><li>public class Title implements Serializable { </li></ul></ul></ul><ul><li>Ability to choose which entities are stored in the grid versus those stored directly in the backing data store </li></ul><ul><li>Support for executing queries against either the Grid or directly against the data store </li></ul><ul><ul><li>JPQL queries translated into Coherence filters </li></ul></ul><ul><ul><li>Queries executed in parallel across all members in grid </li></ul></ul><ul><li>Support for all Coherence distribution schemes </li></ul>
    41. 44. TopLink Grid Features (2) <ul><li>Support for sequence generation and locking using either the Grid or the backing data store </li></ul><ul><ul><li>Optimistic Locking supported against Grid </li></ul></ul><ul><li>Store entities in their object representation </li></ul><ul><ul><li>Avoid object building cost when retrieving entity from grid </li></ul></ul><ul><ul><li>Once in the grid, all member save on building cost, not just initial reader </li></ul></ul><ul><li>Leverage the power of the Coherence Data Grid, e.g., </li></ul><ul><ul><li>Parallel query processing </li></ul></ul><ul><ul><li>Linear scalability </li></ul></ul><ul><ul><li>Change listeners, etc. </li></ul></ul>
    42. 45. TopLink Grid CacheStore <ul><li>Coherence backed by TopLink Grid for DB Access </li></ul>(c) Copyright 2007. Oracle Corporation
    43. 46. TopLink Development Tool Support <ul><li>JDeveloper 11g </li></ul><ul><ul><li>Advanced JPA Tooling </li></ul></ul><ul><ul><li>Native ORM, OXM, and EIS mapping </li></ul></ul><ul><li>Eclipse IDE </li></ul><ul><ul><li>EclipseLink JPA support provided by Dali in Eclipse 3.4 (Ganymede) </li></ul></ul><ul><ul><li>Oracle Enterprise Pack for Eclipse (OEPE) offers additional support for building and deploying to WebLogic </li></ul></ul><ul><li>Standalone Workbench </li></ul><ul><ul><li>TopLink 11g Workbench for using oracle.toplink.* (toplink.jar) </li></ul></ul><ul><ul><ul><li>Native ORM, OXM, and EIS mapping </li></ul></ul></ul><ul><ul><li>EclipseLink Workbench (not shipped in 11g) for org.eclipse.persistence.* (eclipselink.jar) </li></ul></ul><ul><ul><ul><li>Native ORM, OXM, and EIS mapping </li></ul></ul></ul>