Transaction support has been recently added to the Couchbase SDKs. Starting in Spring Data Couchbase 5.0.0-M5, support for transactions has been added to Spring 데이터 카우치베이스. In this blog, we will describe using the Spring Data @Transactional annotation to leverage Couchbase Transactions.

그리고 스프링 데이터 카우치베이스 repository contains a spring-data-testapp which exercises transactions. It relies on the 여행 샘플 bucket on a local Couchbase server. (any bucket with a primary index will suffice).

Beans supporting transaction management have been added to 추상 카우치베이스 구성. To allow the built-in transaction interceptor class to be overridden with the CouchbaseTransactionInterceptor, ensure the following line is in the application.properties file:

application.properties

The transaction configuration can be modified by overriding the configureEnvironment() method in your class which extends 추상 카우치베이스 구성.  Here we set the durabilityLevel없음 so we can work in our single-node cluster.

Config.java

In the class which will use them, we need to define the template as usual and a reference to the service object. It’s important to note that the 오토와이어드 annotation  populates a variable with a proxy object. This is an important detail as the proxy objects perform processing before and after calling the actual method. Directly creating the service object from its constructor will result in the @Transactional annotation having no effect.

CmdRunner.java

The service is defined with an 서비스 annotation. The constructor takes arguments which are @Bean objects, such that the infrastructure can create a proxy object for 오토와이어드.

AirlineGateService.java

AirlineGateService.java (cont’d)

Now use everything we have constructed:

  1. save two AirlineGates documents, each of which indicates 200 gates
  2. verify they have been saved.
  3. execute the transferGates() method of the service to transfer 50 gates from one airline to the other in a transaction.
  4. verify that the transfer has taken place.
  5. attempt to execute transferGates() again, this time with an exception occurring after the first document has been saved.

    CmdRunner.java

How it works:

  • 그리고 TransactionInterceptor @Bean in 추상 카우치베이스 구성 overrides the built-in TransactionInterceptor for @Transactional annotated methods.  
  • The @Transactional annotation causes the proxy for AirportGatesService 를 사용하여 TransactionInterceptor to call the annotated method using the CouchbaseCallbackTransactionManager.  
  • 그리고 CouchbaseCallbackTransactionManager initializes a transaction and then calls the actual service method in the context of the transaction. 
  • If the method call is successful, the CouchbaseCallbackTransactionManager commits the transaction. 
  • If the commit is successful, the call returns. 
  • If the commit fails with a retryable exception, the CouchbaseCallbackTransactionManger will retry the complete process of initializing a new transaction, executing the actual service method and then committing the transaction. 

그리고 CouchbaseCallbackTransactionManager will repeat this until either the commit succeeds, or the maximum number of retries has been reached.

Options for next steps

작성자

게시자 마이클 라이시, 수석 소프트웨어 엔지니어

댓글 하나

  1. 마이클 라이시 10월 18, 2022에서 10:55 오전

    Hi Folks – the couchbase/transactions project has not yet been merged into spring-data-examples. It will be available shortly.
    – Mike

  2. 마이클 라이시 11월 17, 2022에서 12:49 오후

    The transaction sample needs spring-boot-starter-data-couchbase 3.0.0-RC2 to run due to an incompatibility with reactor in other 3.0.0* versions.

  3. Hi Michael, Thanks for implementing built-in couchbase transaction support. It works very well when I use “couchbase transaction” using the single bucket, but I have an error in transactions using with multiple buckets.

    I implemented this link https://github.com/spring-projects/spring-data-couchbase/issues/878. This implementation is not working with transactions. Do you have any idea?

  4. 마이클 라이시 4월 3, 2023에서 12:45 오후

    Spring Data Couchbase Transactions will not work with multi-buckets as multi-buckets in Spring Data Couchbase requires multiple connections to Couchbase and the transaction must be on a single connection.
    – Mike

댓글 남기기