使用通用数据库包时找不到驱动程序
我有一个由多个 OSGi 包构建的项目。其中之一是通用数据库包,它定义了可以在整个项目中使用的数据源。该服务的 spring bean 定义是:
<osgi:service interface="javax.sql.DataSource">
<bean class="org.postgresql.ds.PGPoolingDataSource">
<property name="databaseName" value="xxx" />
<property name="serverName" value="xxx" />
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
</osgi:service>
现在,当使用这个 DataSource 是一个不同的包时,我们收到一个错误:
No suitable driver found for jdbc:postgresql://localhost/xxx
我已尝试以下方法将 org.postgresql.Driver 添加到 DriverManager:
实例化了一个空 bean春季上下文中的驱动程序,如下所示:
在其中一个类中静态实例化驱动程序,如下所示:
Class.forName("org.postgresql.Driver");
a.在将
org.postgresql
包添加为DynamicImport-Package
时也尝试过此操作。添加了一个文件
META-INF\services\java.sql.Driver
,其内容为org.postgresql.Driver
这些解决方案似乎都没有帮助。
I have a project that is build up from several OSGi bundles. One of them is a generic Database bundle that defines a DataSource that can be used throughout the project. The spring bean definition of this service is:
<osgi:service interface="javax.sql.DataSource">
<bean class="org.postgresql.ds.PGPoolingDataSource">
<property name="databaseName" value="xxx" />
<property name="serverName" value="xxx" />
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
</osgi:service>
Now, when using this DataSource is a different bundle, we get an error:
No suitable driver found for jdbc:postgresql://localhost/xxx
I have tried the following to add the org.postgresql.Driver to the DriverManager:
Instantiated an empty bean for that Driver in the spring context, like this:
<bean class="org.postgresql.Driver" />
Instantiated the Driver statically in one of the classes, like this:
Class.forName("org.postgresql.Driver");
a. Also tried this while adding the
org.postgresql
package asDynamicImport-Package
.Added a file
META-INF\services\java.sql.Driver
with the contentorg.postgresql.Driver
None of these solutions seems to help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经找到了解决方案,或者至少是一个解决方法。在我的抽象 DAO 中,我添加了以下内容:
这似乎确实有效。有谁知道为什么?它与 Class.forName 解决方案没有什么不同。
I've found the solution, or at least a workaround. In my abstract DAO I've added the following:
This does seem to work. Does anyone know why? It is not that different from the
Class.forName
solution.“没有合适的驱动程序”与“找不到驱动程序”不是一回事。
这表明 JDBC 驱动程序类已加载,但连接 URL 的语法不正确。
"No suitable driver" isn't the same thing as "can't find the driver".
This suggests to me that the JDBC driver class was loaded, but the syntax of the connection URL is incorrect.