View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.transaction;
19  
20  import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
21  import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
22  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
23  import org.apache.shardingsphere.transaction.api.TransactionType;
24  import org.apache.shardingsphere.transaction.exception.TransactionManagerNotFoundException;
25  import org.apache.shardingsphere.transaction.spi.ShardingSphereDistributedTransactionManager;
26  
27  import javax.sql.DataSource;
28  import java.util.Map;
29  
30  /**
31   * ShardingSphere transaction manager engine.
32   */
33  public final class ShardingSphereTransactionManagerEngine {
34      
35      private final TransactionType transactionType;
36      
37      private final ShardingSphereDistributedTransactionManager distributedTransactionManager;
38      
39      public ShardingSphereTransactionManagerEngine(final TransactionType transactionType) {
40          this.transactionType = transactionType;
41          distributedTransactionManager = TransactionType.LOCAL == transactionType ? null : TypedSPILoader.getService(ShardingSphereDistributedTransactionManager.class, transactionType.name());
42      }
43      
44      /**
45       * Initialize transaction manager.
46       *
47       * @param databaseTypes database types
48       * @param dataSourceMap data source map
49       * @param providerType transaction manager provider type
50       */
51      public void init(final Map<String, DatabaseType> databaseTypes, final Map<String, DataSource> dataSourceMap, final String providerType) {
52          if (TransactionType.LOCAL == transactionType) {
53              return;
54          }
55          distributedTransactionManager.init(databaseTypes, dataSourceMap, providerType);
56      }
57      
58      /**
59       * Get transaction manager.
60       *
61       * @param transactionType transaction type
62       * @return transaction manager
63       */
64      public ShardingSphereDistributedTransactionManager getTransactionManager(final TransactionType transactionType) {
65          if (TransactionType.LOCAL != transactionType) {
66              ShardingSpherePreconditions.checkNotNull(distributedTransactionManager, () -> new TransactionManagerNotFoundException(transactionType));
67          }
68          return distributedTransactionManager;
69      }
70      
71      /**
72       * Close transaction manager.
73       */
74      public void close() {
75          if (TransactionType.LOCAL == transactionType) {
76              return;
77          }
78          distributedTransactionManager.close();
79      }
80  }