91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sharding-jdbc中ShardingTransactionManager有什么用

發布時間:2021-06-22 15:21:01 來源:億速云 閱讀:296 作者:Leah 欄目:大數據

這篇文章將為大家詳細講解有關sharding-jdbc中ShardingTransactionManager有什么用,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

ShardingTransactionManager

incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-core/src/main/java/org/apache/shardingsphere/transaction/spi/ShardingTransactionManager.java

public interface ShardingTransactionManager extends AutoCloseable {
    
    /**
     * Initialize sharding transaction manager.
     *
     * @param databaseType database type
     * @param resourceDataSources resource data sources
     */
    void init(DatabaseType databaseType, Collection<ResourceDataSource> resourceDataSources);
    
    /**
     * Get transaction type.
     *
     * @return transaction type
     */
    TransactionType getTransactionType();
    
    /**
     * Judge is in transaction or not.
     * 
     * @return in transaction or not
     */
    boolean isInTransaction();
    
    /**
     * Get transactional connection.
     *
     * @param dataSourceName data source name
     * @return connection
     * @throws SQLException SQL exception
     */
    Connection getConnection(String dataSourceName) throws SQLException;
    
    /**
     * Begin transaction.
     */
    void begin();
    
    /**
     * Commit transaction.
     */
    void commit();
    
    /**
     * Rollback transaction.
     */
    void rollback();
}
  • ShardingTransactionManager繼承了AutoCloseable接口,它定義了init、getTransactionType、isInTransaction、getConnection、begin、commit、rollback方法

XAShardingTransactionManager

incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManager.java

public final class XAShardingTransactionManager implements ShardingTransactionManager {
    
    private final Map<String, SingleXADataSource> singleXADataSourceMap = new HashMap<>();
    
    private final XATransactionManager xaTransactionManager = XATransactionManagerLoader.getInstance().getTransactionManager();
    
    private ThreadLocal<List<String>> enlistedXAResource = new ThreadLocal<List<String>>() {
        @Override
        public List<String> initialValue() {
            return new LinkedList<>();
        }
    };
    
    @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();
        if (!enlistedXAResource.get().contains(dataSourceName)) {
            xaTransactionManager.enlistResource(singleXAConnection.getXAResource());
            enlistedXAResource.get().add(dataSourceName);
        }
        return singleXAConnection.getConnection();
    }
    
    @SneakyThrows
    @Override
    public void begin() {
        xaTransactionManager.getTransactionManager().begin();
    }
    
    @SneakyThrows
    @Override
    public void commit() {
        try {
            xaTransactionManager.getTransactionManager().commit();
        } finally {
            enlistedXAResource.remove();
        }
    }
    
    @SneakyThrows
    @Override
    public void rollback() {
        try {
            xaTransactionManager.getTransactionManager().rollback();
        } finally {
            enlistedXAResource.remove();
        }
    }
    
    @Override
    public void close() throws Exception {
        for (SingleXADataSource each : singleXADataSourceMap.values()) {
            xaTransactionManager.removeRecoveryResource(each.getResourceName(), each.getXaDataSource());
        }
        singleXADataSourceMap.clear();
        xaTransactionManager.close();
        enlistedXAResource = null;
    }
}
  • XAShardingTransactionManager實現了ShardingTransactionManager接口,它擁有singleXADataSourceMap、xaTransactionManager、enlistedXAResource三個屬性,其init方法對這三個屬性進行了初始化

  • getTransactionType方法返回的是TransactionType.XA;isInTransaction方法則依據xaTransactionManager.getTransactionManager().getStatus()來判斷;getConnection方法從singleXADataSourceMap取出singleXAConnection,然后執行其getConnection方法

  • begin方法執行的是xaTransactionManager.getTransactionManager().begin();commit方法則調用了xaTransactionManager.getTransactionManager().commit(),其會在finally中執行enlistedXAResource.remove();rollback方法調用了xaTransactionManager.getTransactionManager().rollback(),其也在finally中執行enlistedXAResource.remove();close方法則執行xaTransactionManager.removeRecoveryResource、singleXADataSourceMap.clear()、xaTransactionManager.close()

