How Is Driver Class Located in Jdbc4

How is driver class located in JDBC4

Some information about JDBC4 driver loading taken from : http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html

When the method getConnection is called, the DriverManager will
attempt to locate a suitable driver from among the JDBC drivers that
were loaded at initialization and those loaded explicitly using the
same class loader as the current application.

The DriverManager methods getConnection and getDrivers have been
enhanced to support the Java SE Service Provider mechanism (SPM).
According to SPM, a service is defined as a well-known set of
interfaces and abstract classes, and a service provider is a specific
implementation of a service. It also specifies that the service
provider configuration files are stored in the META-INF/services
directory. JDBC 4.0 drivers must include the file
META-INF/services/java.sql.Driver. This file contains the name of the
JDBC driver's implementation of java.sql.Driver. For example, to load
the JDBC driver to connect to a Apache Derby database, the
META-INF/services/java.sql.Driver file would contain the following
entry:

org.apache.derby.jdbc.EmbeddedDriver

Now coming to your question.

My question is how? What if there are multiple drivers in the
classpath?

As a class loader rule, any class found first will be loaded and if it is already loaded then will not be reloaded by the class loader.

How are JDBC drivers loaded without Class.forName() or System Properties?

derby.jar contains file META-INF/services/java.sql.Driver which register org.apache.derby.jdbc.AutoloadedDriver.

This work because DriverManager use ServiceLoader.

The name of the driver class for the datasource is missing (Netbeans+ PostgreSql + Glassfish)

Solved: it was a connection pool issue. Creating a driver one in glassfish admin panel I've got no more problems.

Class.forName(JDBC_DRIVER) no longer needed?

That has nothing to do with that system property. Java6 (and JDBC4) introduced a concept known as "service provider" where implementations of known interface can be detected by the JVM during startup. A driver that is compliant with that will be registered by the DriverManager automatically. That's why Class.forName() is no longer necessary - but only if the driver supports that.

The service registration is initiated if there is a services directory in the driver's jar file inside the META-INF directory. That directory needs to contain a text file with the name of the interface that is implemented in the case of a JDBC driver that is java.sql.Driver containing the implementing class.

generalised code to connect to database engines using jdbc4?

for this connection i had to compile my program code by including the .jar driver in the classpath during compilation.

You should not need to do this. That's actually the whole point of JDBC. You should provide the driver class name (and the connection URL, username and/or password) as a string which can be retrieved from some external configuration (properties?) file. E.g.

String driverClassName = getItFromConfigurationFileSomehow();
Class.forName(driverClassName);
// ...

Even more, since JDBC4 the Class#forName() is not necessary at all. Just put the JDBC driver in the runtime classpath and let the ServiceLoader API do its automatic job of loading the driver.

You only need to rewrite your code to only use java.sql interfaces/classes like java.sql.Connection and so on instead of org.postgresql interfaces/classes so that you do not need the JDBC driver during compile. In other words, you should not have any single line of code which imports/references the JDBC driver specific interfaces/classes.



Related Topics



Leave a reply



Submit