NarayanaXATransactionManager.java 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.transaction.xa.narayana.manager;
19 20 21 22 23 24 25 26 27 28

import com.arjuna.ats.arjuna.recovery.RecoveryManager;
import com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule;
import com.arjuna.ats.jbossatx.jta.RecoveryManagerService;
import com.arjuna.ats.jta.common.jtaPropertyManager;
import lombok.SneakyThrows;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.apache.shardingsphere.transaction.xa.spi.XATransactionManager;

import javax.sql.XADataSource;
L
Liang Zhang 已提交
29 30
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
31
import javax.transaction.TransactionManager;
32
import java.util.Properties;
33 34 35 36 37 38

/**
 * Narayana transaction manager.
 */
public final class NarayanaXATransactionManager implements XATransactionManager {
    
39
    private final TransactionManager transactionManager = jtaPropertyManager.getJTAEnvironmentBean().getTransactionManager();
40
    
41
    private final XARecoveryModule xaRecoveryModule = XARecoveryModule.getRegisteredXARecoveryModule();
42
    
43
    private final RecoveryManagerService recoveryManagerService = new RecoveryManagerService();
44 45 46 47
    
    @Override
    public void init() {
        RecoveryManager.delayRecoveryManagerThread();
48 49
        recoveryManagerService.create();
        recoveryManagerService.start();
50 51 52 53
    }
    
    @Override
    public void registerRecoveryResource(final String dataSourceName, final XADataSource xaDataSource) {
54
        xaRecoveryModule.addXAResourceRecoveryHelper(new DataSourceXAResourceRecoveryHelper(xaDataSource));
55 56 57 58
    }
    
    @Override
    public void removeRecoveryResource(final String dataSourceName, final XADataSource xaDataSource) {
59
        xaRecoveryModule.removeXAResourceRecoveryHelper(new DataSourceXAResourceRecoveryHelper(xaDataSource));
60 61
    }
    
L
Liang Zhang 已提交
62
    @SneakyThrows({SystemException.class, RollbackException.class})
63 64
    @Override
    public void enlistResource(final SingleXAResource singleXAResource) {
65
        transactionManager.getTransaction().enlistResource(singleXAResource.getDelegate());
66 67 68 69
    }
    
    @Override
    public TransactionManager getTransactionManager() {
70
        return transactionManager;
71 72 73 74
    }
    
    @Override
    public void close() throws Exception {
75 76
        recoveryManagerService.stop();
        recoveryManagerService.destroy();
77
    }
78 79 80 81 82 83 84 85 86 87 88 89
    
    @Override
    public String getType() {
        return "narayana";
    }
    
    @Override
    public Properties getProps() {
        return null;
    }
    
    @Override
kimmking's avatar
kimmking 已提交
90
    public void setProps(final Properties props) {
91 92
        
    }
93
}