H2: JdbcSQLException: URL 格式错误
Posted
技术标签:
【中文标题】H2: JdbcSQLException: URL 格式错误【英文标题】:H2: JdbcSQLException: URL format error 【发布时间】:2018-03-13 11:01:59 【问题描述】:我用另一个应该连接到内存数据库的配置替换了一个连接到真实数据库的配置。我想将此配置用于集成测试。一个简单的测试如下所示
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】:通过反复试验的方法,我发现连接字符串中的这条语句导致了错误
\\;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>
【讨论】:
以上是关于H2: JdbcSQLException: URL 格式错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥返回 org.h2.jdbc.JdbcSQLException?
Hibernate/H2 外键抛出 org.h2.jdbc.JdbcSQLException
org.h2.jdbc.JdbcSQLException:模式“myschema”已经存在
org.h2.jdbc.JdbcSQLException:对象已经关闭
引起:org.h2.jdbc.JdbcSQLException: 数据转换错误转换
具有正确 DDL sql 的 H2 org.h2.jdbc.JdbcSQLException:错误代码 = [42000-196]