XAShardingTransactionManagerTest

incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingTransactionManagerTest.java

@RunWith(MockitoJUnitRunner.class)
public final class XAShardingTransactionManagerTest {
    
    private XAShardingTransactionManager xaShardingTransactionManager = new XAShardingTransactionManager();
    
    @Mock
    private XATransactionManager xaTransactionManager;
    
    @Mock
    private TransactionManager transactionManager;
    
    @Before
    public void setUp() {
        when(xaTransactionManager.getTransactionManager()).thenReturn(transactionManager);
        ReflectiveUtil.setProperty(xaShardingTransactionManager, "xaTransactionManager", xaTransactionManager);
    }
    
    @Test
    public void assertGetTransactionType() {
        assertThat(xaShardingTransactionManager.getTransactionType(), is(TransactionType.XA));
    }
    
    @Test
    public void assertRegisterXATransactionalDataSources() {
        Collection<ResourceDataSource> resourceDataSources = createResourceDataSources(DruidXADataSource.class, DatabaseType.MySQL);
        xaShardingTransactionManager.init(DatabaseType.MySQL, resourceDataSources);
        for (ResourceDataSource each : resourceDataSources) {
            verify(xaTransactionManager).registerRecoveryResource(each.getUniqueResourceName(), (XADataSource) each.getDataSource());
        }
    }
    
    @Test
    public void assertRegisterAtomikosDataSourceBeans() {
        xaShardingTransactionManager.init(DatabaseType.MySQL, createAtomikosDataSourceBeanResource());
        verify(xaTransactionManager, times(0)).registerRecoveryResource(anyString(), any(XADataSource.class));
    }
    
    @Test
    public void assertRegisterNoneXATransactionalDAtaSources() {
        Collection<ResourceDataSource> resourceDataSources = createResourceDataSources(HikariDataSource.class, DatabaseType.MySQL);
        xaShardingTransactionManager.init(DatabaseType.MySQL, resourceDataSources);
        Map<String, SingleXADataSource> cachedXADatasourceMap = getCachedSingleXADataSourceMap();
        assertThat(cachedXADatasourceMap.size(), is(2));
    }
    
    @Test
    public void assertIsInTransaction() throws SystemException {
        when(transactionManager.getStatus()).thenReturn(Status.STATUS_ACTIVE);
        assertTrue(xaShardingTransactionManager.isInTransaction());
    }
    
