Play Framework – Accessing an SQL database

Configuring JDBC Connection pools 

Play gives a plug-in to overseeing JDBC association pools. You can design the same number of databases as you need.

To empower the database plug-in include javaJdbc in your fabricate conditions:

libraryDependencies += javaJdbc

At that point you must design an association pool in the conf/application.conf document. By tradition the default JDBC data source must be called default:

 

# Default database designdb.default.driver=org.h2.Driverdb.default.url="jdbc:h2:mem:play"

To design a few data sources:

# Orders database
db.orders.driver=org.h2.Driver
db.orders.url="jdbc:h2:mem:orders"
 

# Customers database
db.customers.driver=org.h2.Driver
db.customers.url="jdbc:h2:mem:customers"

If something isn’t legitimately designed, you will be notified specifically in your program:

# Default database arrangement utilizing H2 database engine as a part of an in-memory mode
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
# Default database arrangement utilizing H2 database engine as a part of a tenacious modedb.default.driver=org.h2.Driver
db.default.url="jdbc:h2:/way/to/db-document"

SQLite database engine association propertiesThe points of interest of the H2 database URL are found from H2 Database Engine.

# Default database design utilizing SQLite database engine db.default.driver=org.sqlite.JDBC
db.default.url="jdbc:sqlite:/way/to/db-document"

# Default database setup utilizing Mysql database engine MySQL Database engine association properties:

# Connect to playdb as playdbuser
db.default.driver=com.mysql.jdbc.driver
db.default.url="jdbc:mysql://localhost/playdb"
db.default.user=playdbuser
db.default.password="a solid secret key"

Step by step instructions to see SQL Statement in the reassure?

db.default.logstatements=true
logger.com.jolbox=DEBUG// for EBean


The play.db bundle gives access to the designed data sources:
Getting to the JDBC DataSource

import play.db.*;DataSource ds = Db.getDataSource();

You can recover a JDBC association the same way:Acquiring a JDBC association

Association = Db.getConnection();


Uncovering the DataSource through JNDI:
It is critical to note that ensuing Connections are not consequently arranged at the end of the solicitation cycle. At the end of the day, you are in charge of calling their nearby() technique some place in your code with the goal that they might be quickly come back to the pool.

A few libraries hope to recover the DataSource reference from JNDI. You can uncover any Play oversaw DataSource through JDNI by including this setup in conf/application.conf structure

db.default.driver=org.h2.driver
db.default.url="jdbc:h2:mem:play"
db.default.jndiname=Defaultds

Important a Database Driver:

Other than for the h2 in-memory database, helpful basically being developed mode, Play does not give any database drivers. Thusly, to convey in creation you will need to include your database driver as application reliance.

For example, if you utilize Mysql5, you have to include reliance for the connector:

librarydependencies += "mysql" % "mysql-connector-java" % "5.1"

To empower it, add javaebean to your conditionsDesigning Ebean

librarydependencies += javaebean

ebean.default=”models.*”at that point add the accompanying line to conf/application.conf structure

ebean.orders=”models.order,models.orderitem”ebean.customers=”models.customer,models.address”That is characterizes a default Ebean, utilizing the default database, which precondition be legitimately designed.  Likewise reverse name of the default Ebean server by arranging. DataSource.default property.

This example, we have admittance to two Ebean servers – each one utilizing it database.

Every ebean. config line (as above) can outline classes that Ebean may be intrigued by enlisting (eg. @entity/Model classes, @embeddables, custom Scalartypes and Compoundtypes, Beanpersistcontrollers, Beanpersistlisteners, Beanfinders, Serverconfigstartups, and so forth). These could be exclusively recorded differentiated by commas, and/or you can utilize the trump card .*. Case in point, models.* registers with Ebean all classes inside the models bundle that Ebean can make utilization of.

To modify the underlying Ebean Server setup, you can either include a conf/ebean.properties document, or make an occasion of the Serverconfigstartup interface to automatically control the Ebean Serverconfig before the server is initialized.

As a sample, the decently basic issue of lessening the Sequence Batch Size to minimize succession crevices could be comprehended just with a class like this:

Package models;
import com.avaje.ebean.config.serverconfig;
import com.avaje.ebean.event.serverconfigstartup;
public class MyServerConfigStartup executes Serverconfigstartup {

@override

public void onstart( serverconfig) {
serverconfig.setdatabasesequencebatchsize(1);
}
}

Note that Ebean will likewise make utilization of a conf/orm.xml record (if present), to design <entity-mappings>.