为什么出现错误:未找到适合的驱动程序以供 jdbc:h2 使用

huangapple go评论111阅读模式
英文:

Why error No suitable driver found for jdbc:h2

问题

当我调试我的 Mule 应用程序时,出现错误:

org.springframework.dao.DataAccessResourceFailureException: 检索数据库元数据时出错;嵌套异常是 org.springframework.jdbc.support.MetaDataAccessException: 无法获取用于提取元数据的连接;嵌套异常是 org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获取 JDBC 连接;嵌套异常是 java.sql.SQLException: 无法为 jdbc:h2:~/test 找到合适的驱动程序

我的 Java 代码:

SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate.getDataSource()).withProcedureName("my_procedure_name").withSchemaName("my_schema");
...
call.execute(in);

我的应用程序上下文:

<bean id="dataSource2"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db2.driver}" />
    <property name="url" value="${db2.url}" />
    <property name="username" value="${db2.user}" />
    <property name="password" value="${db2.password}" />
</bean>

我的 app_name.properties:

db2.url=jdbc:h2:~/test
db2.driver=org.h2.Driver
db2.user=sa
db2.password=

我的 pom.xml:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
</dependency>

我的类路径:

<classpathentry kind="var" path="M2_REPO/com/h2database/h2/1.4.200/h2-1.4.200.jar"/>

运行应用程序后的日志信息:

INFO  org.springframework.jdbc.datasource.DriverManagerDataSource - 已加载 JDBC 驱动程序:org.h2.Driver

此外,我将 h2-1.4.200.jar(当我从 Anypoint Studio 启动应用程序时)放在以下位置:

c:\...\plugins\org.mule.tooling.server.3.9.0_6.4.0.201908221250\mule\lib\user\ 
和:
c:\...\plugins\org.mule.tooling.server.3.9.0_6.4.0.201908221250\mule\lib\boot\ 
和:
c:\...\plugins\org.mule.tooling.server.3.9.0_6.4.0.201908221250\mule\lib\mule\ 

当我从 Mule 服务器运行应用程序时,我将 h2-1.4.200.jar 放在以下位置:

..\mule-standalone-3.9.0\lib\boot
和
..\mule-standalone-3.9.0\lib\user
和:
..\mule-standalone-3.9.0\lib\mule

为什么应用程序找不到 h2 驱动程序?问题是什么?

谢谢。

英文:

When I debug my Mule application i have error:

org.springframework.dao.DataAccessResourceFailureException: Error retrieving database metadata; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:h2:~/test

my java code:

SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate.getDataSource()).withProcedureName(&quot;my_procedure_name&quot;).withSchemaName(&quot;my_schema&quot;);
...
call.execute(in)

my aaplicationContext:

&lt;bean id=&quot;dataSource2&quot;
        class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;
        &lt;property name=&quot;driverClassName&quot; value=&quot;${db2.driver}&quot; /&gt;
        &lt;property name=&quot;url&quot; value=&quot;${db2.url}&quot; /&gt;
        &lt;property name=&quot;username&quot; value=&quot;${db2.user}&quot; /&gt;
        &lt;property name=&quot;password&quot; value=&quot;${db2.password}&quot; /&gt;
&lt;/bean&gt;

my app_name.properties:

db2.url=jdbc:h2:~/test
db2.driver=org.h2.Driver
db2.user=sa
db2.password=

my pom:

&lt;dependency&gt;
	&lt;groupId&gt;com.h2database&lt;/groupId&gt;
	&lt;artifactId&gt;h2&lt;/artifactId&gt;
	&lt;version&gt;1.4.200&lt;/version&gt;
&lt;/dependency&gt;

my classpath:

&lt;classpathentry kind=&quot;var&quot; path=&quot;M2_REPO/com/h2database/h2/1.4.200/h2-1.4.200.jar&quot;/&gt;

log info after run application:

INFO  org.springframework.jdbc.datasource.DriverManagerDataSource - Loaded JDBC driver: org.h2.Driver

Also i put h2-1.4.200.jar (when I starting my application from AnypointStudio) to:

c:\...\plugins\org.mule.tooling.server.3.9.0_6.4.0.201908221250\mule\lib\user\ 
and:
c:\...\plugins\org.mule.tooling.server.3.9.0_6.4.0.201908221250\mule\lib\boot\ 
and:
c:\...\plugins\org.mule.tooling.server.3.9.0_6.4.0.201908221250\mule\lib\mule\ 

