Springcloudのトランザクションロールバックの方法

Spring Cloud では、以下の方法を使用してトランザクションのロールバックを実現できます。プログラミングによるトランザクションのロールバック:トランザクションの管理が必要なメソッドに `@Transactional` アノテーションをマークします。メソッドの実行時に例外が発生すると、トランザクションは前の状態にロールバックされます。

@Transactional
public void foo() {
   // 业务逻辑
}

宣言的トランザクションロールバック:トランザクションマネージャーをコンフィギュレーションファイルで設定することで、宣言的トランザクションロールバック機能を利用することができます。

# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=testuser
spring.datasource.password=testpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 事务管理器配置
spring.jpa.properties.hibernate.transaction.factory_class=org.hibernate.transaction.
JDBCTransactionFactory
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.
SpringSessionContext
spring.jpa.properties.hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.
NoJtaPlatform
spring.jpa.properties.hibernate.transaction.flush_before_completion=false
spring.jpa.properties.hibernate.transaction.auto_close_session=false

分散トランザクションソリューションの使用: 分散システムでトランザクションのロールバックを実装する必要がある場合、Spring Cloudが提供する分散トランザクションソリューション、例えばSpring Cloud AlibabaのSeataやSpring Cloud NetflixのHystrixなどが利用可能です。

bannerAds