    @Test
    public void assertIsNotInTransaction() throws SystemException {
        when(transactionManager.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
        assertFalse(xaShardingTransactionManager.isInTransaction());
    }
    
    @Test
    public void assertGetConnection() {
        setCachedSingleXADataSourceMap("ds1");
        Connection actual = xaShardingTransactionManager.getConnection("ds1");
        assertThat(actual, instanceOf(Connection.class));
        verify(xaTransactionManager).enlistResource(any(SingleXAResource.class));
    }
    
    @Test
    public void assertGetConnectionWithoutEnlist() {
        setCachedSingleXADataSourceMap("ds1");
        Connection actual = xaShardingTransactionManager.getConnection("ds1");
        assertThat(actual, instanceOf(Connection.class));
        xaShardingTransactionManager.getConnection("ds1");
        assertThat(actual, instanceOf(Connection.class));
        verify(xaTransactionManager).enlistResource(any(SingleXAResource.class));
    }
    
    @Test
    public void assertClose() throws Exception {
        setCachedSingleXADataSourceMap("ds1");
        xaShardingTransactionManager.close();
        Map<String, SingleXADataSource> cachedSingleXADataSourceMap = getCachedSingleXADataSourceMap();
        verify(xaTransactionManager).removeRecoveryResource(anyString(), any(XADataSource.class));
        assertThat(cachedSingleXADataSourceMap.size(), is(0));
    }
    
    @SneakyThrows
    @SuppressWarnings("unchecked")
    private Map<String, SingleXADataSource> getCachedSingleXADataSourceMap() {
        Field field = xaShardingTransactionManager.getClass().getDeclaredField("singleXADataSourceMap");
        field.setAccessible(true);
        return (Map<String, SingleXADataSource>) field.get(xaShardingTransactionManager);
    }
    
    @SneakyThrows
    private void setCachedSingleXADataSourceMap(final String datasourceName) {
        Field field = xaShardingTransactionManager.getClass().getDeclaredField("singleXADataSourceMap");
        field.setAccessible(true);
        field.set(xaShardingTransactionManager, createMockSingleXADataSourceMap(datasourceName));
    }
    
    @SneakyThrows
    private Map<String, SingleXADataSource> createMockSingleXADataSourceMap(final String datasourceName) {
        SingleXADataSource singleXADataSource = mock(SingleXADataSource.class);
        SingleXAConnection singleXAConnection = mock(SingleXAConnection.class);
        XADataSource xaDataSource = mock(XADataSource.class);
        SingleXAResource singleXAResource = mock(SingleXAResource.class);
        Connection connection = mock(Connection.class);
        when(singleXAConnection.getConnection()).thenReturn(connection);
        when(singleXAConnection.getXAResource()).thenReturn(singleXAResource);
        when(singleXADataSource.getXAConnection()).thenReturn(singleXAConnection);
        when(singleXADataSource.getResourceName()).thenReturn(datasourceName);
        when(singleXADataSource.getXaDataSource()).thenReturn(xaDataSource);
        Map<String, SingleXADataSource> result = new HashMap<>();
        result.put(datasourceName, singleXADataSource);
        return result;
    }
    
    private Collection<ResourceDataSource> createResourceDataSources(final Class<? extends DataSource> dataSourceClass, final DatabaseType databaseType) {
        List<ResourceDataSource> result = new LinkedList<>();
        result.add(new ResourceDataSource("ds1", DataSourceUtils.build(dataSourceClass, databaseType, "demo_ds_1")));
        result.add(new ResourceDataSource("ds2", DataSourceUtils.build(dataSourceClass, databaseType, "demo_ds_2")));
        return result;
    }
    
    private Collection<ResourceDataSource> createAtomikosDataSourceBeanResource() {
        List<ResourceDataSource> result = new LinkedList<>();
        result.add(new ResourceDataSource("ds1", new AtomikosDataSourceBean()));
        result.add(new ResourceDataSource("ds2", new AtomikosDataSourceBean()));
        return result;
    }
}
  • 這里mock了xaTransactionManager、transactionManager

小結

  • XAShardingTransactionManager實現了ShardingTransactionManager接口,它擁有singleXADataSourceMap、xaTransactionManager、enlistedXAResource三個屬性,其init方法對這三個屬性進行了初始化

  • getTransactionType方法返回的是TransactionType.XA;isInTransaction方法則依據xaTransactionManager.getTransactionManager().getStatus()來判斷;getConnection方法從singleXADataSourceMap取出singleXAConnection,然后執行其getConnection方法

  • begin方法執行的是xaTransactionManager.getTransactionManager().begin();commit方法則調用了xaTransactionManager.getTransactionManager().commit(),其會在finally中執行enlistedXAResource.remove();rollback方法調用了xaTransactionManager.getTransactionManager().rollback(),其也在finally中執行enlistedXAResource.remove();close方法則執行xaTransactionManager.removeRecoveryResource、singleXADataSourceMap.clear()、xaTransactionManager.close()

關于sharding-jdbc中ShardingTransactionManager有什么用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

南京市| 乌兰察布市| 麦盖提县| 张家界市| 兴业县| 兴安盟| 铜川市| 新绛县| 泗水县| 北川| 桐柏县| 大荔县| 霍邱县| 白银市| 新竹市| 孟村| 江都市| 芦山县| 安平县| 乐昌市| 大新县| 长武县| 南溪县| 东至县| 文昌市| 贵定县| 柳州市| 栖霞市| 岳西县| 克拉玛依市| 满城县| 延边| 桂平市| 饶阳县| 锡林浩特市| 陇西县| 余江县| 镇安县| 吴江市| 宜良县| 于田县|