了解Java的服务提供程序机制,该机制可自动加载Jdbc驱动程序 [英] Understanding Java's Service Provider Mechanism that automatically loads the Jdbc driver

查看:102
本文介绍了了解Java的服务提供程序机制,该机制可自动加载Jdbc驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图准确地了解Java的服务提供者机制如何工作以找到适当的JDBC驱动程序.这是我到目前为止的内容:

I am trying to get an accurate understanding of how Java's Service Provider Mechanism works to locate the appropriate JDBC driver. Here is what I have so far:

由于Class.ForName不再用于显式加载JDBC Driver,因此Java将从传递给getConnection方法的数据库URL字符串中知道它需要哪种类型的驱动程序.例如,连接到oracle数据库的数据库URL如下所示:

Since Class.ForName is no longer used to explicitly load a JDBC Driver , Java would know what type driver it needs from the database url string that one passes to the getConnection method. For instance the database url to connect to a oracle database would be something like this:

public static final String DB_URL = "jdbc:oracle:thin@//localhost:1521/ORCL";

然后,DriverManager将在项目类路径中指定的jar中寻找oracle驱动程序的实现.它将在每个jar的META-INF/Services目录中查找驱动程序配置文件(在其中为实际的驱动程序类的名称). Class Loader将加载找到的第一个匹配项,而忽略其余匹配项.

The DriverManager would then look for implementation of oracle driver in the jars specified in the projects class path. It would look for the drivers configuration files (in which would be the name of actual driver classes) in META-INF/Services directory of each jar. The Class Loader will load the very first match that it finds and ignore the rest.

以上工作是否正确?请让我知道如果我错过了什么或做错了什么.

Is the above working accurate ? Please let me know If I missed something or got something wrong.

推荐答案

如果检查源代码,您将看到Java不会尝试从URL中检测驱动程序的实现名称(即驱动程序类).相反,它询问在类路径中找到的每个驱动程序实现是否能够处理该URL.

If you check the source code you will see that Java does not try to detect the driver's implementation name (i.e. the driver class) from the url. Instead it asks each driver implementation it finds in the classpath if they are able to handle that url or not.

动作顺序似乎如下:

  • 当您请求连接时,将加载DriverManager类.它执行一个静态块,该块加载系统属性jdbc.drivers
  • 中指定的所有类.
  • 然后调用服务提供程序机制并加载在类路径中找到的所有java.sql.driver类.
  • When you ask for the connection the DriverManager class is loaded. It executes a static block that loads all the classes specified in the system property jdbc.drivers
  • Then the Service Provider Mechanism is invoked and loads all classes of java.sql.driver it finds in the classpath.

现在,当您要求它建立连接时,它会循环遍历已注册的驱动程序并调用

Now when you ask it for a connection it loops through the registered drivers and call the Driver.connect(String url, Properties info) method on them. Quote:

尝试建立到给定URL的数据库连接.司机 如果意识到这是错误的驱动程序,则应返回"null" 连接到给定的URL.这将很常见,例如JDBC驱动程序 经理被要求连接到给定的URL,它将URL传递给每个URL. 依次加载驱动程序.

Attempts to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.

如果驱动程序是正确的驱动程序,则应引发SQLException 连接到给定的URL,但无法连接到数据库.

The driver should throw an SQLException if it is the right driver to connect to the given URL but has trouble connecting to the database.

因此,第一个返回非null连接的驱动程序就是将要使用的驱动程序.

So the first driver that returns a non null connection is the driver that will be used.

希望有帮助

这篇关于了解Java的服务提供程序机制,该机制可自动加载Jdbc驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