Search in sources :

Example 1 with DataSourceIdentity

use of com.ctrip.platform.dal.dao.datasource.DataSourceIdentity in project dal by ctripcorp.

the class DalConnectionManager method getConnectionFromDSLocator.

private DalConnection getConnectionFromDSLocator(DalHints hints, boolean isMaster, boolean isSelect, ConnectionAction action) throws SQLException {
    Connection conn;
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    String shardId = null;
    if (dbSet.isShardingSupported()) {
        DalShardingStrategy strategy = dbSet.getStrategy();
        // In case the sharding strategy indicate that master shall be used
        isMaster |= strategy.isMaster(config, logicDbName, hints);
        shardId = hints.getShardId();
        if (shardId == null)
            shardId = strategy.locateDbShard(config, logicDbName, hints);
        if (shardId == null)
            throw new DalException(ErrorCode.ShardLocated, logicDbName);
        dbSet.validate(shardId);
    }
    DataBase selectedDataBase = select(logicDbName, dbSet, hints, shardId, isMaster, isSelect);
    try {
        DbMeta meta;
        if (selectedDataBase instanceof ClusterDataBase) {
            ClusterDataBase clusterDataBase = (ClusterDataBase) selectedDataBase;
            conn = locator.getConnection(clusterDataBase, action);
            meta = DbMeta.createIfAbsent(clusterDataBase, dbSet.getDatabaseCategory(), conn);
            if (shardId == null)
                shardId = clusterDataBase.getSharding();
        } else if (selectedDataBase instanceof ProviderDataBase) {
            DataSourceIdentity id = selectedDataBase.getDataSourceIdentity();
            conn = locator.getConnection(id, action);
            meta = DbMeta.createIfAbsent(id, dbSet.getDatabaseCategory(), conn);
        } else {
            String allInOneKey = selectedDataBase.getConnectionString();
            conn = locator.getConnection(allInOneKey, action);
            meta = DbMeta.createIfAbsent(allInOneKey, dbSet.getDatabaseCategory(), conn);
        }
        return new DalConnection(conn, selectedDataBase.isMaster(), shardId, meta);
    } catch (Throwable e) {
        throw new DalException(ErrorCode.CantGetConnection, e, selectedDataBase.getConnectionString());
    }
}
Also used : DataSourceIdentity(com.ctrip.platform.dal.dao.datasource.DataSourceIdentity) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) DalException(com.ctrip.platform.dal.exceptions.DalException) Connection(java.sql.Connection)

Example 2 with DataSourceIdentity

use of com.ctrip.platform.dal.dao.datasource.DataSourceIdentity in project dal by ctripcorp.

the class DalConnectionPool method createConnection.

@Override
protected PooledConnection createConnection(long now, PooledConnection notUsed, String username, String password) throws SQLException {
    long startTime = System.currentTimeMillis();
    PooledConnection pooledConnection;
    PoolConfiguration poolConfig = getPoolProperties();
    DataSourceIdentity dataSourceId = poolConfig instanceof DalExtendedPoolConfiguration ? ((DalExtendedPoolConfiguration) poolConfig).getDataSourceId() : null;
    try {
        pooledConnection = super.createConnection(now, notUsed, username, password);
    } catch (Throwable e) {
        String connectionUrl = LoggerHelper.getSimplifiedDBUrl(getPoolProperties().getUrl());
        connectionListener.onCreateConnectionFailed(getName(), connectionUrl, dataSourceId, e, startTime);
        throw e;
    }
    try {
        connectionListener.onCreateConnection(getName(), getConnection(pooledConnection), dataSourceId, startTime);
    } catch (Throwable e) {
        logger.error("[createConnection]" + this, e);
    }
    preHandleConnection(pooledConnection, true);
    return pooledConnection;
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration) DataSourceIdentity(com.ctrip.platform.dal.dao.datasource.DataSourceIdentity) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)

Example 3 with DataSourceIdentity

use of com.ctrip.platform.dal.dao.datasource.DataSourceIdentity in project dal by ctripcorp.

the class ClusterDatabaseSetTest method testNonShardingCluster.

