Skip to content

Commit

Permalink
Fine tune Hikari connection pool and add leaked connections detection…
Browse files Browse the repository at this point in the history
… and remediation logic (#4030)

* fine tune hikari pool and free detected leaked connections

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* handle potential concurrent modification of entries

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* close connection

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* close is not needed

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

---------

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>
  • Loading branch information
iliyan-velichkov committed Jun 11, 2024
1 parent 5453f02 commit 764c441
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@
*/
package org.eclipse.dirigible.components.api.db;

import static java.text.MessageFormat.format;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.io.output.WriterOutputStream;
import org.eclipse.dirigible.commons.api.helpers.GsonHelper;
import org.eclipse.dirigible.components.data.management.helpers.DatabaseMetadataHelper;
Expand All @@ -37,6 +24,17 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import static java.text.MessageFormat.format;

/**
* The Class DatabaseFacade.
*/
Expand Down Expand Up @@ -77,6 +75,17 @@ public void afterPropertiesSet() throws Exception {
INSTANCE = this;
}

/**
* Gets the data sources.
*
* @return the data sources
*/
public static final String getDataSources() {
return GsonHelper.toJson(DatabaseFacade.get()
.getDatabaseDefinitionService()
.getDataSourcesNames());
}

/**
* Gets the instance.
*
Expand All @@ -95,26 +104,6 @@ public DatabaseDefinitionService getDatabaseDefinitionService() {
return databaseDefinitionService;
}

/**
* Gets the data sources manager.
*
* @return the data sources manager
*/
public DataSourcesManager getDataSourcesManager() {
return dataSourcesManager;
}

/**
* Gets the data sources.
*
* @return the data sources
*/
public static final String getDataSources() {
return GsonHelper.toJson(DatabaseFacade.get()
.getDatabaseDefinitionService()
.getDataSourcesNames());
}

/**
* Gets the default data source.
*
Expand All @@ -126,6 +115,15 @@ public static final DataSource getDefaultDataSource() {
.getDefaultDataSource();
}

/**
* Gets the data sources manager.
*
* @return the data sources manager
*/
public DataSourcesManager getDataSourcesManager() {
return dataSourcesManager;
}

/**
* Gets the metadata.
*
Expand All @@ -142,6 +140,21 @@ public static final String getMetadata(String datasourceName) throws SQLExceptio
return DatabaseMetadataHelper.getMetadataAsJson(dataSource);
}

/**
* Gets the data source.
*
* @param datasourceName the datasource name
* @return the data source
*/
private static DataSource getDataSource(String datasourceName) {
return datasourceName == null || "".equals(datasourceName.trim()) || "DefaultDB".equals(datasourceName) ? DatabaseFacade.get()
.getDataSourcesManager()
.getDefaultDataSource()
: DatabaseFacade.get()
.getDataSourcesManager()
.getDataSource(datasourceName);
}

/**
* Gets the metadata.
*
Expand Down Expand Up @@ -188,23 +201,20 @@ public static final String getProductName() throws SQLException {
return DatabaseMetadataHelper.getProductName(dataSource);
}

// ============ Query ===========

/**
* Gets the data source.
* Executes SQL query.
*
* @param datasourceName the datasource name
* @return the data source
* @param sql the sql
* @param parameters the parameters
* @return the result of the query as JSON
* @throws Exception the exception
*/
private static DataSource getDataSource(String datasourceName) {
return datasourceName == null || "".equals(datasourceName.trim()) || "DefaultDB".equals(datasourceName) ? DatabaseFacade.get()
.getDataSourcesManager()
.getDefaultDataSource()
: DatabaseFacade.get()
.getDataSourcesManager()
.getDataSource(datasourceName);
public static final String query(String sql, String parameters) throws Exception {
return query(sql, parameters, null);
}

// ============ Query ===========

/**
* Executes SQL query.
*
Expand Down Expand Up @@ -250,23 +260,23 @@ public static final String query(String sql, String parameters, String datasourc
* Executes SQL query.
*
* @param sql the sql
* @param parameters the parameters
* @return the result of the query as JSON
* @throws Exception the exception
*/
public static final String query(String sql, String parameters) throws Exception {
return query(sql, parameters, null);
public static final String query(String sql) throws Exception {
return query(sql, null, null);
}

/**
* Executes SQL query.
* Executes named parameters SQL query.
*
* @param sql the sql
* @param parameters the parameters
* @return the result of the query as JSON
* @throws Exception the exception
*/
public static final String query(String sql) throws Exception {
return query(sql, null, null);
public static final String queryNamed(String sql, String parameters) throws Exception {
return queryNamed(sql, parameters, null);
}

/**
Expand Down Expand Up @@ -310,18 +320,6 @@ public static final String queryNamed(String sql, String parameters, String data
}
}

/**
* Executes named parameters SQL query.
*
* @param sql the sql
* @param parameters the parameters
* @return the result of the query as JSON
* @throws Exception the exception
*/
public static final String queryNamed(String sql, String parameters) throws Exception {
return queryNamed(sql, parameters, null);
}

/**
* Executes named parameters SQL query.
*
Expand Down Expand Up @@ -413,6 +411,18 @@ public static final List<Long> insertNamed(String sql, String parameters, String

// =========== Update ===========

/**
* Executes SQL update.
*
* @param sql the sql
* @param parameters the parameters
* @return the number of the rows that has been changed
* @throws Exception the exception
*/
public static final int update(String sql, String parameters) throws Exception {
return update(sql, parameters, null);
}

/**
* Executes SQL update.
*
Expand Down Expand Up @@ -442,18 +452,6 @@ public static final int update(String sql, String parameters, String datasourceN
}
}

/**
* Executes SQL update.
*
* @param sql the sql
* @param parameters the parameters
* @return the number of the rows that has been changed
* @throws Exception the exception
*/
public static final int update(String sql, String parameters) throws Exception {
return update(sql, parameters, null);
}

/**
* Executes SQL update.
*
Expand Down Expand Up @@ -517,32 +515,36 @@ public static final int updateNamed(String sql) throws Exception {
return update(sql, null, null);
}



/**
* Gets the connection.
*
* @param datasourceName the datasource name
* @return the connection
* @throws SQLException the SQL exception
*/
public static final Connection getConnection(String datasourceName) throws SQLException {
DataSource dataSource = getDataSource(datasourceName);
if (dataSource == null) {
String error = format("DataSource {0} not known.", datasourceName);
throw new IllegalArgumentException(error);
}
return dataSource.getConnection();
public static final Connection getConnection() throws SQLException {
return getConnection(null);
}

/**
* Gets the connection.
*
* @param datasourceName the datasource name
* @return the connection
* @throws SQLException the SQL exception
*/
public static final Connection getConnection() throws SQLException {
return getConnection(null);
public static final Connection getConnection(String datasourceName) throws SQLException {
DataSource dataSource = getDataSource(datasourceName);
if (dataSource == null) {
String error = format("DataSource {0} not known.", datasourceName);
throw new IllegalArgumentException(error);
}
try {
return dataSource.getConnection();
} catch (RuntimeException | SQLException ex) {
String errorMessage = "Failed to get connection from datasource: " + datasourceName;
logger.error(errorMessage, ex); // log it here because the client may handle the exception and hide the details.
throw new SQLException(errorMessage, ex);
}
}

// ========= Sequence ===========
Expand All @@ -558,18 +560,6 @@ public static long nextval(String sequence) throws SQLException {
return nextval(sequence, null, null);
}

/**
* Nextval.
*
* @param sequence the sequence
* @param datasourceName the datasource name
* @return the long
* @throws SQLException the SQL exception
*/
public static long nextval(String sequence, String datasourceName) throws SQLException {
return nextval(sequence, datasourceName, null);
}

/**
* Nextval.
*
Expand Down Expand Up @@ -668,6 +658,29 @@ private static void createSequenceInternal(String sequence, Integer sequenceStar
}
}

/**
* Nextval.
*
* @param sequence the sequence
* @param datasourceName the datasource name
* @return the long
* @throws SQLException the SQL exception
*/
public static long nextval(String sequence, String datasourceName) throws SQLException {
return nextval(sequence, datasourceName, null);
}

/**
* Creates the sequence.
*
* @param sequence the sequence
* @param start the start
* @throws SQLException the SQL exception
*/
public static void createSequence(String sequence, Integer start) throws SQLException {
createSequence(sequence, null, null);
}

/**
* Creates the sequence.
*
Expand Down Expand Up @@ -695,21 +708,20 @@ public static final void createSequence(String sequence, Integer start, String d
* Creates the sequence.
*
* @param sequence the sequence
* @param start the start
* @throws SQLException the SQL exception
*/
public static void createSequence(String sequence, Integer start) throws SQLException {
public static void createSequence(String sequence) throws SQLException {
createSequence(sequence, null, null);
}

/**
* Creates the sequence.
* Drop sequence.
*
* @param sequence the sequence
* @throws SQLException the SQL exception
*/
public static void createSequence(String sequence) throws SQLException {
createSequence(sequence, null, null);
public static void dropSequence(String sequence) throws SQLException {
dropSequence(sequence, null);
}

/**
Expand Down Expand Up @@ -739,16 +751,6 @@ public static final void dropSequence(String sequence, String datasourceName) th
}
}

/**
* Drop sequence.
*
* @param sequence the sequence
* @throws SQLException the SQL exception
*/
public static void dropSequence(String sequence) throws SQLException {
dropSequence(sequence, null);
}

// =========== SQL ===========

/**
Expand Down
Loading

0 comments on commit 764c441

Please sign in to comment.