001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.jdbc.metadata;
018
019import javax.sql.DataSource;
020
021import com.zaxxer.hikari.HikariDataSource;
022import com.zaxxer.hikari.pool.HikariPool;
023
024import org.springframework.beans.DirectFieldAccessor;
025
026/**
027 * {@link DataSourcePoolMetadata} for a Hikari {@link DataSource}.
028 *
029 * @author Stephane Nicoll
030 * @since 2.0.0
031 */
032public class HikariDataSourcePoolMetadata
033                extends AbstractDataSourcePoolMetadata<HikariDataSource> {
034
035        public HikariDataSourcePoolMetadata(HikariDataSource dataSource) {
036                super(dataSource);
037        }
038
039        @Override
040        public Integer getActive() {
041                try {
042                        return getHikariPool().getActiveConnections();
043                }
044                catch (Exception ex) {
045                        return null;
046                }
047        }
048
049        private HikariPool getHikariPool() {
050                return (HikariPool) new DirectFieldAccessor(getDataSource())
051                                .getPropertyValue("pool");
052        }
053
054        @Override
055        public Integer getMax() {
056                return getDataSource().getMaximumPoolSize();
057        }
058
059        @Override
060        public Integer getMin() {
061                return getDataSource().getMinimumIdle();
062        }
063
064        @Override
065        public String getValidationQuery() {
066                return getDataSource().getConnectionTestQuery();
067        }
068
069        @Override
070        public Boolean getDefaultAutoCommit() {
071                return getDataSource().isAutoCommit();
072        }
073
074}