Search in sources :

Example 6 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class MasterSlaveDataSourceTest method assertGetDatabaseProductName.

@Test
public void assertGetDatabaseProductName() throws SQLException {
    DataSource masterDataSource = mock(DataSource.class);
    DataSource slaveDataSource1 = mock(DataSource.class);
    DataSource slaveDataSource2 = mock(DataSource.class);
    Connection masterConnection = mockConnection("H2");
    Connection slaveConnection1 = mockConnection("H2");
    Connection slaveConnection2 = mockConnection("H2");
    when(masterDataSource.getConnection()).thenReturn(masterConnection);
    when(slaveDataSource1.getConnection()).thenReturn(slaveConnection1);
    when(slaveDataSource2.getConnection()).thenReturn(slaveConnection2);
    assertThat(((MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource("ds", masterDataSource, slaveDataSource1, slaveDataSource2)).getDatabaseProductName(), is("H2"));
    verify(masterConnection).close();
    verify(slaveConnection1).close();
    verify(slaveConnection2).close();
}
Also used : Connection(java.sql.Connection) TestDataSource(com.dangdang.ddframe.rdb.sharding.fixture.TestDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 7 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class ShardingConnectionTest method setUp.

@Before
public void setUp() {
    Map<String, DataSource> dataSourceMap = new HashMap<>(1);
    dataSourceMap.put(DS_NAME, MASTER_SLAVE_DATA_SOURCE);
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    ShardingRule rule = new ShardingRule.ShardingRuleBuilder().dataSourceRule(dataSourceRule).tableRules(Collections.singleton(new TableRule.TableRuleBuilder("test").dataSourceRule(dataSourceRule).build())).build();
    ShardingContext sc = new ShardingContext(rule, null, null);
    connection = new ShardingConnection(sc);
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) TestDataSource(com.dangdang.ddframe.rdb.sharding.fixture.TestDataSource) DataSource(javax.sql.DataSource) Before(org.junit.Before)

Example 8 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class ShardingDataSourceTest method createShardingDataSource.

private ShardingDataSource createShardingDataSource(final DataSource dataSource) {
    Map<String, DataSource> dataSourceMap = new HashMap<>(1);
    dataSourceMap.put("ds", dataSource);
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    TableRule tableRule = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(dataSourceRule).build();
    return new ShardingDataSource(ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Collections.singletonList(tableRule)).build());
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) DataSource(javax.sql.DataSource)

Example 9 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class DatabaseTest method setRouteRuleContext.

@Before
public void setRouteRuleContext() {
    Map<String, DataSource> dataSourceMap = new HashMap<>();
    dataSourceMap.put("ds_0", null);
    dataSourceMap.put("ds_1", null);
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).databaseShardingStrategy(new DatabaseShardingStrategy(new OrderDatabaseShardingAlgorithm())).build();
}
Also used : DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) OrderDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.router.fixture.OrderDatabaseShardingAlgorithm) DataSource(javax.sql.DataSource) Before(org.junit.Before)

Example 10 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class YamlIntegratedTest method testWithDataSource.

@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
    File yamlFile = new File(YamlIntegratedTest.class.getResource(filePath).toURI());
    DataSource dataSource;
    if (hasDataSource) {
        dataSource = new YamlShardingDataSource(yamlFile);
    } else {
        dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {

            @Override
            public DataSource apply(final String key) {
                return createDataSource(key);
            }
        }), yamlFile);
    }
    try (Connection conn = dataSource.getConnection();
        Statement stm = conn.createStatement()) {
        stm.executeQuery("SELECT * FROM t_order");
        stm.executeQuery("SELECT * FROM t_order_item");
        stm.executeQuery("SELECT * FROM config");
    }
}
Also used : Statement(java.sql.Statement) YamlShardingDataSource(com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource) Connection(java.sql.Connection) File(java.io.File) YamlShardingDataSource(com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21