简体   繁体   English

在网络应用程序中放置META-INF / services / java.sql.Driver的位置

[英]Where to put META-INF/services/java.sql.Driver in web app

The javadoc says "to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry: my.sql.Driver". javadoc说“要加载my.sql.Driver类,META-INF / services / java.sql.Driver文件将包含条目:my.sql.Driver”。

Don't webapps typically have a META-INF folder as a sibling of WEB-INF? Web应用程序通常没有META-INF文件夹作为WEB-INF的兄弟吗? However, JPA specs are looking for persistence.xml in WEB-INF/classes/META-INF/. 但是,JPA规范正在WEB-INF / classes / META-INF /中寻找persistence.xml。

Which place is correct for services/java.sql.Driver? 哪个地方对于services / java.sql.Driver是正确的?

By the way, I am getting "No suitable Driver" exception with it in both locations. 顺便说一下,我在两个地方都得到了“No suitable Driver”例外。

You should not provide it yourself. 你不应该自己提供。 The JDBC driver JAR file should already contain it. JDBC驱动程序JAR文件应该已包含它。 At least, if it's a JDBC 4.0 compliant driver. 至少,如果它是JDBC 4.0兼容的驱动程序。 This is also explicitly mentioned in javadoc of DriverManager : 这在DriverManager javadoc中也明确提到:

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. DriverManager方法getConnectiongetDrivers已得到增强,可支持Java Standard Edition Service Provider机制。 JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver . JDBC 4.0驱动程序必须包含文件META-INF/services/java.sql.Driver This file contains the name of the JDBC drivers implementation of java.sql.Driver . 此文件包含java.sql.Driver的JDBC驱动程序实现的名称。 For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry: 例如,要加载my.sql.Driver类, META-INF/services/java.sql.Driver文件将包含以下条目:

 my.sql.Driver 

Applications no longer need to explictly load JDBC drivers using Class.forName() . 应用程序不再需要使用Class.forName()明确加载JDBC驱动程序。 Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification. 当前使用Class.forName()加载JDBC驱动程序的现有程序将继续工作而无需修改。

If yours doesn't have, then it's apparently not a JDBC 4.0 compliant driver. 如果你没有,那么它显然不是JDBC 4.0兼容的驱动程序。 You'd need to explicitly load the driver yourself, or to upgrade to a JDBC 4.0 compliant driver, or just use a container managed DataSource (which offers more advantages as well, such as connection pooling). 您需要自己显式加载驱动程序,或者升级到JDBC 4.0兼容的驱动程序,或者只使用容器管理的DataSource (它提供了更多优势,例如连接池)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM