|
26 | 26 | import org.springframework.context.annotation.Bean; |
27 | 27 | import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; |
28 | 28 | import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.context.annotation.Role; |
29 | 30 | import org.springframework.core.type.filter.AnnotationTypeFilter; |
30 | 31 | import org.springframework.data.convert.CustomConversions; |
31 | 32 | import org.springframework.data.couchbase.CouchbaseClientFactory; |
|
40 | 41 | import org.springframework.data.couchbase.core.mapping.Document; |
41 | 42 | import org.springframework.data.couchbase.repository.config.ReactiveRepositoryOperationsMapping; |
42 | 43 | import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping; |
| 44 | +import org.springframework.data.couchbase.transaction.CouchbaseCallbackTransactionManager; |
| 45 | +import org.springframework.data.couchbase.transaction.CouchbaseTransactionInterceptor; |
| 46 | +import org.springframework.data.couchbase.transaction.CouchbaseTransactionalOperator; |
43 | 47 | import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy; |
44 | 48 | import org.springframework.data.mapping.model.FieldNamingStrategy; |
45 | 49 | import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; |
| 50 | +import org.springframework.transaction.TransactionManager; |
| 51 | +import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; |
| 52 | +import org.springframework.transaction.interceptor.TransactionAttributeSource; |
| 53 | +import org.springframework.transaction.interceptor.TransactionInterceptor; |
46 | 54 | import org.springframework.util.ClassUtils; |
47 | 55 | import org.springframework.util.StringUtils; |
48 | 56 |
|
@@ -123,7 +131,7 @@ protected Authenticator authenticator() { |
123 | 131 | * @param couchbaseCluster the cluster reference from the SDK. |
124 | 132 | * @return the initialized factory. |
125 | 133 | */ |
126 | | - @Bean |
| 134 | + @Bean(name = BeanNames.COUCHBASE_CLIENT_FACTORY) |
127 | 135 | public CouchbaseClientFactory couchbaseClientFactory(final Cluster couchbaseCluster) { |
128 | 136 | return new SimpleCouchbaseClientFactory(couchbaseCluster, getBucketName(), getScopeName()); |
129 | 137 | } |
@@ -280,9 +288,8 @@ public TranslationService couchbaseTranslationService() { |
280 | 288 |
|
281 | 289 | /** |
282 | 290 | * Creates a {@link CouchbaseMappingContext} equipped with entity classes scanned from the mapping base package. |
283 | | - * |
284 | 291 | */ |
285 | | - @Bean |
| 292 | + @Bean(BeanNames.COUCHBASE_MAPPING_CONTEXT) |
286 | 293 | public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customConversions) throws Exception { |
287 | 294 | CouchbaseMappingContext mappingContext = new CouchbaseMappingContext(); |
288 | 295 | mappingContext.setInitialEntitySet(getInitialEntitySet()); |
@@ -310,6 +317,44 @@ public ObjectMapper couchbaseObjectMapper() { |
310 | 317 | return mapper; |
311 | 318 | } |
312 | 319 |
|
| 320 | + /** |
| 321 | + * The default blocking transaction manager. It is an implementation of CallbackPreferringTransactionManager |
| 322 | + * CallbackPreferrringTransactionmanagers do not play well with test-cases that rely |
| 323 | + * on @TestTransaction/@BeforeTransaction/@AfterTransaction |
| 324 | + * |
| 325 | + * @param clientFactory |
| 326 | + * @return |
| 327 | + */ |
| 328 | + @Bean(BeanNames.COUCHBASE_TRANSACTION_MANAGER) |
| 329 | + CouchbaseCallbackTransactionManager couchbaseTransactionManager(CouchbaseClientFactory clientFactory) { |
| 330 | + return new CouchbaseCallbackTransactionManager(clientFactory); |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * The default TransactionalOperator. |
| 335 | + * |
| 336 | + * @param couchbaseCallbackTransactionManager |
| 337 | + * @return |
| 338 | + */ |
| 339 | + @Bean(BeanNames.COUCHBASE_TRANSACTIONAL_OPERATOR) |
| 340 | + public CouchbaseTransactionalOperator couchbaseTransactionalOperator( |
| 341 | + CouchbaseCallbackTransactionManager couchbaseCallbackTransactionManager) { |
| 342 | + return CouchbaseTransactionalOperator.create(couchbaseCallbackTransactionManager); |
| 343 | + } |
| 344 | + |
| 345 | + @Bean |
| 346 | + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 347 | + public TransactionInterceptor transactionInterceptor(TransactionManager couchbaseTransactionManager) { |
| 348 | + TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource(); |
| 349 | + TransactionInterceptor interceptor = new CouchbaseTransactionInterceptor(couchbaseTransactionManager, |
| 350 | + transactionAttributeSource); |
| 351 | + interceptor.setTransactionAttributeSource(transactionAttributeSource); |
| 352 | + if (couchbaseTransactionManager != null) { |
| 353 | + interceptor.setTransactionManager(couchbaseTransactionManager); |
| 354 | + } |
| 355 | + return interceptor; |
| 356 | + } |
| 357 | + |
313 | 358 | /** |
314 | 359 | * Configure whether to automatically create indices for domain types by deriving the from the entity or not. |
315 | 360 | */ |
@@ -375,5 +420,4 @@ private boolean nonShadowedJacksonPresent() { |
375 | 420 | public QueryScanConsistency getDefaultConsistency() { |
376 | 421 | return null; |
377 | 422 | } |
378 | | - |
379 | 423 | } |
0 commit comments