【问题标题】:Exception java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist, when trying to make a query to Oracle DB尝试查询 Oracle DB 时出现异常 java.sql.SQLSyntaxErrorException:架构“SA”不存在
【发布时间】:2020-04-17 13:49:56
【问题描述】:



几个星期以来我一直犯错误,我想知道这里的任何人是否可以提供一些启示......

我有一个 Web 应用程序,我使用 Spring Boot 构建它,它连接到两个数据库,都是 Oracle。

在我的开发环境中,我部署在 Spring Boot 嵌入式 tomcat 中,一切正常。当我在 Weblogic(集成环境)中部署时出现问题,当我尝试登录时,在尝试执行 select 时返回以下异常:

<Dec 27, 2019 1:58:16,335 PM CET> <Warning> <org.hibernate.engine.jdbc.spi.SqlExceptionHelper> <BEA-000000> <SQL Error: 30000, SQLState: 42Y07>
<Dec 27, 2019 1:58:16,336 PM CET> <Error> <org.hibernate.engine.jdbc.spi.SqlExceptionHelper> <BEA-000000> <Schema 'SA' does not exist>
<Dec 27, 2019 1:58:16,343 PM CET> <Error> <org.springframework.boot.web.servlet.support.ErrorPageFilter> <BEA-000000> <Forwarding to error page from request [/login_ldap] due to exception [could not prepare statement; SQL [select userentity0_.id as id1_6_, userentity0_.name as name2_6_, userentity0_.operator as operator3_6_, userentity0_.profile as profile4_6_, userentity0_.roles as roles5_6_, userentity0_.theme as theme6_6_, userentity0_.token as token7_6_ from USERS userentity0_ where userentity0_.name=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement]
org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select userentity0_.id as id1_6_, userentity0_.name as name2_6_, userentity0_.operator as operator3_6_, userentity0_.profile as profile4_6_, userentity0_.roles as roles5_6_, userentity0_.theme as theme6_6_, userentity0_.token as token7_6_ from USERS userentity0_ where userentity0_.name=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:240)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:223)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    ....
    ....
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
Caused By: org.hibernate.exception.SQLGrammarException: could not prepare statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:181)
    ....
    ....
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
Caused By: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    ....
    ....
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
Caused By: ERROR 42Y07: Schema 'SA' does not exist
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getSchemaDescriptor(Unknown Source)
    at org.apache.derby.iapi.sql.StatementUtil.getSchemaDescriptor(Unknown Source)
    ....
    ....
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
>

我访问了很多论坛,他们都提到了使用 Derby DB 时可能出现的错误,实际上,在异常的跟踪中我可以看到org.apache.derby.iapi.error.StandardException(我不明白为什么)。
当我添加第二个数据源时,问题就出现了,因为 Spring Boot 默认带来的配置对我不利,因此我不得不“手动”配置它。
也许,我有一些错误的配置,但我找不到它。

我的 application.properties 是:

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@//IP1:PUERTO1/SERVICIO1
spring.datasource.jdbc-url=jdbc:oracle:thin:@//IP1:PUERTO1/SERVICIO1
spring.datasource.username=USERNAME1
spring.datasource.password=PASSWORD1
spring.datasource.hikari.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.hikari.connection-init-sql=ALTER SESSION SET CURRENT_SCHEMA=USERNAME1

spring.remd-datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.remd-datasource.url=jdbc:oracle:thin:@//IP2:PUERTO2/SERVICIO2
spring.remd-datasource.jdbc-url=jdbc:oracle:thin:@//IP2:PUERTO2/SERVICIO2
spring.remd-datasource.username=USERNAME2
spring.remd-datasource.password=PASSWORD2
spring.remd-datasource.hikari.driver-class-name=oracle.jdbc.OracleDriver
spring.remd-datasource.hikari.connection-init-sql=ALTER SESSION SET CURRENT_SCHEMA=USERNAME2

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

logging.level.org.springframework.web=ERROR
logging.level.org.springframework.security.access.intercept=debug
logging.level.org.springframework.security=ERROR
logging.level.com.hpe=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.file=./logs/serverApplication.log

server.error.whitelabel.enabled=false


以及我为这两个数据源创建的配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactory",
    basePackages = { "com.project.repository.clar" }
)
public class ClarDataSourceConfiguration {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    @Bean(name = "dataSource")
    public DataSource dataSource(DataSourceProperties properties) {
        return DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.project.model.clar").persistenceUnit("clar").build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

和

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "remdEntityManagerFactory", 
    transactionManagerRef = "remdTransactionManager", 
    basePackages = { "com.project.repository.remd" }
)
public class RemdDataSourceConfiguration {

