gpt4 book ai didi

java - H2:JdbcSQLException:URL 格式错误

转载 作者:行者123 更新时间:2023-11-30 06:15:42 32 4
gpt4 key购买 nike

我用另一个应该连接到内存数据库的配置替换了一个连接到真实数据库的配置。我想使用此配置进行集成测试。一个简单的测试如下所示

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>

这些属性被注入(inject)到资源文件中,然后由 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

如何解决?

最佳答案

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

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

关于java - H2:JdbcSQLException:URL 格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49254492/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com