All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.dts.client.store.access.impl.CommonDaoH2 Maven / Gradle / Ivy

package com.alibaba.dts.client.store.access.impl;

import com.alibaba.dts.client.store.access.CommonDao;
import com.alibaba.dts.client.store.access.ExecutionCounterDao;
import com.alibaba.dts.common.domain.store.ExecutionCounter;
import com.alibaba.dts.common.exception.AccessException;
import com.alibaba.dts.common.logger.SchedulerXLoggerFactory;
import com.alibaba.dts.common.logger.innerlog.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Ronan Zhan
 * @date 2016/10/18.
 */
public class CommonDaoH2 implements CommonDao {

    private static final Logger logger = SchedulerXLoggerFactory.getLogger(CommonDaoH2.class);

    private DataSource dataSource;

    public CommonDaoH2(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    @Override
    public int healthCheck() throws AccessException {
        int result = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement(
                    "SELECT id FROM DTS_HEALTH_CHECK limit 1");
            preparedStatement.executeQuery();

        } catch (SQLException e) {
            throw new AccessException("[healthCheck]: error", e);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    logger.error(e.getMessage(), e);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        return result;
    }

    @Override
    public int createHealthCheckTable() throws AccessException {
        int result = 0;

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement(
                    "CREATE TABLE IF NOT EXISTS `DTS_HEALTH_CHECK` (" +
                            "        `id` bigint(20) NOT NULL AUTO_INCREMENT," +
                            "        PRIMARY KEY (`id`)" +
                            "        ) ; ");
            result = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            throw new AccessException("[createHealthCheckTable]: error", e);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    logger.error(e.getMessage(), e);
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy