SpringCloudAlibaba、Hibernate集成openGauss数据库

SpringCloudAlibaba、Hibernate集成openGauss数据库

OpenGauss是华为推出的开源关系型数据库管理系统,旨在为企业提供高性能、高可靠、高可扩展的数据库服务。

  1. 技术背景:
    OpenGauss基于PostgreSQL开发,并融合了华为在数据库领域的多年经验和技术积累。
    支持多种数据库特性,特别是在处理大数据量、高并发事务和复杂查询时表现优异。
  2. 核心特性
    高性能:通过多核并行处理、智能查询优化和大规模数据分布等技术,提供高性能的数据库引擎。
    高可用性:支持主备复制、自动故障切换和数据恢复,确保数据库的持续可用性。
    高扩展性:支持分布式数据库架构,能够在多节点上分布数据,提供水平扩展能力。
    高安全性:提供多层次的安全保障,包括数据加密、访问控制和审计日志等。
  3. 核心特性
    OpenGauss与PostgreSQL高度兼容,大部分PostgreSQL应用、SQL语法、扩展和工具都能在OpenGauss上运行。
  4. 应用场景:
    适用于银行、金融、电商等需要高可靠性、高并发处理的大型系统。
    支持大规模数据存储和分析场景,适合需要进行复杂查询和分析的应用。
    可用于构建混合事务与分析处理系统,适应现代数据架构。

1.Maven的pom.xml配置opengauss-jdbc数据库驱动

  <dependency>
     <groupId>org.opengauss</groupId>
     <artifactId>opengauss-jdbc</artifactId>
     <version>6.0.0</version>
  </dependency>

需要注意:
1.有很多文章中都是使用的postgresql的驱动,会有问题,因为高斯数据库的密码加密使用的是sha256,而postgresql驱动不支持sha256,会报错:

org.postgresql.util.PSQLException: Invalid or unsupported by client SCRAM mechanisms
  1. 如果使用阿里云仓库拉下来的驱动包,启动项目报下面的错,就去opengauss官网去下载驱动更新到Maven仓库:
Cannot load driver class: org.opengauss.Driver

2.nacos的yml中配置参数

在nacos中配置ms-common-db-gauss.yml、 fms-common-db-gauss-env.yml,这2个yml文件微服务都需要引用才行。

fms-common-db-gauss.yml
spring:
  jpa:
    database: postgresql
    properties:
      hibernate:
        jdbc:
          batch_size: 500
          batch_versioned_data: true
        order_inserts: true
        order_updates: true
    hibernate:
      use-new-id-generator-mappings: false
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  datasource:
    driver-class-name: org.opengauss.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      max-active: 50
      max-wait: 10000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 100
      phy-timeout-millis: 25200000
      test-on-borrow: true
      test-while-idle: true
      validation-query: 'select 1'
        
fms-common-db-gauss-env.yml

database:数据库名称
currentSchema=test1:模式名称
batchMode=off, 使用Hibernate,需要把batchMode参数配置成off

spring:
  datasource:
    url: jdbc:opengauss://127.0.0.1:15400/database?currentSchema=test1&batchMode=off
    username: root
    password: Gauss_123
  1. 如果不把batchMode参数配置成off,会出现下面的错误;如果各位没有出现此错误,欢迎留言评论交流。
Caused by: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 5; expected: 1
出现的问题
1、在保存数据时,数据库连接中断,出现下面错误
Caused by: org.hibernate.TransactionException: Unable to rollback against JDBC Connection
Caused by: org.opengauss.util.PSQLException: This connection has been closed.
Caused by: java.io.EOFException: EOF Exception

并不是所有保存数据时,都会出现这个错误信息,经过排查发现,在一个(有事务,标注了 @Transactional)方法中,执行时间过长(并没有验证会出现错误的具体的时长)就会偶发的出现此错误;出现执行时间过长的逻辑是不需要事务的,所有我的处理方法是把不需要事务的逻辑写在另一个方法中,需要事务的逻辑写在一个方法中;经过这样处理,就没有再出现过报错;有好的处理方法欢迎留言评论交流。

2数据库表、字段大写

如果表、字段是有大写的字母,那么在代码中表对应的实体字段上的@Column(name = ““name””)需要加上双引号;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值