@Test
public void testNonShardingCluster() {
    ClusterConfigProvider provider = new DefaultLocalConfigProvider("NonShardingCluster");
    // todo-lhj
    ClusterConfig config = provider.getClusterConfig(new DefaultDalConfigCustomizedOption());
    Cluster cluster = config.generate();
    ClusterDatabaseSet databaseSet = new ClusterDatabaseSet("NonShardingCluster", cluster, new DalConnectionLocator() {

        @Override
        public void setup(Collection<DatabaseSet> databaseSets) {
        }

        @Override
        public Connection getConnection(String name) throws Exception {
            return null;
        }

        @Override
        public Connection getConnection(String name, ConnectionAction action) throws Exception {
            return null;
        }

        @Override
        public Connection getConnection(DataSourceIdentity id) throws Exception {
            return null;
        }

        @Override
        public Connection getConnection(DataSourceIdentity id, ConnectionAction action) throws Exception {
            return null;
        }

        @Override
        public IntegratedConfigProvider getIntegratedConfigProvider() {
            return null;
        }

        @Override
        public void setupCluster(Cluster cluster) {
        }

        @Override
        public void uninstallCluster(Cluster cluster) {
        }

        @Override
        public void initialize(Map<String, String> settings) throws Exception {
        }
    });
    Assert.assertFalse(databaseSet.isShardingSupported());
    Assert.assertEquals(1, databaseSet.getMasterDbs().size());
    Assert.assertEquals(0, databaseSet.getSlaveDbs().size());
}
Also used : DataSourceIdentity(com.ctrip.platform.dal.dao.datasource.DataSourceIdentity) Connection(java.sql.Connection) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) ClusterConfigProvider(com.ctrip.framework.dal.cluster.client.config.ClusterConfigProvider) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) DefaultLocalConfigProvider(com.ctrip.framework.dal.cluster.client.config.DefaultLocalConfigProvider) ClusterConfig(com.ctrip.framework.dal.cluster.client.config.ClusterConfig) DalConnectionLocator(com.ctrip.platform.dal.dao.client.DalConnectionLocator) Test(org.junit.Test)

Example 4 with DataSourceIdentity

use of com.ctrip.platform.dal.dao.datasource.DataSourceIdentity in project dal by ctripcorp.

the class DalConnectionPoolTest method simpleTest.

/**
 * need local mysql
 *
 * @throws SQLException
 */
@Test
public void simpleTest() throws SQLException {
    int initSize = 5;
    PoolConfiguration p = new PoolProperties();
    p.setUrl("jdbc:mysql://localhost:3306/test?a=1,b=2");
    p.setDriverClassName("com.mysql.jdbc.Driver");
    p.setUsername("root");
    p.setPassword("root");
    p.setInitialSize(initSize);
    final AtomicInteger create = new AtomicInteger();
    final AtomicInteger release = new AtomicInteger();
    final AtomicInteger abandon = new AtomicInteger();
    DalConnectionPool.setConnectionListener(new AbstractConnectionListener() {

        @Override
        public void doOnCreateConnection(String poolDesc, Connection connection, DataSourceIdentity dataSourceId, long startTime) {
            create.incrementAndGet();
        }

        @Override
        public void doOnReleaseConnection(String poolDesc, Connection connection) {
            release.incrementAndGet();
        }

        @Override
        protected void doOnAbandonConnection(String poolDesc, Connection connection) {
            abandon.incrementAndGet();
        }

        @Override
        public int getOrder() {
            return 0;
        }
    });
    DataSource dataSource = new DalTomcatDataSource(p);
    ConnectionPool dalConnectionPool = dataSource.createPool();
    Assert.assertEquals(initSize, create.get());
    List<Connection> connections = new LinkedList<>();
    for (int i = 0; i < initSize; i++) {
        connections.add(dalConnectionPool.getConnection());
    }
    Assert.assertEquals(initSize, create.get());
    for (int i = 0; i < initSize; i++) {
        connections.add(dalConnectionPool.getConnection());
    }
    Assert.assertEquals(initSize * 2, create.get());
    for (Connection connection : connections) {
        connection.close();
    }
    Assert.assertEquals(initSize * 2, create.get());
    Assert.assertEquals(0, release.get());
    dalConnectionPool.purge();
    Assert.assertEquals(initSize * 2, create.get());
    Assert.assertEquals(initSize * 2, release.get());
}
Also used : DataSourceIdentity(com.ctrip.platform.dal.dao.datasource.DataSourceIdentity) Connection(java.sql.Connection) AbstractConnectionListener(com.ctrip.platform.dal.dao.datasource.AbstractConnectionListener) LinkedList(java.util.LinkedList) RefreshableDataSource(com.ctrip.platform.dal.dao.datasource.RefreshableDataSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

DataSourceIdentity (com.ctrip.platform.dal.dao.datasource.DataSourceIdentity)4 Connection (java.sql.Connection)3 Test (org.junit.Test)2 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 ClusterConfigProvider (com.ctrip.framework.dal.cluster.client.config.ClusterConfigProvider)1 DefaultLocalConfigProvider (com.ctrip.framework.dal.cluster.client.config.DefaultLocalConfigProvider)1 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)1 DalConnectionLocator (com.ctrip.platform.dal.dao.client.DalConnectionLocator)1 DalExtendedPoolConfiguration (com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)1 AbstractConnectionListener (com.ctrip.platform.dal.dao.datasource.AbstractConnectionListener)1 RefreshableDataSource (com.ctrip.platform.dal.dao.datasource.RefreshableDataSource)1 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)1 DalException (com.ctrip.platform.dal.exceptions.DalException)1 LinkedList (java.util.LinkedList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)1 PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)1