opensas edited this page Apr 3, 2012 · 2 revisions
Clone this wiki locally

Accessing an SQL database

Esta página todavía no ha sido traducida al castellano. Puedes ayudarnos con la tarea simplemente presionando el botón Edit Page. Para más información puedes leer esta guía para el traductor. Aquí puedes ver cuánto nos falta para terminar la traducción.

Configuring JDBC connection pools

Play 2.0 provides a plugin for managing JDBC connection pools. You can configure as many databases you need.

To enable the database plugin, configure a connection pool in the conf/application.conf file. By convention the default JDBC data source must be called default:

# Default database configuration

To configure several data sources:

# Orders database

# Customers database

If something isn’t properly configured, you will be notified directly in your browser:

Accessing the JDBC datasource

The play.db package provides access to the configured data sources:

import play.db.*;

DataSource ds = DB.getDatasource();

Obtaining a JDBC connection

You can retrieve a JDBC connection the same way:

Connection connection = DB.getConnection();

Exposing the datasource through JNDI

Some libraries expect to retrieve the Datasource reference from JNDI. You can expose any Play managed datasource via JDNI by adding this configuration in conf/application.conf:


Importing a Database Driver

Other than for the h2 in-memory database, useful mostly in development mode, Play 2.0 does not provide any database drivers. Consequently, to deploy in production you will have to add your database driver as an application dependency.

For example, if you use MySQL5, you need to add a dependency for the connector:

val appDependencies = Seq(
     // Add your project dependencies here,
     "mysql" % "mysql-connector-java" % "5.1.18"

Next: Using Ebean to access your database