Table of contents
1.
Introduction
2.
JDBC
3.
Why JDBC Came into Existence?
4.
Configuration of Databases
5.
Frequently Asked Questions
5.1.
What three elements makeup JDBC?
5.2.
Which JDBC driver is the fastest?
5.3.
What is JDBC's disadvantage?
5.4.
What does JDBC's purpose are?
5.5.
Why do we use a play framework?
6.
Key Takeaways
Last Updated: Mar 27, 2024
Medium

Configuring the JDBC Connection Pool

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Play's architecture is lightweight, stateless and suitable for the web. Play, based on Akka, gives highly scalable applications predictable and low resource consumption (CPU, memory, threads). HikariCP is in charge of managing the Play JDBC data source.

play

Before connecting the front end, your Java program, and the back end, the database, we should understand what a JDBC is and why it was created. Let's first talk about what JDBC is, and then we'll explain how to use it with a practical example.

JDBC

JDBC, often known as Java Database Connectivity, is an abbreviation. It represents progress for ODBC ( Open Database Connectivity ). JDBC is a common API specification created to transfer data from the front end to the back end. Java classes and Java interfaces make up this API.

JDBC

 It serves as a conduit between your Java program and databases (not the kind we use in Java), creating a connection that allows a programmer to send data from Java code and store it in the database for later use.

Why JDBC Came into Existence?

As was previously said, JDBC is an improvement over ODBC because of the latter's platform dependence. Since C, C++, Python, and Core Java are platform-dependent languages (except Java and a small portion of Python), the ODBC API was created in these languages. Because of this, a database vendor created JDBC, which consists of Java classes and interfaces, to eliminate reliance.

Before establishing a connection between JDBC and the database, let us first configure our databases.

Configuration of Databases

 @*  To configure MySQL*@
db.default.url="mysql://user:password@localhost/database"


 @* To configure PostgreSQL*@
db.default.url="postgres://user:password@localhost/database"

 

If extra tuning parameters are required, they are supported in addition to the standard driver, URL, username, and password configuration attributes. The prototype for configuring all database connections is the play.db.prototype configuration from the Play JDBC reference. conf. You can see the defaults for each configuration parameter here:

play {
  modules {
    enabled += "play.API.db.DBModule"
    enabled += "play.API.db.HikariCPModule"
  }

 

Database configuration: The item's name from which to read the database configuration. Therefore, if set to db, it signifies that the settings for the database named default are found in db.default.

  db {
    config = "db"

   @*
    *The name of default database, used when no database name is explicitly
    *specified.
   *@
    default = "default"


    @* The default connection pool.
     *Valid values are:
     *-default - Use the default connection pool provided by the platform (HikariCP)
     *- hikaricp - Use HikariCP
     *- A FQCN to a class that implements play.api.db.ConnectionPool *@
    pool = "default"


    @* The prototype for database configuration*@
    prototype = {


      @* The connection pool for this database.
       * Valid values are:
       *- default - Delegate to play.db.pool
       *- hikaricp - Use HikariCP
       *- A FQCN to a class that implements play.api.db.ConnectionPool *@
      pool = "default"


      @* The database driver*@
      driver = null


       @*  The database URL*@
      URL = null


      @*  The username*@
      username = null


       @*  The password*@
      password = null


       @*  If nonnull, bind the JNDI name to this data source to the given JNDI name.*@
      jndiName = null


      @* If it should log SQL statements*@
      logSql = false


      @*  HikariCP configuration options*@
      hikaricp {


        @*  The data source class name, if not using a URL*@
        dataSourceClassName = null


        @*  Data source configuration options*@
        dataSource {
        }


        @* Whether autocommit should be used*@
        autoCommit = true


        @*  The connection timeout*@
        connectionTimeout = 30 seconds


        @*  The idle timeout*@
        idle-timeout = 10 minutes


        @*  The max lifetime of a connection*@
        maxLifetime = 30 minutes


        @*  If nonnull, query should be used to test connections*@
        connectionTestQuery = null


        @*  If non-null, sets the minimum number of idle connections to maintain.*@
        minimumIdle = null


        @*  maximum number of connections to make.*@
        maximumPoolSize = 10


         @* If non-null, sets the name of the connection pool.*@
        poolName = null

 

This property controls whether the pool will "fail fast" if the pool cannot be seeded with an initial connection.

 1. Any positive number is the number of milliseconds to attempt to acquire an initial connection; the application thread will be blocked during this period. An exception will be thrown if a connection cannot be acquired before this timeout occurs. This timeout is applied after the connectionTimeout period.

 2. If the value is zero (0), HikariCP will attempt to obtain and validate a connection. If a connection is obtained but fails validation, an exception will be thrown, and the pool not started. However, If a connection cannot be obtained, the pool will start, but later efforts to obtain a connection may fail.

 3. A value less than zero will bypass any initial connection attempt, and the pool will start immediately while trying to obtain connections in the background. Consequently, later efforts to obtain a connection may fail.

        initializationFailTimeout = -1


        @*  Sets whether internal queries should be isolated*@
        isolateInternalQueries = false


        @* Sets whether pool suspension is allowed.*@
        allowPoolSuspension = false


         @* Sets whether connections should be read-only*@
        readOnly = false


         @*  Sets whether mbeans should be registered*@
        registerMbeans = false


         @* If non-null sets, the catalog that should be used on connections*@
        catalog = null


         @* SQL statement will be executed after every new connection creation before adding it to pool*@
        connectionInitSql = null


         @* If non-null sets the transaction isolation level*@
        transaction isolation = null


         @* The validation timeout to use*@
        validationTimeout = 5 seconds


        @* 
        *If non-null sets threshold for the amount of time that a connection has been out of the pool before it is
        *considered to have leaked.
         *@
        leakDetectionThreshold = null
      }
    }
  }
}

 

You can override the prototype settings when you need to specify specific connection pool settings.

Frequently Asked Questions

What three elements makeup JDBC?

The three types of JDBC statements are Statement, PreparedStatement, and CallableStatement.

Which JDBC driver is the fastest?

The quickest driver is the JDBC Net pure Java driver (Type 4), which transforms JDBC calls into vendor-specific protocol calls and communicates directly with the database.

What is JDBC's disadvantage?

The following are drawbacks to using this kind of driver: Due to the JDBC call passing through the bridge, ODBC driver, and native database connectivity interface, performance is decreased. The process is then reversed, and the findings are sent back. Java just only a few features.

What does JDBC's purpose are?

Any tabular data, particularly data kept in a relational database, can be accessed via the Java API known as JDBC. JDBC enables you to create Java programs that control these three types of programming work: Join a data source, such as a database. Send statements to the database for queries and updates.

Why do we use a play framework?

Scala & Java developers may create web applications quickly and easily with Play Framework. Play's architecture is lightweight, stateless and suitable for the web. Play, based on Akka, gives highly scalable applications predictable and low resource consumption (CPU, memory, threads).

Key Takeaways

Let us brief out the article. In this article, we study JDBC, its usage, its necessity, and its basic configuration. That’s the end of the article. I hope you all like it.

You can refer to the 19 best python frameworks and flask introduction to learn more about different frameworks. 

That's the end of the article. I hope you all like this article. 

Happy Learning, Ninjas!

 

Live masterclass