InstanceH2CacheDAO.java 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

19
package org.skywalking.apm.collector.cache.dao;
P
pengys5 已提交
20

clevertension's avatar
clevertension 已提交
21 22
import java.sql.ResultSet;
import java.sql.SQLException;
clevertension's avatar
clevertension 已提交
23 24
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
25
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
clevertension's avatar
clevertension 已提交
26
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
P
pengys5 已提交
27
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
clevertension's avatar
clevertension 已提交
28 29
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
P
pengys5 已提交
30 31 32 33

/**
 * @author pengys5
 */
P
peng-yongsheng 已提交
34
public class InstanceH2CacheDAO extends H2DAO implements IInstanceCacheDAO {
35

P
peng-yongsheng 已提交
36
    private final Logger logger = LoggerFactory.getLogger(InstanceH2CacheDAO.class);
37 38

    private static final String GET_APPLICATION_ID_SQL = "select {0} from {1} where {2} = ?";
P
pengys5 已提交
39

40
    @Override public int getApplicationId(int applicationInstanceId) {
41
        logger.info("get the application id with application id = {}", applicationInstanceId);
clevertension's avatar
clevertension 已提交
42
        H2Client client = getClient();
43 44
        String sql = SqlBuilder.buildSql(GET_APPLICATION_ID_SQL, InstanceTable.COLUMN_APPLICATION_ID, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID);
        Object[] params = new Object[] {applicationInstanceId};
clevertension's avatar
clevertension 已提交
45 46
        try (ResultSet rs = client.executeQuery(sql, params)) {
            if (rs.next()) {
47
                return rs.getInt(InstanceTable.COLUMN_APPLICATION_ID);
clevertension's avatar
clevertension 已提交
48 49 50 51
            }
        } catch (SQLException | H2ClientException e) {
            logger.error(e.getMessage(), e);
        }
52
        return 0;
P
pengys5 已提交
53 54
    }
}