/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.transaction.xa;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.transaction.core.ResourceDataSource;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager;
import org.apache.shardingsphere.transaction.xa.jta.connection.SingleXAConnection;
import org.apache.shardingsphere.transaction.xa.jta.datasource.SingleXADataSource;
import org.apache.shardingsphere.transaction.xa.manager.XATransactionManagerLoader;
import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;

import javax.sql.DataSource;
import javax.transaction.Status;
import java.sql.Connection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Sharding transaction manager for XA.
 *
 * @author zhaojun
 */
public final class XAShardingTransactionManager implements ShardingTransactionManager {
    
    private final Map<String, SingleXADataSource> singleXADataSourceMap = new HashMap<>();
    
    private final XATransactionManager xaTransactionManager = XATransactionManagerLoader.getInstance().getTransactionManager();
    
    @Override
    public void init(final DatabaseType databaseType, final Collection<ResourceDataSource> resourceDataSources) {
        for (ResourceDataSource each : resourceDataSources) {
            DataSource dataSource = each.getDataSource();
            if (dataSource instanceof AtomikosDataSourceBean) {
                continue;
            }
            SingleXADataSource singleXADataSource = new SingleXADataSource(databaseType, each.getUniqueResourceName(), dataSource);
            singleXADataSourceMap.put(each.getOriginalName(), singleXADataSource);
            xaTransactionManager.registerRecoveryResource(each.getUniqueResourceName(), singleXADataSource.getXaDataSource());
        }
        xaTransactionManager.init();
    }
    
    @Override
    public TransactionType getTransactionType() {
        return TransactionType.XA;
    }
    
    @SneakyThrows
    @Override
    public boolean isInTransaction() {
        return Status.STATUS_NO_TRANSACTION != xaTransactionManager.getTransactionManager().getStatus();
    }
    
    @SneakyThrows
    @Override
    public Connection getConnection(final String dataSourceName) {
        SingleXAConnection singleXAConnection = singleXADataSourceMap.get(dataSourceName).getXAConnection();
        xaTransactionManager.enlistResource(singleXAConnection.getXAResource());
        return singleXAConnection.getConnection();
    }
    
    @SneakyThrows
    @Override
    public void begin() {
        xaTransactionManager.getTransactionManager().begin();
    }
    
    @SneakyThrows
    @Override
    public void commit() {
        xaTransactionManager.getTransactionManager().commit();
    }
    
    @SneakyThrows
    @Override
    public void rollback() {
        xaTransactionManager.getTransactionManager().rollback();
    }
    
    @Override
    public void close() throws Exception {
        for (SingleXADataSource each : singleXADataSourceMap.values()) {
            xaTransactionManager.removeRecoveryResource(each.getResourceName(), each.getXaDataSource());
        }
        singleXADataSourceMap.clear();
        xaTransactionManager.close();
    }
}

@@ -1669,16 +1669,68 @@
 ashMap;%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A
 import j
@@ -2132,24 +2132,241 @@
 ger();%0A    %0A
+    private ThreadLocal%3CList%3CString%3E%3E enlistedXAResource = new ThreadLocal%3CList%3CString%3E%3E() %7B%0A        @Override%0A        public List%3CString%3E initialValue() %7B%0A            return new LinkedList%3C%3E();%0A        %7D%0A    %7D;%0A    %0A
     @Overrid
@@ -3606,24 +3606,94 @@
 nnection();%0A
+        if (!enlistedXAResource.get().contains(dataSourceName)) %7B%0A    
         xaTr
@@ -3753,32 +3753,100 @@
 tXAResource());%0A
+            enlistedXAResource.get().add(dataSourceName);%0A        %7D%0A
         return s
@@ -4077,24 +4077,42 @@
  commit() %7B%0A
+        try %7B%0A    
         xaTr
@@ -4158,24 +4158,95 @@
 ).commit();%0A
+        %7D finally %7B%0A            enlistedXAResource.remove();%0A        %7D%0A
     %7D%0A    %0A 
@@ -4301,24 +4301,42 @@
 ollback() %7B%0A
+        try %7B%0A    
         xaTr
@@ -4384,24 +4384,95 @@
 rollback();%0A
+        %7D finally %7B%0A            enlistedXAResource.remove();%0A        %7D%0A
     %7D%0A    %0A 
@@ -4788,20 +4788,55 @@
 er.close();%0A
+        enlistedXAResource = null;%0A
     %7D%0A%7D%0A

add enlistedXAResource to avoid one resource enlisting many times in one thread.