Book Image

Play Framework Cookbook

By : Alexander Reelsen
Book Image

Play Framework Cookbook

By: Alexander Reelsen

Overview of this book

<p>The Play framework is the new kid on the block of Java frameworks. By breaking with existing standards the play framework tries not to abstract away from HTTP as most web frameworks do, but tightly integrates with it. This means quite a shift for Java programmers. Understanding these concepts behind the play framework and its impact on web development with Java are crucial for fast development of applications.<br /><br />The Play Framework Cookbook starts where the beginner documentation ends. It shows you how to utilize advanced features of the Play framework &ndash; piece by piece and completely outlined with working applications!<br /><br />The reader will be taken through all layers of the Play Framework and provided with in-depth knowledge from as many examples and applications as possible. Leveraging the most from the Play framework means to think simple again in a java environment. Implement your own renderers, integrate tightly with HTTP, use existing code, improve site performance with caching and integrate with other web services and interfaces. Learn about non-functional issues like modularity or integration into production and testing environments. In order to provide the best learning experience during reading Play Framework Cookbook, almost every example is provided with source code, so you can start immediately to integrate recipes into your own play applications.</p>
Table of Contents (16 chapters)
Play Framework Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Further Information About the Play Framework
Index

Using Oracle or other databases with Play


This is just a quick recipe to show that any database with a JDBC driver can be used as persistence storage in Play, though it has been mainly developed with MySQL in mind. The most simple configuration of a database is to use the in memory H2 database by specifying db=mem in the application.conf file. You can ensure persistence by specifying db=fs, which also uses the H2 database. Both of these options are suitable for development mode as well as automated test running. However, in other cases you might want to use a real SQL database like MySQL or PostgreSQL.

How to do it...

Just add driver-specific configuration in your configuration file. In order to support PostgreSQL, this is the way:

db.url=jdbc:postgresql:accounting_db
db.driver=org.postgresql.Driver

db.user=acct
db.pass=Bdgc54S

Oracle can also be configured without problems:

db.url=jdbc:oracle:thin:@db01.your.host:1521:tst-db01

db.driver=oracle.jdbc.driver.OracleDriver

How it works...

As the JDBC mechanism already provides a generic way to unify the access to arbitrary databases, the complexity to configure different database is generally pretty low in Java. Play supports this by only needing to configure the db.url and db.driver configuration variables to have support for most databases, which provide a JDBC driver.

There's more...

You can even go further and configure your connection pool (if the JDBC driver has support for that), reusing application server resources, or check whether changing your JPA dialect is also needed when changing your default database.

Using application server datasources

It is also possible to use datasources provided by the underlying application server, just put the following line in your config file:

db=java:/comp/env/jdbc/myDatasource

Using connection pools

Connection pools are a very important feature to ensure a performant and resource saving link to the database from your application. This means saving resources by not creating a new TCP connection every time you issue a query. Most JDBC drivers come with this out of the box, but you can also tweak the settings in your config file:

# db.pool.timeout=1000
# db.pool.maxSize=30
# db.pool.minSize=10

Configuring your JPA dialect

It might also be necessary to configure your JPA dialect for certain databases. As Play uses hibernate, you need to specify a hibernate dialect:

jpa.dialect=org.hibernate.dialect.Oracle10gDialect

For more information about dialects, check out http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional-dialects.