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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228