1

As for JDBC4 it is required that the JDBC driver loads automatically. It is made by adding a static block where the driver registers in DriverManager. However when I'm writing such a block in my class, it is executed only when I create the object of this class. Otherwise the code is not run.

My question is: how do the drivers initilize itself, when they are not being created from the application code, however they are registered in DriverManager when I use it for getting db connection?

Szymon Lipiński
  • 27,098
  • 17
  • 75
  • 77
  • The code doesn't matter. There is just a static block, which is not executed if I don't create the class. However this mechanism works in JDBC4, as the drivers register themselves during application start. How can I make exactly the same thing? To have the static block executed during application startup without accessing the class? – Szymon Lipiński Feb 04 '13 at 11:07
  • Show the static initializer block. From your question, my assumption would be that you have coded a instance initializer, not a static initializer, but we need to see the code. – Andreas Fester Feb 04 '13 at 11:07

4 Answers4

2

The paragraph 9.2 of the JDBC4 specification, states that the Driver implementation must register itself to the DriverManager upon class loading, so that when the Driver implementation is loaded the static initializer will automatically register an instance of the driver.

So, simply loading the Driver implementation by (Class.forName("driverClassName")), will register the driver with the DriverManager.

Alternatively, the specification provides a mean to externally specify the drivers to be loaded (and thus registered) by the DriverManager, through the system property jdbc.drivers (see paragraph 9.2.1):

 java -Djdbc.drivers=com.acme.jdbc.AcmeJdbcDriver Test

These methods of registration are available also in old JDBC3 implementations.

JDBC4 introdces a new method of registration, leveraging the Service Provider Mechanism: every compliant driver must provide a jar including the META-INF/services/java.sql.Driver file.

The DriverManager (on DriverManager.getConnection() calls ) uses the java.sql.Driver service provider and loads the classes there specified, thus automatically registering the driver. This removes the need to invoke Class.forName (see paragraph 9.2.1 and paragraph 3.1, first bullet).

Carlo Pellegrini
  • 5,656
  • 40
  • 45
1

In previous versions of JDBC when using a DataSource you will often see a call to Class.forName("driver.class")

In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver.

The documentation further states:

Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

So I assume the classpath is being scanned for any classes implementing the driver interface.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

You can use the ServiceLoader facility of Java to automatically load services.

You need to put a file in the JAR that lists your class, then Java will automatically load it when the service is needed. Check the documentation for ServiceLoader for more details.

The answer to this similar question actually says that this is the way that JDBC uses.

Community
  • 1
  • 1
Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
0

Usually you use Class.forName to initialize the class.

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40