3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.monitoring;
22 import org.apache.commons.dbcp2.BasicDataSource;
23 import org.sonar.api.SonarQubeSide;
24 import org.sonar.api.SonarRuntime;
25 import org.sonar.db.DbClient;
26 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section;
27 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
29 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
32 * Information about database connection pool
34 public class DbConnectionSection extends BaseSectionMBean implements DbConnectionSectionMBean {
36 private final DatabaseVersion dbVersion;
37 private final DbClient dbClient;
38 private final SonarRuntime runtime;
40 public DbConnectionSection(DatabaseVersion dbVersion, DbClient dbClient, SonarRuntime runtime) {
41 this.dbVersion = dbVersion;
42 this.dbClient = dbClient;
43 this.runtime = runtime;
47 public String name() {
52 public String getMigrationStatus() {
53 return dbVersion.getStatus().name();
57 public int getPoolActiveConnections() {
58 return commonsDbcp().getNumActive();
62 public int getPoolMaxActiveConnections() {
63 return commonsDbcp().getMaxTotal();
67 public int getPoolIdleConnections() {
68 return commonsDbcp().getNumIdle();
72 public int getPoolMaxIdleConnections() {
73 return commonsDbcp().getMaxIdle();
77 public int getPoolMinIdleConnections() {
78 return commonsDbcp().getMinIdle();
82 public int getPoolInitialSize() {
83 return commonsDbcp().getInitialSize();
87 public long getPoolMaxWaitMillis() {
88 return commonsDbcp().getMaxWaitMillis();
92 public boolean getPoolRemoveAbandoned() {
93 return commonsDbcp().getRemoveAbandonedOnBorrow();
97 public int getPoolRemoveAbandonedTimeoutSeconds() {
98 return commonsDbcp().getRemoveAbandonedTimeout();
102 public Section toProtobuf() {
103 Section.Builder protobuf = Section.newBuilder();
104 String side = runtime.getSonarQubeSide() == SonarQubeSide.COMPUTE_ENGINE ? "Compute Engine" : "Web";
105 protobuf.setName(side + " Database Connection");
106 completePoolAttributes(protobuf);
107 return protobuf.build();
110 private void completePoolAttributes(Section.Builder protobuf) {
111 setAttribute(protobuf, "Pool Active Connections", getPoolActiveConnections());
112 setAttribute(protobuf, "Pool Max Connections", getPoolMaxActiveConnections());
113 setAttribute(protobuf, "Pool Initial Size", getPoolInitialSize());
114 setAttribute(protobuf, "Pool Idle Connections", getPoolIdleConnections());
115 setAttribute(protobuf, "Pool Min Idle Connections", getPoolMinIdleConnections());
116 setAttribute(protobuf, "Pool Max Idle Connections", getPoolMaxIdleConnections());
117 setAttribute(protobuf, "Pool Max Wait (ms)", getPoolMaxWaitMillis());
118 setAttribute(protobuf, "Pool Remove Abandoned", getPoolRemoveAbandoned());
119 setAttribute(protobuf, "Pool Remove Abandoned Timeout (seconds)", getPoolRemoveAbandonedTimeoutSeconds());
122 private BasicDataSource commonsDbcp() {
123 return (BasicDataSource) dbClient.getDatabase().getDataSource();