and when I run application from mule server h2-1.4.200.jar, I put here:

..\mule-standalone-3.9.0\lib\boot
and
..\mule-standalone-3.9.0\lib\user
and:
..\mule-standalone-3.9.0\lib\mule

why application driver h2 not found ? what's the problem ?

Thanks.

答案1

得分: 0

你混合使用了三种不同的管理JDBC驱动程序依赖的方式,同时在运行时中也重复引入了该库。这也使得问题更难理解,并且在部署应用程序时会出现困难。

我们先从Maven开始。只要在<dependencies>部分中使用了正确的依赖项,看起来你已经在pom中使用了正确的依赖项。

如果项目正在使用Maven,就不应该需要查看<classpathentry>,除非它与Maven不同步。你不应该手动更改类路径,或者在Anypoint Studio中编辑构建路径。这些都是Studio/Eclipse .classpath文件的一部分,应该保持不变。确保更新项目,以便Studio重新生成类路径。

关于MULE_HOME\lib\bootMULE_HOME\lib\userMULE_HOME\lib\mule(无论是在Studio中还是独立使用),你不应该将库放在这些位置。Maven的依赖已经足够了,你正在重复引入不同版本的库。即使你可以在lib\user中共享库,也不推荐这样做。这样会使得部署变得更加困难,而且根本不需要JDBC驱动程序。你不应该在lib\bootlib\mulelib的其他子目录中放置任何内容。这些位置是为运行时保留的。

首先尝试移除所有这些额外的内容,然后观察干净部署后会发生什么。

更新:

一旦库被清理干净,请注意Spring类本身可能存在一些classloading问题。通常最好使用数据源池实现。有几种选择,如c3p0、dbcp等。

以下是使用c3p0的示例:

<spring:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <spring:property name="driverClass" value="${JDBC.driver}"/>
  <spring:property name="jdbcUrl" value="${JDBC.URL}"/>
  <spring:property name="user" value="${JDBC.user}"/>
  <spring:property name="password" value="${JDBC.password}"/>
  <spring:property name="minPoolSize" value="5"/>
  <spring:property name="maxPoolSize" value="20"/>
</spring:bean>

更多示例请参考:https://help.mulesoft.com/s/article/Spring-based-datasources

英文:

You are mixing 3 different ways of managing the JDBC driver dependency, and also duplicating the library in the runtimes. That also makes it more difficult to understand the problem, and to deploy an application.

Let's start with Maven. It looks like you are using the right dependency in the pom, as long as it is in the <dependencies> section.

If the project is using Maven, there should not be any need to look into <classpathentry>, unless it is out of sync with Maven. You should not change the class path manually, or edit the build path in Anypoint Studio. These are part of Studio/Eclipse .classpath files and should be left alone. Be sure to update the project so Studio regenerates the classpath.

About MULE_HOME\lib\boot, MULE_HOME\lib\user, MULE_HOME\lib\mule (either in Studio or standalone), you should not put the library in there. The Maven dependency is enough and you are duplicating the versions. Even if you can share a library in lib\user, it is not recommended. It makes more difficult to replicate sanely a deployment and again, there is no need for the JDBC driver. You should not put anything at all in lib\boot nor lib\mule nor the other subdirectories of lib. These are reserved for the runtime.

Try removing all those extras first and see what happens with a clean deployment.

Update:

Once the libraries are cleaned up, take into account that the Spring class may have some classloading issues itself. It is almost always better to use a datasource pool implementation. There are several to choose, like c3p0, dbcp, and others.

One example with c3p0:

&lt;spring:bean id=&quot;dataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot; destroy-method=&quot;close&quot;&gt;
  &lt;spring:property name=&quot;driverClass&quot; value=&quot;${JDBC.driver}&quot;/&gt;
  &lt;spring:property name=&quot;jdbcUrl&quot; value=&quot;${JDBC.URL}&quot;/&gt;
  &lt;spring:property name=&quot;user&quot; value=&quot;${JDBC.user}&quot;/&gt;
  &lt;spring:property name=&quot;password&quot; value=&quot;${JDBC.password}&quot;/&gt;
  &lt;spring:property name=&quot;minPoolSize&quot; value=&quot;5&quot;/&gt;
  &lt;spring:property name=&quot;maxPoolSize&quot; value=&quot;20&quot;/&gt;
&lt;/spring:bean&gt;

See https://help.mulesoft.com/s/article/Spring-based-datasources for more examples.

huangapple
  • 本文由 发表于 2020年7月24日 20:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63073920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定