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());
}
}
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;
}
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());
}
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());
}
Aggregations