JHipster多个数据源导致JdbcSQLSyntaxErrorException:找不到序列“ SEQUENCEGENERATOR”

时间:2019-06-07 09:37:30

标签: java hibernate spring-boot jhipster

solution here之后,我想为我的应用程序(两个SQL Server)添加两个额外的数据源。我的设置是:

  • 让JHipster拥有自己的数据库,并可以通过JPA访问它。在开发环境中,在生产SQL Server中使用H2。
  • 添加两个我仅用于JDBC(JdbcTemplate)的SQL Server数据源。这些是read-only数据库,我只需要在它们上运行查询。

因此,我生成了一个新的JHipster Monolithic应用程序(6.0.1),其中包含用于开发和生产中的SQL Server的基于磁盘的h2。我将DatabaseConfigration更改为此:

@Configuration
@EnableJpaRepositories(".....repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class DatabaseConfiguration {

    private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);

    private final Environment env;

    public DatabaseConfiguration(Environment env) {
        this.env = env;
    }

    /**
     * Open the TCP port for the H2 database, so it is available remotely.
     *
     * @return the H2 database TCP server.
     * @throws SQLException if the server failed to start.
     */
    @Bean(initMethod = "start", destroyMethod = "stop")
    @Profile(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)
    public Object h2TCPServer() throws SQLException {
        String port = getValidPortForH2();
        log.debug("H2 database is available on port {}", port);
        return H2ConfigurationHelper.createServer(port);
    }

    private String getValidPortForH2() {
        int port = Integer.parseInt(env.getProperty("server.port"));
        if (port < 10000) {
            port = 10000 + port;
        } else {
            if (port < 63536) {
                port = port + 2000;
            } else {
                port = port - 2000;
            }
        }
        return String.valueOf(port);
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties defaultDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.hikari")
    public DataSource defaultDataSource() {
        return defaultDataSourceProperties().initializeDataSourceBuilder()
            //.type(HikariDataSource.class)
            .build();
    }

    @Bean(name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
        return builder
            .dataSource(defaultDataSource())
            .packages(MyMainApplicationClassApp.class)
            .persistenceUnit("default")
            .build();
    }

    @Bean(name = "transactionManager")
    @Primary
    public JpaTransactionManager db2TransactionManager(@Qualifier("entityManagerFactory") final EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}

然后我创建了一个名为AdditionalDatabaseConfiguration的新配置类,首先我只想添加一个附加数据库:

@Configuration
@EnableTransactionManagement
public class AdditionalDatabaseConfiguration {

    @Bean
    @ConfigurationProperties("sync.datasource")
    public DataSourceProperties SyncDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("sync.datasource.hikari")
    public HikariDataSource SyncDataSource() {
        var properties = SyncDataSourceProperties();
        return properties.initializeDataSourceBuilder()
            .type(HikariDataSource.class)
            .build();
    }
}

我还没有更改默认的spring.datasource配置,但是我确实为新的ds添加了配置:

sync:
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:sqlserver://***:1433;database=***
        username: ****
        password: ***
        testOnBorrow: true
        validationQuery: SELECT 1
        hikari:
            poolName: HikariSync
            auto-commit: false
            connection-test-query: SELECT 1
            maximum-pool-size: 2
            idle-timeout: 10000
            data-source-properties:
                cachePrepStmts: true
                prepStmtCacheSize: 250
                prepStmtCacheSqlLimit: 2048
                useServerPrepStmts: true

应用程序编译并启动,没有错误或警告,并且应用程序的主页也加载。但是,如果尝试登录,则会出现异常:

https://pastebin.com/raw/HWSEQGyq

org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [call next value for sequenceGenerator]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:279)
        at org.sprin.....
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
        ... 130 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Sequence "SEQUENCEGENERATOR" not found; SQL statement:
call next value for sequenceGenerator [90036-199]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:573)
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
        at org.h2.message.DbException.get(DbException.java:205)
        at org.h2.message.DbException.get(DbException.java:181)
        at org.h2.command.Parser.readSequence(Parser.java:7196)
        at org.h2.command.Parser.readTermWithIdentifier(Parser.java:4192)
        at org.h2.command.Parser.readTerm(Parser.java:3972)
        at org.h2.command.Parser.readFactor(Parser.java:3090)
        at org.h2.command.Parser.readSum(Parser.java:3077)
        at org.h2.command.Parser.readConcat(Parser.java:3047)
        at org.h2.command.Parser.readCondition(Parser.java:2900)
        at org.h2.command.Parser.readAnd(Parser.java:2872)
        at org.h2.command.Parser.readExpression(Parser.java:2864)
        at org.h2.command.Parser.parseCall(Parser.java:6043)
        at org.h2.command.Parser.parsePrepared(Parser.java:848)
        at org.h2.command.Parser.parse(Parser.java:788)
        at org.h2.command.Parser.parse(Parser.java:760)
        at org.h2.command.Parser.prepareCommand(Parser.java:683)
        at org.h2.engine.Session.prepareLocal(Session.java:627)
        at org.h2.engine.Session.prepareCommand(Session.java:565)
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1292)
        at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:77)
        at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:349)
        at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:311)
        at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
        ... 165 common frames omitted

我的@Primary数据源配置/代码可能不正确。谁能告诉我要添加什么?

编辑。:如果删除数据源配置,则可以登录,这是导致错误的原因。

编辑。:主要问题是Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Sequence "SEQUENCEGENERATOR" not found; SQL statement: call next value for sequenceGenerator [90036-199]

Edit2 ::为默认数据源设置方言并不能解决问题(使用yml):

  jpa:
    database-platform: io.github.jhipster.domain.util.FixedH2Dialect <-- maybe this is the issue?
    database: H2
    show-sql: true
    properties:
      hibernate.id.new_generator_mappings: true
      hibernate.connection.provider_disables_autocommit: true
      hibernate.cache.use_second_level_cache: true
      hibernate.cache.use_query_cache: false
      hibernate.generate_statistics: false
      hibernate.dialect: org.hibernate.dialect.H2Dialect <-- this

Edit3:我在开发人员配置文件(直接在SQL Server)上未使用H2的情况下生成了一个新项目,添加了相同的多数据源配置,问题仍然存在。我刚收到一个com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'sequenceGenerator'.异常。

Edit4 .:我的MSSQL问题可以通过将sequence_generatordomain.User文件中的生成器名称更改为domain.PersistentAuditEvent来解决。 H2的具体问题?

tasukete kudasai

1 个答案:

答案 0 :(得分:0)

在我的情况下,jhipster为序列生成了不同的名称(我选择了oracle db进行生产) 000000000000000_initial_schema.xml中的“ hibernate_sequence”

    

域实体中的

“ sequenceGenerator”

@Id @GeneratedValue(策略= GenerationType.SEQUENCE,生成器=“ sequenceGenerator”) @SequenceGenerator(name =“ sequenceGenerator”) 私人Long ID;

通过在两个地方设置相同的名称已解决了问题。