DriverManager class in Java

DriverManager class in Java is a member of the java.sql package which is a part of JDBC API. DriverManager class acts as the primary mediator between our java application and the drivers of the database we want to connect with. The JDBC driver manager is a very important class that defines an object which connects Java applications to a JDBC driver.

Usually, DriverManager is the backbone of the JDBC architecture. It’s very simple and small that is used to provide a means of managing the different types of JDBC drivers for the database which is running on the application. The main responsibility of the JDBC database driver is to load all the drivers found in the system.

Properly as well as to select the most appropriate driver from opening a connection to a database. The DriverManager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected.

The DriverManager class works between the user and the drivers. The task of the DriverManager class is to keep track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. It even keeps track of the driver login time limits and printing of log and tracing messages.

This class is mainly useful for the simple application, the most frequently used method of this class is Drivermanager.getConnection(). We can know by the name of the method that this method establishes a connection to a database.

The DriverManager class maintains the list of Driver classes. Each driver has to be get registered in the DriverManager class by calling the method Drivermanager.registerDriver().

Important Methods of DriverManager class

The DriverManager class has many methods. Some of the commonly used methods are given below.

1) registerDriver(Driver driver):- It registers the driver with the DriverManager class.
2) deregisterDriver(Driver driver):- It drops the driver from the list of drivers registered in the DriverManager class.
3) getConnection(String url):- It tries to establish the connection to a given database URL. This method is used to establish the Connection of single-user database software like ms-access.
4) getConnection(String url, String user, String password):- It tries to establish the connection to a given database URL. It is used to establish the Connection of multiuser database software like Oracle, Sybase.
5) getConnection(String url, Properties props):- It tries to establish the connection to a given database URL and its properties.
6) getDriver(String url):- It attempts to locate the driver by the given string.
7) getDrivers():- It retrieves the enumeration of the drivers which has been registered with the DriverManager class.

See another methods of DriverManager

How JDBC Driver Class Loaded

From JDBC 4.0 version onwards JDBC driver supports autoloading of driver class. One of the great additions in version 4.0 of JDBC, we don’t have to explicitly load the driver by calling Class.forName() method anymore. When our application attempts to connect the database for the first time, DriverManager automatically loads the driver found in the application CLASSPATH.

In CLASSPATH there may be multiple drivers (like JDBC driver of Oracle/MySQL/PostgreSQL). When the getConnection() is called, then based on its protocol of the Driver class (Example:- jdbc:oracle:thin, jdbc:mysql, jdbc:postgresql) the DriverManager will attempt to locate a suitable driver from among the JDBC drivers.

For example, three JDBC drivers:- Oracle JDBC driver, MySQL JDBC driver, and PostgreSQL JDBC driver are configured in the classpath of the application. If URL having "jdbc:oracle:thin" then it will load Oracle JDBC driver. Similarly, if the URL having "jdbc:mysql" then it will load MySQL JDBC driver/connector, and for "jdbc:postgresql" it will load JDBC driver of PostgreSQL database.

The DriverManager methods getConnection() and getDrivers() have been enhanced to support the Java SE Service Provider mechanism (SPM). It specifies that the service provider configuration files are stored in the META-INF/services directory. This file contains the name of the JDBC driver’s implementation of java.sql.Driver.

Unzip the jar file of JDBC driver, and go to META-INF/services directory there you will found a file “java.sql.Driver”. Open this file, the Driver class name is given in this file. So, whenever the JDBC driver is used by our program then the Driver class is loaded into the JVM. Hence, we don’t have to explicitly load the driver by calling Class.forName()

How JDBC Driver is Registered with DriverManager Service?

When Driver class is loaded then the static block of JDBC driver class gets executed. This block contains logic to create a JDBC driver class object and to register that object to the DriverManager service. The static block of oracle.jdbc.driver.OracleDriver class looks as shown below:

private static OracleDriver defaultDriver = null;
static {
    try {
       if (defaultDriver == null) {
          defaultDriver = new oracle.jdbc.OracleDriver();
          DriverManager.registerDriver(defaultDriver);
       }
       // other code
    } catch (SQLException var5) {
       // code to handle SQLException
    } catch (RuntimeException var6) {
       // code to handle SQLException
    }
    // remaining code
}

Almost every JDBC driver class contains static block as shown above. Due to these static blocks, the JDBC driver software is registered.

You can see the source code of the .class file through the decompilation process. Unzip the jar file and decompile the .class file. For example, to see the source code of the OracleDriver class, unzip the jar file and go to the Oracle/JDBC folder and search for OracleDriver.class file. Use an online decompiler to see the source code of the .class file.

DataSource

The JDBC API provides the DataSource interface as an alternative to the DriverManager for establishing the connection. A DataSource object is the representation of the database or the data source in the Java programming language. DataSource object is mostly preferred over the DriverManager for establishing a connection to the database.

DataSource object can be thought of as a factory for making connections to the particular database that the DataSource instance represents. It has a set of properties that identify and describe the real-world data source. The properties include information about the location of the database server, the network protocol used to communicate with the server, the name of the database, and so on.

DataSource object works with JNDI (Java Naming and Directory interface) naming service so the application can use the JNDI API to access the DataSource object.

In short, we can say that the DataSource interface is implemented to provide three kinds of connections.

1) Basic DataSource class:- This class is provided by the driver vendor. It is used for portability and easy maintenance.
2) To provide connection pooling:- It is provided by the application server vendor. It works with the ConnectionPoolDataSource class provided by a driver vendor. Its advantage is portability, easy maintenance, and increased performance.
3) To provide connection transactions:- This class works with an XADataSource class, which is provided by the driver vendor. Its advantages are easy maintenance, portability, and ability to participate in distributed transactions.

Source :- Documentation of DriverManager

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also learn,

Leave a Reply