将 spring 数据配置连接到 IBM DB2 z/os 时出现异常

Exception while connecting spring data config to IBM DB2 z/os

我在启动 spring 引导应用程序时遇到以下异常(应用程序没有任何代码,只有 application.properties 文件中定义的与数据库相关的配置和连接参数)

无法从数据库中获取 SequenceInformation com.ibm.db2.jcc.am.SqlSyntaxErrorException:DB2 SQL 错误:SQLCODE=-204,SQLSTATE=42704,SQLERRMC=SYSCAT.SEQUENCES, 驱动程序=4.19.49

pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>db2.jcc</groupId> <!-- internal from private repo -->
        <artifactId>db2jcc_license_cu</artifactId>
        <version>4.19.49</version>
    </dependency>

    <dependency>
        <groupId>db2.jcc</groupId> <!-- internal from private repo -->
        <artifactId>db2jcc4</artifactId>
        <version>4.19.49</version>
    </dependency>

数据库连接属性:

     spring.jpa.hibernate.ddl-auto=validate
     spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
     spring.jpa.hibernate.dialect=org.hibernate.dialect.DB2Dialect
     spring.jpa.hibernate.synonyms=true
     spring.jpa.show-sql=true
     spring.db2.datasource.url=jdbc:db2://HOSTNAME:PORT/DBNAME
     spring.db2.datasource.username=somename
     spring.db2.datasource.password=password
     spring.datasource.testWhileIdle=true
     spring.datasource.validationQuery=SELECT 1
     spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
     spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

spring 引导中的数据库配置代码:

     @Configuration
     @EnableTransactionManagement
     @EnableJpaRepositories(entityManagerFactoryRef = "db2EntityManagerFactory", transactionManagerRef = "db2TransactionManager", basePackages = {
    "com.example.db.repositories" })
     public class DB2Config {

private Logger log = LogManager.getLogger(DB2Config.class);

@Value("${spring.datasource.driver-class-name}")
String driverClassName;

@Value("${spring.db2.datasource.url}")
String dataSourceUrl;

@Value("${spring.db2.datasource.username}")
String username;

@Value("${spring.db2.datasource.password}")
String passkey;

@Value("${spring.jpa.hibernate.ddl-auto}")
String hbm2ddl;

@Value("${spring.jpa.hibernate.dialect}")
String dialect;

@Bean
public DataSource db2DataSource() {
    log.info("Loading db2 datasource");
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(dataSourceUrl);
    dataSource.setUsername(username);
    dataSource.setPassword(passkey);
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean db2EntityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(db2DataSource());
    em.setPackagesToScan("com.example.db");
    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
    properties.put("hibernate.dialect", dialect);
    em.setJpaPropertyMap(properties);
    em.setPersistenceUnitName("db2");

    return em;
}

@Bean
public PlatformTransactionManager db2TransactionManager() {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(db2EntityManagerFactory().getObject());
    return transactionManager;
}

}

每个评论线程,在使用 Db2 时,请始终注意目标平台(Z/OS、i 系列、Linux/Unix/Windows)决定 SQL 方言,以及许多其他事情。平台决定 SQL 方言。

在您的情况下,当您使用 Db2 for Z/OS 时,有必要将 DB2390Dialect 与您的工具链一起使用,以便在目标数据库上引用正确的目录对象。具体来说,SYSIBM 是 Db2-for-Z/OS 目录对象的模式,而 SYSCAT 是用于 Linux/Unix/Windows.

的模式