为我的MSSQL配置加载Bean对象时发生AbstractMethodError

时间:2019-03-13 10:27:32

标签: java spring hibernate spring-boot sql-server-2008

我正在尝试通过spring和hibernate连接并使用MSSQL。我收到以下错误

Caused by: java.lang.AbstractMethodError
at org.apache.commons.dbcp2.DelegatingConnection.getSchema(DelegatingConnection.java:989)
at org.apache.commons.dbcp2.DelegatingConnection.getSchema(DelegatingConnection.java:989)
at org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver$SchemaNameResolverJava17Delegate.resolveSchemaName(DefaultSchemaNameResolver.java:77)
at org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver.resolveSchemaName(DefaultSchemaNameResolver.java:70)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.determineCurrentSchemaName(JdbcEnvironmentImpl.java:298)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.<init>(JdbcEnvironmentImpl.java:232)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:114)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)

使用的dialectorg.hibernate.dialect.SQLServer2012Dialect,用于连接的驱动程序是com.microsoft.sqlserver.jdbc.SQLServerDriver。我可能做错了什么?

我尝试设置属性validationQuery

<bean class="org.apache.commons.dbcp2.BasicDataSource">
 <property name="connectionProperties"
             value="password=123;user=111"></property>
             <property name="maxTotal"  
value="8" />
            <property name="driverClassName" 
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="url" 
value="url" />
            <property name="testOnBorrow" value="true"/>
            <property name="validationQuery" value="select 1" />
</bean>

驱动程序的Maven依赖项是

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>

1 个答案:

答案 0 :(得分:0)

我了解到在初始化bean时由dbcp2建立连接是一个问题。所以我更新了sql驱动程序,它为我解决了问题。

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.1.0.jre8</version>
    </dependency>