H2:JdbcSQLException:URL格式错误

时间:2018-03-13 11:01:59

标签: java url jdbc h2

我替换了一个配置,该配置与另一个应该连接到内存数据库的配置连接到真实数据库。我想使用此配置进行集成测试。一个简单的测试看起来像这样

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

如何解决?

1 个答案:

答案 0 :(得分:1)

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

\\;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>