Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Play is a very important framework in today’s world. Based on a stateless, lightweight, web-friendly architecture. Play is built on Akka, and it has minimal resource consumption. It is a developer-friendly framework and very versatile. Most of the Java libraries are also supported in Play.
JDBC is the abbreviation for Java Database Connectivity. JNDI is the abbreviation for Java Naming and Directory Interface API. In the JNDI directory, we actually store a JDBC DataSource, so we simply use JDBC to obtain a connection via JNDI lookup.
Since databases are quite frequently used, we must understand how to configure and connect our code with the database of our choosing.
Today we’ll learn about accessing the SQL database in Play.
Configuring JDBC Connection Pools
A plugin for managing JDBC connection pools is available from Play. We can set up as many databases as we require.
Add the following build dependency for enabling the database plugin:
libraryDependencies ++= Seq(
javaJdbc
)
Configuring The JDBC Driver Dependency
Database drivers are not provided by Play. As a result, in order to deploy in production, you must include your database driver as an application requirement.
For example, if we use MySQL5, we must add the following requirement for the connector:
There is a need to configure the connection pool in the conf/application.conf file. The default JDBC data source is called default by convention. It’s corresponding configuration properties are db.default.driver and db.default.url.
# Default-database configuration using H2 database engine in in-memory mode
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
In file database.
# Default-database configuration using H2 database engine in persistent mode
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:/path/to/db-file"
SQLite Database Engine Connection Properties
Following is the configuration using SQLite database engine
# Default database configuration using SQLite database engine
db.default.driver=org.sqlite.JDBC
db.default.url="jdbc:sqlite:/path/to/db-file"
PostgreSQL Database Engine Connection Properties
Following is the configuration using the PostgreSQL database engine
# Default database configuration using PostgreSQL database engine
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://database.example.com/playdb"
MySQL Database Engine Connection Properties
Following is the configuration using the MySQL database engine
# Default database configuration using MySQL database engine
# Connect to playdb as playdbuser
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/playdb"
db.default.username=playdbuser
db.default.password="a strong password"
Exposing The Datasource Through JNDI
Some libraries anticipate getting the JNDI Datasource reference. By including the following settings in conf/application.conf, we may expose any Play controlled datasource using JDNI:
Not all connection pools provide an option to log SQL statements right out of the box. HikariCP advises using your database vendor's log capacity in each individual case. HikariCP documentation.
In order to ensure consistent SQL log statement support for supported pools, Play uses jdbcdslog-exp. By utilising the logSql property, the database may configure the SQL log statement:
Then we configure jdbcdslog-exp. Basically, we need to set the root logger to INFO and then choose the jdbcdslog-exp logs that we want to use (connections, statements and result sets). Here is an illustration of how to setup the logs using logback.xml:
It is essential to remember that connections created as a consequence are not immediately destroyed at the conclusion of the request cycle. This means that in order for them to be instantly returned to the pool, you must use their close() function somewhere in your code.
Configuring The Connection Pool
HikariCP is the default database connection pool solution used by Play out of the box. Additionally, we may utilise our own pool by using the fully qualified class name of play.api.db.ConnectionPool:
play.db.pool=your.own.ConnectionPool
Frequently Asked Questions
Define the Play framework.
Play is a very important framework in today’s world. Based on a stateless, lightweight, web-friendly architecture. Play is built on Akka, and it has minimal resource consumption. It is a developer-friendly framework and very versatile. Most of the Java libraries are also supported in Play.
Define JDBC.
JDBC is the abbreviation for Java Database Connectivity. It specifies how a client can access a database. It is a Java-based data access method used for connecting Java databases.
Define JNDI.
JNDI is the abbreviation for Java Naming and Directory Interface API. In the JNDI directory, we actually store a JDBC DataSource, so we simply use JDBC to obtain a connection via JNDI lookup.
Which is the default database connection used by Play?
HikariCP is the default database connection pool solution used by Play.
What is the use of withConnection?
When accessing the database when we use withConnection in either scenario, the connection will be immediately terminated at the conclusion of the block.
Conclusion
In this article, we extensively discussed accessing an SQL database Play. We started with the introduction of the Play, JDBC and JDNI. Then we saw the default database configuration. Then we learned about the engine connection of the H2 database. Then we saw accessing and obtaining a connection with JDBC. In the end, we saw the frequently asked questions.