Stig
Stig

Reputation: 2086

Derby EmbeddedDriver working without Class.forName

The documentation tells us to load JDBC driver like so

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();

https://db.apache.org/derby/papers/DerbyTut/embedded_intro.html

But it works fine without and getting the connection straight away

connection = DriverManager.getConnection("jdbc:derby:" + pathDerby + ";create=true");

Why is that?

Version from the log: Booting Derby version The Apache Software Foundation - Apache Derby - 10.13.1.1 - (1765088)

EDIT:

Actually it is needed if you shut down the Derby engine and want to open it again in the same JWM process (I do this all the time in my integration tests)

After shut down

DriverManager.getConnection("jdbc:derby:;shutdown=true");

You should reopen like this

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
connection = DriverManager.getConnection("jdbc:derby:" + pathDerby + ";create=true");

Upvotes: 2

Views: 551

Answers (1)

Maxim
Maxim

Reputation: 9961

From official documentation:

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

my.sql.Driver

Applications no longer need to explicitly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

Upvotes: 2

Related Questions