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.infra.database.core.type.DatabaseType;
21  import org.apache.shardingsphere.infra.exception.core.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.ShardingSphereTransactionManager;
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 ShardingSphereTransactionManager transactionManager;
38      
39      public ShardingSphereTransactionManagerEngine(final TransactionType transactionType) {
40          this.transactionType = transactionType;
41          transactionManager = TransactionType.LOCAL == transactionType ? null : TypedSPILoader.getService(ShardingSphereTransactionManager.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              transactionManager.init(databaseTypes, dataSourceMap, providerType);
54          }
55      }
56      
57      /**
58       * Get transaction manager.
59       *
60       * @param transactionType transaction type
61       * @return transaction manager
62       */
63      public ShardingSphereTransactionManager getTransactionManager(final TransactionType transactionType) {
64          if (TransactionType.LOCAL != transactionType) {
65              ShardingSpherePreconditions.checkNotNull(transactionManager, () -> new TransactionManagerNotFoundException(transactionType));
66          }
67          return transactionManager;
68      }
69      
70      /**
71       * Close transaction manager.
72       */
73      public void close() {
74          if (TransactionType.LOCAL != transactionType) {
75              transactionManager.close();
76          }
77      }
78  }