在网络应用程序中放置META-INF / services / java.sql.Driver的位置 [英] Where to put META-INF/services/java.sql.Driver in web app

查看:107
本文介绍了在网络应用程序中放置META-INF / services / java.sql.Driver的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javadoc 说到加载my.sql.Driver类,META-INF / services / java.sql.Driver文件将包含条目:my.sql.Driver。



Don webapps通常有一个META-INF文件夹作为WEB-INF的兄弟?但是,JPA规范正在寻找WEB-INF / classes / META-INF /中的persistence.xml。



哪个地方对于services / java.sql.Driver是正确的?



顺便说一下,我得到没有合适的驱动程序在两个位置都有例外。

解决方案

你不应该自己提供。 JDBC驱动程序JAR文件应该已经包含它。至少,如果它是JDBC 4.0兼容的驱动程序。 javadoc DriverManager


DriverManager 方法 getConnection getDrivers 已得到增强,以支持Java Standard Edition Service Provider 机制。 JDBC 4.0驱动程序必须包含文件 META-INF / services / java.sql.Driver 。此文件包含 java.sql.Driver 的JDBC驱动程序实现的名称。例如,要加载 my.sql.Driver 类, META-INF / services / java.sql.Driver 文件将包含条目:

  my.sql.Driver 
Class.forName()
。当前使用 Class.forName()加载JDBC驱动程序的现有程序将继续工作而不进行修改。


如果你没有,那么它显然不是JDBC 4.0兼容的驱动程序。您需要自己显式加载驱动程序,或升级到JDBC 4.0兼容驱动程序,或者只使用容器管理 DataSource (这也提供了更多优势,例如作为连接池)。


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".

Don't webapps typically have a META-INF folder as a sibling of WEB-INF? However, JPA specs are looking for persistence.xml in WEB-INF/classes/META-INF/.

Which place is correct for services/java.sql.Driver?

By the way, I am getting "No suitable Driver" exception with it in both locations.

解决方案

You should not provide it yourself. The JDBC driver JAR file should already contain it. At least, if it's a JDBC 4.0 compliant driver. This is also explicitly mentioned in javadoc of DriverManager:

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 explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

If yours doesn't have, then it's apparently not a JDBC 4.0 compliant driver. 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).

这篇关于在网络应用程序中放置META-INF / services / java.sql.Driver的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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