Search in sources :

Example 1 with MasterSlaveDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.

the class ShardingConnection method getConnection.

/**
 * Get database connection via data source name.
 *
 * @param dataSourceName data source name
 * @param sqlType SQL type
 * @return all database connections via data source name
 * @throws SQLException SQL exception
 */
public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException {
    if (getCachedConnections().containsKey(dataSourceName)) {
        return getCachedConnections().get(dataSourceName);
    }
    DataSource dataSource = shardingContext.getDataSourceMap().get(dataSourceName);
    Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
    String realDataSourceName;
    if (dataSource instanceof MasterSlaveDataSource) {
        NamedDataSource namedDataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType);
        realDataSourceName = namedDataSource.getName();
        if (getCachedConnections().containsKey(realDataSourceName)) {
            return getCachedConnections().get(realDataSourceName);
        }
        dataSource = namedDataSource.getDataSource();
    } else {
        realDataSourceName = dataSourceName;
    }
    Connection result = dataSource.getConnection();
    getCachedConnections().put(realDataSourceName, result);
    replayMethodsInvocation(result);
    return result;
}
Also used : MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) Connection(java.sql.Connection) NamedDataSource(io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource) DataSource(javax.sql.DataSource) NamedDataSource(io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)

Example 2 with MasterSlaveDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.

the class SpringBootMasterSlaveTest method assertWithMasterSlaveDataSource.

@Test
public void assertWithMasterSlaveDataSource() {
    assertTrue(dataSource instanceof MasterSlaveDataSource);
    for (DataSource each : ((MasterSlaveDataSource) dataSource).getAllDataSources().values()) {
        assertThat(((BasicDataSource) each).getMaxTotal(), is(16));
    }
    Map<String, Object> configMap = new ConcurrentHashMap<>();
    configMap.put("key1", "value1");
    assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap));
}
Also used : MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with MasterSlaveDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.

the class OrchestrationFacade method getActualDataSourceMapForMasterSlave.

private Map<String, DataSource> getActualDataSourceMapForMasterSlave(final Map<String, DataSource> dataSourceMap) {
    Map<String, DataSource> result = new LinkedHashMap<>();
    for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
        if (entry.getValue() instanceof MasterSlaveDataSource) {
            MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue();
            result.putAll(masterSlaveDataSource.getAllDataSources());
        } else {
            result.put(entry.getKey(), entry.getValue());
        }
    }
    return result;
}
Also used : MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with MasterSlaveDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.

the class ShardingConnectionTest method init.

@BeforeClass
public static void init() throws SQLException {
    DataSource masterDataSource = new TestDataSource("test_ds_master");
    DataSource slaveDataSource = new TestDataSource("test_ds_slave");
    Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
    dataSourceMap.put("test_ds_master", masterDataSource);
    dataSourceMap.put("test_ds_slave", slaveDataSource);
    masterSlaveDataSource = new MasterSlaveDataSource(dataSourceMap, new MasterSlaveRuleConfiguration("test_ds", "test_ds_master", Collections.singletonList("test_ds_slave")), Collections.<String, Object>emptyMap());
    ((TestDataSource) slaveDataSource).setThrowExceptionWhenClosing(true);
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) HashMap(java.util.HashMap) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) BeforeClass(org.junit.BeforeClass)

Example 5 with MasterSlaveDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.

the class ShardingConnectionTest method setUp.

@Before
public void setUp() {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("test");
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
    dataSourceMap.put(DS_NAME, masterSlaveDataSource);
    ShardingContext shardingContext = new ShardingContext(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), null, null, false);
    connection = new ShardingConnection(shardingContext);
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) HashMap(java.util.HashMap) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext) Before(org.junit.Before)

Aggregations

MasterSlaveDataSource (io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)9 DataSource (javax.sql.DataSource)9 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)3 HashMap (java.util.HashMap)3 TestDataSource (io.shardingjdbc.core.fixture.TestDataSource)2 NamedDataSource (io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource)2 Connection (java.sql.Connection)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MasterSlaveRuleConfiguration (io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration)1 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)1 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)1 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)1 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1