Skip to content

Commit 7997095

Browse files
committed
Upgrade mysql and snowflake drivers.
Improve connection management, support mysql on windows.
1 parent 39a8146 commit 7997095

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

dqops/src/main/java/com/dqops/connectors/jdbc/AbstractJdbcSourceConnection.java

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.dqops.utils.exceptions.RunSilently;
2626
import com.zaxxer.hikari.HikariConfig;
2727
import com.zaxxer.hikari.HikariDataSource;
28+
import lombok.extern.slf4j.Slf4j;
2829
import tech.tablesaw.api.Table;
2930
import tech.tablesaw.columns.Column;
3031

@@ -37,6 +38,7 @@
3738
/**
3839
* Base abstract class for JDBC based source connections.
3940
*/
41+
@Slf4j
4042
public abstract class AbstractJdbcSourceConnection extends AbstractSqlSourceConnection {
4143
private final JdbcConnectionPool jdbcConnectionPool;
4244
private Connection jdbcConnection;
@@ -85,6 +87,17 @@ public void open(SecretValueLookupContext secretValueLookupContext) {
8587
this.jdbcConnection.setAutoCommit(true);
8688
}
8789
catch (Exception ex) {
90+
if (this.jdbcConnection != null) {
91+
try {
92+
this.jdbcConnection.close();
93+
}
94+
catch (SQLException sqlException) {
95+
log.error("Cannot close a connection during a failure, error: " + sqlException.getMessage(), sqlException);
96+
} finally {
97+
this.jdbcConnection = null;
98+
}
99+
}
100+
88101
if (this.getConnectionSpec().getHierarchyId() != null) {
89102
String connectionName = this.getConnectionSpec().getConnectionName();
90103
throw new JdbcConnectionFailedException("Connection failed for source " + connectionName + ", error: " + ex.getMessage(), ex);

dqops/src/main/java/com/dqops/connectors/jdbc/JdbcConnectionPoolImpl.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import com.dqops.metadata.sources.ConnectionSpec;
2020
import com.google.common.cache.Cache;
2121
import com.google.common.cache.CacheBuilder;
22+
import com.google.common.cache.RemovalNotification;
2223
import com.zaxxer.hikari.HikariConfig;
2324
import com.zaxxer.hikari.HikariDataSource;
25+
import lombok.extern.slf4j.Slf4j;
2426
import org.springframework.beans.factory.annotation.Autowired;
2527
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2628
import org.springframework.context.annotation.Scope;
@@ -31,10 +33,11 @@
3133
import java.util.concurrent.TimeUnit;
3234

3335
/**
34-
* JDDB connection pool that supports multiple connections.
36+
* JDBC connection pool that supports multiple connections.
3537
*/
3638
@Component
3739
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
40+
@Slf4j
3841
public class JdbcConnectionPoolImpl implements JdbcConnectionPool {
3942
/**
4043
* Data sources cache.
@@ -51,9 +54,24 @@ public JdbcConnectionPoolImpl(DqoJdbcConnectionsConfigurationProperties jdbcConn
5154
CacheBuilder.newBuilder()
5255
.maximumSize(jdbcConnectionsConfigurationProperties.getMaxConnectionInPool())
5356
.expireAfterAccess(jdbcConnectionsConfigurationProperties.getExpireAfterAccessSeconds(), TimeUnit.SECONDS)
57+
.removalListener(notification -> onRemoveDataSource(notification))
5458
.build();
5559
}
5660

61+
/**
62+
* Notification called when the cache decided to remove a data source.
63+
* @param notification Notification that a data source is removed.
64+
*/
65+
private void onRemoveDataSource(RemovalNotification<Object, Object> notification) {
66+
try {
67+
HikariDataSource dataSource = (HikariDataSource)notification.getValue();
68+
dataSource.close();
69+
}
70+
catch (Exception ex) {
71+
log.error("Cannot close a data source for the connection: " + notification.getKey().toString() + ", error: " + ex.getMessage(), ex);
72+
}
73+
}
74+
5775
/**
5876
* Returns or creates a data source for the given connection specification.
5977
* @param connectionSpec Connection specification (should be not mutable).

dqops/src/main/java/com/dqops/connectors/mysql/MysqlSourceConnection.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,41 @@ public String buildListColumnsSql(String schemaName, List<String> tableNames) {
223223
if (mysqlParametersSpec.getMysqlEngineType() == MysqlEngineType.singlestoredb) {
224224
return SingleStoreDbSourceConnection.buildListColumnsSql(getConnectionSpec(), schemaName, tableNames, this.getInformationSchemaName());
225225
} else {
226-
return super.buildListColumnsSql(schemaName, tableNames);
226+
return this.buildListColumnsSqlForMySql(schemaName, tableNames);
227227
}
228228
}
229+
230+
/**
231+
* Creates an SQL for listing columns in the given tables.
232+
* @param schemaName Schema name (bigquery dataset name).
233+
* @param tableNames Table names to list.
234+
* @return SQL of the INFORMATION_SCHEMA query.
235+
*/
236+
public String buildListColumnsSqlForMySql(String schemaName, List<String> tableNames) {
237+
StringBuilder sqlBuilder = new StringBuilder();
238+
sqlBuilder.append("SELECT * FROM ");
239+
240+
sqlBuilder.append(getInformationSchemaName());
241+
sqlBuilder.append(".COLUMNS ");
242+
sqlBuilder.append("WHERE TABLE_SCHEMA='");
243+
sqlBuilder.append(schemaName.replace("'", "''"));
244+
sqlBuilder.append("'");
245+
246+
if (tableNames != null && tableNames.size() > 0) {
247+
sqlBuilder.append(" AND TABLE_NAME IN (");
248+
for (int ti = 0; ti < tableNames.size(); ti++) {
249+
String tableName = tableNames.get(ti);
250+
if (ti > 0) {
251+
sqlBuilder.append(",");
252+
}
253+
sqlBuilder.append('\'');
254+
sqlBuilder.append(tableName.replace("'", "''"));
255+
sqlBuilder.append('\'');
256+
}
257+
sqlBuilder.append(") ");
258+
}
259+
sqlBuilder.append("ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION");
260+
String sql = sqlBuilder.toString();
261+
return sql;
262+
}
229263
}

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<dependency>
4747
<groupId>mysql</groupId>
4848
<artifactId>mysql-connector-java</artifactId>
49-
<version>8.0.30</version>
49+
<version>8.0.33</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>org.mariadb.jdbc</groupId>
@@ -71,7 +71,7 @@
7171
<dependency>
7272
<groupId>net.snowflake</groupId>
7373
<artifactId>snowflake-jdbc</artifactId>
74-
<version>3.13.32</version>
74+
<version>3.15.1</version>
7575
</dependency>
7676
</dependencies>
7777
</dependencyManagement>

0 commit comments

Comments
 (0)