H2:JdbcSQLException:URL格式错误

和Xa

我用一个应该连接到内存数据库的配置替换了一个可以连接到真实数据库的配置。我想将此配置用于集成测试。一个简单的测试看起来像这样

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
    @Test
    public void contextLoads() {}
}

pom.xml中的新配置

<profiles>
    <profile>
        <id>IntegrationTest</id>
        <properties>
            <datasource.url>jdbc:h2:mem:db_name_test;MODE=MSSQLServer;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS db_name_test\\;SET SCHEMA db_name_test</datasource.url>
            <datasource.username>sa</datasource.username>
            <datasource.password></datasource.password>
        </properties>
    </profile>
    <!-- other profiles omitted -->
</profiles>

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.195</version>
    </dependency>
    <!-- some dependencies omitted -->
</dependencies>

这些属性被注入到资源文件中,然后在上下文初始化期间由Spring拾取。XML上下文中的数据源配置如下所示:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="url" value="${datasource.url}"/>
    <property name="username" value="${datasource.username}"/>
    <property name="password" value="${datasource.password}"/>
</bean>

我使用以下命令来构建项目并执行测试:

mvn -T 1C clean integration-test -P IntegrationTest

现在,与以前的配置一起使用的测试会引发以下异常。

堆栈跟踪

2018-03-13 13:36:04.627 ERROR 9076 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@cb6c1e9] to prepare test instance [com.WebApplicationTests@3d52aca]

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... some frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'aService': Unsatisfied dependency expressed through field 'ctSecurityUtils'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ctSecurityUtils': Unsatisfied dependency expressed through field 'arService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arService': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... some frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ctSecurityUtils': Unsatisfied dependency expressed through field 'arService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arService': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... some frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arService': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... some frames omitted
Caused by: org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... some frames omitted
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    ... some frames omitted
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]" but is "jdbc:h2:mem:db_name_test" [90046-196])
    at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) ~[commons-dbcp-1.4.jar:1.4]
    ... some frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]" but is "jdbc:h2:mem:db_name_test" [90046-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.196.jar:1.4.196]
    ... some frames omitted

如何解决?

和Xa

通过试错法,我发现连接字符串中的此语句导致了错误

\\;SET SCHEMA db_name_test

JdbcSQLException: URL format error删除时也不例外。但是我想保留应用程序的行为,因此与其删除语句,不如将其移入connectionInitSqls我也指定了driverClassName

更新的Maven配置文件:

<profile>
    <id>IntegrationTest</id>
    <properties>
        <datasource.driverClassName>org.h2.Driver</datasource.driverClassName>
        <datasource.url>jdbc:h2:mem:db_name_test;MODE=MSSQLServer;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS db_name_test</datasource.url>
        <datasource.connectionInitSqls>SET SCHEMA db_name_test</datasource.connectionInitSqls>
        <datasource.username>sa</datasource.username>
        <datasource.password></datasource.password>
    </properties>
</profile>

在XML上下文中更新了数据源配置:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${datasource.driverClassName}"/>
    <property name="url" value="${datasource.url}"/>
    <property name="username" value="${datasource.username}"/>
    <property name="password" value="${datasource.password}"/>
    <property name="connectionInitSqls" value="${datasource.connectionInitSqls}"/>
</bean>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

具有正确DDL sql的H2 org.h2.jdbc.JdbcSQLException:错误代码= [42000-196]

JdbcSQLException在H2中使用``MATCH simple''执行PostgreSQL查询

H2 - 甲骨文 - liquibase - org.h2.jdbc.JdbcSQLException:表 “all_sequences” 找不到;

Spring H2数据库EmbeddedDatabaseFactory错误的连接URL

H2数据库异常“引起原因:org.h2.jdbc.JdbcSQLException:找不到序列“ SYSTEM_SEQUENCE_ *”;” 在运行Junit测试用例时

过滤 <p> </p> 之间的所有内容的 url,其中 <h2></h2> = 值

为什么尝试通过“ jdbc:h2:test”的URL创建数据库时收到错误消息

运行Spring应用程序时出错(org.h2.jdbc.JdbcSQLNonTransientConnectionException:URL格式错误)

h2 SCRIPT命令的错误“ CREATE SEQUENCE”行

Play框架任务示例H2错误-已修复

Grails抛出H2 WebServlet加载错误

H2 的 Hibernate 多租户问题:错误的架构

从类分配数据的Ormlite H2错误

数据转换错误H2数据库

什么是micronaut中的H2默认jdbc url

org.h2.jdbc.JdbcSQLException:找不到列“ Salman”;

org.h2.jdbc.JdbcSQLException:找不到架构“ DBO”

休眠:找不到表 - org.h2.jdbc.JdbcSQLException

在H2数据库中编写查询以转换日期格式

如何在H2中将时间转换为24小时格式

是否将日期“ 1472120311221”转换为H2数据库中的日期时间格式?

H2语句池

H2中的where()

H2性能建议

h2类的appendChild

WSO2 ESB 启动错误 - H2 数据库无法删除行

H2 数据库:NIO JVM 错误消息是否与 H2(以及可能的数据库损坏)有关?

UPDATE语句H2 DB中的预期SET错误代码:42001

H2模式的最终化。SQL语句中的语法错误