Search in sources :

Example 11 with Channel

use of com.alibaba.cobar.mysql.bio.Channel in project cobar by alibaba.

the class MySQLDataSource method releaseChannel.

public void releaseChannel(Channel c) {
    // 状态检查
    if (c == null || c.isClosed()) {
        return;
    }
    // 释放资源
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        final Channel[] items = this.items;
        for (int i = 0; i < items.length; i++) {
            if (items[i] == null) {
                ++idleCount;
                --activeCount;
                c.setLastActiveTime(TimeUtil.currentTimeMillis());
                items[i] = c;
                return;
            }
        }
    } finally {
        lock.unlock();
    }
    // 关闭多余的资源
    c.close();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Channel(com.alibaba.cobar.mysql.bio.Channel)

Example 12 with Channel

use of com.alibaba.cobar.mysql.bio.Channel in project cobar by alibaba.

the class MySQLDataNode method getChannel.

/**
     * 取得数据源通道
     */
public Channel getChannel(int i) throws Exception {
    if (initSuccess) {
        Channel c = sources[i].getChannel();
        ++executeCount;
        return c;
    } else {
        throw new IllegalArgumentException("Invalid DataSource:" + i);
    }
}
Also used : Channel(com.alibaba.cobar.mysql.bio.Channel)

Example 13 with Channel

use of com.alibaba.cobar.mysql.bio.Channel in project cobar by alibaba.

the class MySQLDataNode method initSource.

private boolean initSource(MySQLDataSource ds, int size) {
    boolean success = true;
    Channel[] list = new Channel[size < ds.size() ? size : ds.size()];
    for (int i = 0; i < list.length; i++) {
        try {
            list[i] = ds.getChannel();
        } catch (Exception e) {
            success = false;
            LOGGER.warn(getMessage(ds.getIndex(), " init error."), e);
            break;
        }
    }
    for (Channel c : list) {
        if (c == null) {
            continue;
        }
        if (success) {
            c.release();
        } else {
            c.close();
        }
    }
    return success;
}
Also used : Channel(com.alibaba.cobar.mysql.bio.Channel) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Example 14 with Channel

use of com.alibaba.cobar.mysql.bio.Channel in project cobar by alibaba.

the class MySQLChannelMain method main.

public static void main(String[] args) throws Exception {
    MySQLChannelMain test = new MySQLChannelMain();
    Channel channel = test.getChannel();
    channel.close();
}
Also used : Channel(com.alibaba.cobar.mysql.bio.Channel)

Aggregations

Channel (com.alibaba.cobar.mysql.bio.Channel)14 ReentrantLock (java.util.concurrent.locks.ReentrantLock)8 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)7 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)4 IOException (java.io.IOException)4 ServerConnection (com.alibaba.cobar.server.ServerConnection)3 CobarConfig (com.alibaba.cobar.CobarConfig)2 UnknownDataNodeException (com.alibaba.cobar.exception.UnknownDataNodeException)2 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)2 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)2 Executor (java.util.concurrent.Executor)2 ConfigException (com.alibaba.cobar.config.util.ConfigException)1 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)1 ByteBuffer (java.nio.ByteBuffer)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1