    @ConfigurationProperties(prefix = "spring.remd-datasource")
    @Bean(name = "remdDataSource")
    public DataSource dataSource(DataSourceProperties properties) {
        return DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
    }

    @Bean(name = "remdEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean remdEntityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("remdDataSource") DataSource remdDataSource) {
        return builder.dataSource(remdDataSource).packages("com.project.model.remd").persistenceUnit("remd").build();
    }

    @Bean(name = "remdTransactionManager")
    public PlatformTransactionManager remdTransactionManager(@Qualifier("remdEntityManagerFactory") EntityManagerFactory remdEntityManagerFactory) {
        return new JpaTransactionManager(remdEntityManagerFactory);
    }

}

提前感谢大家和问候!!

【问题讨论】:

    标签: java database spring-boot oracle11g weblogic12c


    【解决方案1】:

    我知道这可能无法回答您的问题,但不久前我遇到了同样的问题(定义多个数据源);这是我的解决方案:

    @Configuration
    @EnableTransactionManagement
    class PersistenceConfig {
      @Bean(name = "oneDS")
      @ConfigurationProperties(prefix = "custom-namespace.datasource.one")
      public DataSource oneDataSource() {
        return DataSourceBuilder.create().build();
      }
    
      @Primary
      @Bean(name = "mainDS")
      @ConfigurationProperties(prefix = "custom-namespace.datasource.main")
      public DataSource mainDataSource() {
        return DataSourceBuilder.create().build();
      }
    
      @Bean(name = "twoDS")
      @ConfigurationProperties(prefix = "custom-namespace.datasource.two")
      public DataSource twoDataSource() {
        return DataSourceBuilder.create().build();
      }
    
      @Bean
      public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(mainDataSource());
      }
    
      // ...other beans using the other datasources
    }
    

    ...这就是application.yml 在这些设置下的样子:

    请注意,application.yml 中的数据源没有内置配置。

    custom-namespace:
      datasource:
        one:
          driver-class-name: oracle.jdbc.OracleDriver
          jdbc-url: jdbc:oracle:thin:/@oneDS
        main:
          driver-class-name: oracle.jdbc.OracleDriver
          jdbc-url: jdbc:oracle:thin:/@mainDS
        two:
          driver-class-name: oracle.jdbc.OracleDriver
          jdbc-url: jdbc:oracle:thin:/@twoDS
    server:
      compression:
        enabled: true
      port: "${SERVER_PORT:8080}"
      ssl:
        enabled: true
    spring:
      application:
        name: "spring-core"
      banner:
        charset: UTF-8
      http:
        encoding:
          charset: UTF-8
          enabled: true
          force: true
      main:
        banner-mode: "off"
      output:
        ansi:
          enabled: detect
      profiles:
        active: default
    ---
    spring:
      profiles: development
    ---
    spring:
      profiles: production
    ---
    server:
      ssl:
        enabled: false
    spring:
      main:
        banner-mode: console
      profiles: sandbox
    ---
    spring:
      profiles: test
    

    由于我使用的是 Spring 配置文件,因此必须“激活”它们;例如:--spring.profiles.active=sandbox.

    【讨论】:

    • 嗨@x80486!非常感谢您的回复。我已经尝试过您提出的解决方案,但是当我尝试在 Weblogic 中部署我的应用程序时出现错误:java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required。(尽管 jdbc- url 在我的 application.properties 中)
    • 您使用的是哪个 Spring Boot 版本?看起来jdbc-url 不适合你;我会尝试使用url...
    • 我使用 Spring Boot 2.0.6,它内置了“Hikary 连接池”,jdbc-ur 属性是 Hikary CP 所必需的(而不是 jdbc 属性)。无论如何,我发现了错误。由于我无法理解的原因,Spring 没有对我试图从配置类中读取的 application.properties 文件中定义的属性收费。相反,如果我对这些值进行硬编码,所有这些都可以完美运行。作为一种解决方法,我在另一个属性文件中定义了这些属性并且工作正常,但我将继续调查为什么我的 application.properties 中的属性没有被收费。
    • 感谢您的时间和支持@x80486,新年快乐!
    【解决方案2】:

    我发现了错误。 由于我无法理解的原因,Spring 没有对我试图从配置类中读取的 application.properties 文件中定义的属性进行收费(properties.determineDriverClassName()、properties.determineUrl() 等)。相反,如果我对这些值进行硬编码,所有这些都可以正常工作。

    作为一种解决方法,我在另一个属性文件中定义了这些属性并且工作正常,但我将继续调查为什么我的 application.properties 中的属性不是正在充电。

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      • 2012-05-30
      • 2012-12-29
      相关资源
      最近更新 更多